1

This is my Anguarjs Code

 $httpProvider.defaults.headers.common['key'] = CryptoJS.AES.encrypt('<datatoencrypt>', '<key let says xyx>=', {
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
        keySize: '256 / 32'
    });

Node JS Code To decrypt using, algorithm as aes-256-cbc and key as same as angular.

app.all('*', function (req, res, next) {
var headers = JSON.parse(JSON.stringify(req.headers));
var decipher = crypto.createDecipher(algorithm, key);
decipher.setAutoPadding(true);
var dec = decipher.update(headers.key, 'hex', 'utf8');
dec += decipher.final('utf8');
if (dec != "<datatoencrypt>")
{
    //do something
    next();
}
else
{
    //do something
    next();
}});

I am unable to decrypt the encryption done in angular. They both work fine if used in itself. if i decrypt the string in angular itself it work same goes for node. But Cross platform its not working can anyone suggest what is wrong with my approach. Any help would be appreciated. I have tried removing autopadding form both sides as well, also buffers encrypt/decrypt is not working. Thanks in advance.

1 Answer 1

2

The problem was node crypto library uses random salt for encoding and decoding, augular cryptoJS does not. So if you want encrypt in angular and decrypt in node you need to use node-cryptojs-aes

var CryptoJS = require('node-cryptojs-aes').CryptoJS;
function decrypt(text) {
try {
    var decrypted = CryptoJS.AES.decrypt(text, key);
    return decrypted.toString(CryptoJS.enc.Utf8);
} catch (ex)
{
    console.log(ex);
}}

Key must be same for both, in angular code will go like

var encry = CryptoJS.AES.encrypt('<texttoecrypt>', <key>');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.