Axe*_*and 2 javascript cryptography aes node.js
我正在尝试使用nodeJS加密模块使用AES 128的ECB模式加密某些十六进制字符串.
为此,我使用以下代码:
cryptoAES = function (sInput, sKey, bEncrypt) {
return crypto('AES-128-ECB', sInput, sKey, bEncrypt);
};
crypto = function (sAlgo, sInput, sKey, bEncrypt) {
var result = "";
if (bEncrypt){
var cipher;
var bKey = new Buffer(sKey, 'hex');
var bInput = new Buffer(sInput, 'hex');
cipher = crypto.createCipher(sAlgo, bKey);
cipher.setAutoPadding(false);
result = cipher.update(bInput, null, 'hex');
result += cipher.final('hex');
}
return result;
};
Run Code Online (Sandbox Code Playgroud)
用以下方法调用cryptoAES时:
sKey = '12345678900987654321123456789001'
sInput = '060123456789ABCDEF00000000000000'
Run Code Online (Sandbox Code Playgroud)
我应该得到
result = 'FBECD5D02C5B7CD1055AAF86238D1E2F'
Run Code Online (Sandbox Code Playgroud)
但我得到了:
result = 'ea1f940da8e269b9e075c936bff6a1f7'
Run Code Online (Sandbox Code Playgroud)
知道我可能做错了吗?
阅读https://github.com/joyent/node/issues/1318#issuecomment-1562766,你就需要crypto.createCipheriv():
cipher = crypto.createCipheriv(sAlgo, bKey, '');
Run Code Online (Sandbox Code Playgroud)
这会产生所需的结果.
| 归档时间: |
|
| 查看次数: |
1468 次 |
| 最近记录: |