How to decrypt a field sent from client to Node server using CryptoJS?

情到浓时终转凉″ 提交于 2019-12-13 04:34:19

问题


I am trying to adapt the following solution into my Node application to decrypt a field sent from the client browser via POST:

How to decrypt with CryptoJS using AES?

Seem to be going round in circles in getting values to match in the console. The values for the encryption of 'hello' match both applied from Server and from client (sending 'hello' however there is now no decryption value showing for either.

The server side code in a Node POST Route:

var ENC_KEY = "c2VjcmV0"; //'secret'
app.post('/hello', function (req, res) {
    console.log('POST /hello');

    var key = CryptoJS.enc.Base64.parse(ENC_KEY);

    console.log('key: ' + key);


    console.log('client msg ("hello"): ' + req.body.msg_hello);

    var encrypted = CryptoJS.AES.encrypt("hello", key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });

    console.log('server msg "hello" encrypted to: ' + encrypted);

    var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });

    console.log('server msg decrypted: ' + hex2a(decrypted));

    var decryptedClient = CryptoJS.AES.decrypt(req.body.msg_hello, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });



    console.log('client msg decrypted: ' + hex2a(decryptedClient));

    res.end(JSON.stringify('{ response: "response" }'));
});

Any help appreciated!

来源:https://stackoverflow.com/questions/27174971/how-to-decrypt-a-field-sent-from-client-to-node-server-using-cryptojs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!