最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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
Post a comment

comment list (0)

  1. No comments so far