Node JS中的Angular CryptoJs加密不解密CryptoJS [英] Angular CryptoJs Encryption Not Decryption in Node JS CryptoJS

查看:110
本文介绍了Node JS中的Angular CryptoJs加密不解密CryptoJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Anguarjs代码

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'
    });

节点JS代码要使用AES-256-cbc算法解密,密钥与angular密钥相同。

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();
}});

我无法解密有角度的加密。如果本身使用,它们都可以正常工作。如果我解密角度本身的字符串,它的工作原理同样适用于节点。但是Cross平台无法正常工作,谁能暗示我的方法有什么问题。任何帮助,将不胜感激。
我也尝试过从两侧删除自动填充,也无法对缓冲区进行加密/解密。 预先感谢。

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.

推荐答案

问题是节点加密库使用随机盐进行编码和解码,八进制cryptoJS则没有。因此,如果要对角度加密并在节点中解密,则需要使用 node-cryptojs-aes

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>');

这篇关于Node JS中的Angular CryptoJs加密不解密CryptoJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆