最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - node crypto-js AES encrypt -> decrypt usage? - Stack Overflow

programmeradmin2浏览0评论

I am trying to generate a simple test of crypto-js on node as follows:

'use strict';

var AES = require('crypto-js/aes');
var key = 'passPhrase';
var ecr = function(str)
{
    return AES.encrypt(str, key);
};
var dcr = function(str)
{
    return AES.decrypt(str, key);
};

console.log(dcr(ecr('hello world')));
// expected result is:  hello world

The actual result is:

{ words: [ 1751477356, 1864398703, 1919706117, 84215045 ],
  sigBytes: 11 }

What is the right usage?

I am trying to generate a simple test of crypto-js on node as follows:

'use strict';

var AES = require('crypto-js/aes');
var key = 'passPhrase';
var ecr = function(str)
{
    return AES.encrypt(str, key);
};
var dcr = function(str)
{
    return AES.decrypt(str, key);
};

console.log(dcr(ecr('hello world')));
// expected result is:  hello world

The actual result is:

{ words: [ 1751477356, 1864398703, 1919706117, 84215045 ],
  sigBytes: 11 }

What is the right usage?

Share Improve this question edited Jun 27, 2016 at 9:38 gevorg 5,0755 gold badges38 silver badges54 bronze badges asked Dec 23, 2013 at 7:20 user1028880user1028880
Add a ment  | 

2 Answers 2

Reset to default 3

I modified the code to deal any object:

'use strict';

var CryptoJS = require('crypto-js');
var key = 'pass phrase';
var ecr = function(obj)
{
    return CryptoJS.AES.encrypt(JSON.stringify(obj), key);
};
var dcr = function(obj)
{
    return JSON.parse(CryptoJS.AES.decrypt(obj, key)
        .toString(CryptoJS.enc.Utf8));
};

var s = 'hello world';
console.log(dcr(ecr(s)));

var obj = {
    id: 'ken',
    key: 'password'
};
console.log(dcr(ecr(obj)));

Oh well.. Working Code:

'use strict';

var CryptoJS = require('crypto-js');
var key = 'pass phrase';
var ecr = function(str)
{
    return CryptoJS.AES.encrypt(str, key);
};
var dcr = function(str)
{
    return CryptoJS.AES.decrypt(str, key)
        .toString(CryptoJS.enc.Utf8);
};

console.log(dcr(ecr('hello world')));

Result:

hello world
发布评论

评论列表(0)

  1. 暂无评论