尝试在node.js中重新创建以下php加密代码:
$size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($size, MCRYPT_RAND);
$msg = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, 'MY_KEY_LONG_STRING', 'PLAINTEXT', MCRYPT_MODE_ECB, $iv));
试过这个:
var text = 'PLAINTEXT';
var len = text.length;
for (var i = 0; i < 16 - len % 16; i++) { // pad to multiple of block size
text += '\0';
}
var key = 'MY_KEY_LONG_STRING';
key = key.substr(0, 16); // trim to expected key size for aes128
var cipher = crypto.createCipher('aes-128-ecb', key);
cipher.setAutoPadding(false); // did our own padding, to match mcrypt_encrypt
var encrypted = cipher.update(text, 'utf8', 'base64');
encrypted += cipher.final('base64');
从php获得不同的结果...
还尝试使用IV创建密码(甚至不应该在aes-128-ecb中使用):
var cipher = crypto.createCipheriv('aes-128-ecb', key, '');
另外,php的结果不同。任何想法如何使这个行为完全像PHP版本?
答案 0 :(得分:3)
互相玩两个相当不健康的图书馆可能很有趣。我会按照要求帮助您完成所有工作,而不是完成所有工作:
aes-192-ecb
作为算法(你需要继续使用MCRYPT_RIJNDAEL_128
但替代,PHP中的128是块大小而不是密钥大小)createCipher
,因为第二个参数是密码;如果你使用那个,node.js会执行密钥派生,所以你需要使用三个参数createCipher
代替并提供任何16字节的IV PHP中的IV代码只是不必要地对随机数生成器征税,不使用IV。
执行填充的代码
var padSize = 16 - ((len + 16 - 1) % 16 + 1);
for (var i = 0; i < padSize; i++) { // pad 0 .. 15 until to multiple of block size
text += '\0';
}
或者您可以使用自己的填充方法,除非 len % 16 == 0
。
答案 1 :(得分:1)
这是我的代码,用于解决从PHP迁移到NodeJS的问题。我有严格的32字节密钥,所以我必须使用$text = "Some super mega text I want to encode";
$skey = "rcbTw667C7zxghZ5U3gwhQlp22t8c5Rq";
function encode($text,$skey) {
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $skey, $text, MCRYPT_MODE_ECB, $iv);
return base64_encode($crypttext);
}
echo encode($text,$skey);
。
PHP代码我想重写:
dU1eOy+YwkYvm/KCTB8aqR1UsisyrHrvBu+E/H3G/s0aagMDKlNFekGXNQyFMFJD
输出:
var crypto = require('crypto');
var text = "Some super mega text I want encode";
var skey = "rcbTw667C7zxghZ5U3gwhQlp22t8c5Rq";
function encode(text, skey) {
var len = text.length;
var padSize = 16 - ((len + 16 - 1) % 16 + 1);
for (var i = 0; i < padSize; i++) {
text += '\0';
}
var cipher = crypto.createCipheriv('aes-256-ecb', skey, '');
cipher.setAutoPadding(false);
var encrypted = cipher.update(text, 'utf8', 'base64');
encrypted += cipher.final('base64');
return encrypted;
}
console.log(encode(text, skey));
<强>的NodeJS:强>
dU1eOy+YwkYvm/KCTB8aqR1UsisyrHrvBu+E/H3G/s0aagMDKlNFekGXNQyFMFJD
输出:
npm install mcrypt
NodeJS mcrypt包也适用:
var text = "Some super mega text I want encode";
var skey = "rcbTw667C7zxghZ5U3gwhQlp22t8c5Rq";
function encode(text, skey) {
var MCrypt = require('mcrypt').MCrypt;
var rijEcb = new MCrypt('rijndael-128', 'ecb');
rijEcb.open(skey);
var ciphertext = rijEcb.encrypt(text);
return ciphertext.toString('base64');
}
console.log(encode(text, skey));
代码:
dU1eOy+YwkYvm/KCTB8aqR1UsisyrHrvBu+E/H3G/s0aagMDKlNFekGXNQyFMFJD
输出:
main
答案 2 :(得分:0)
我尝试了其他选择,但无济于事,
function encrypt(text) {
text = '' + text;
var crypto = require('crypto');
var len = text.length;
var padSize = 16 - (((len + 16 - 1) % 16) + 1);
var data = String.fromCharCode(padSize);
var text = text + data.repeat(padSize);
var cipher = crypto.createCipheriv('aes-128-ecb','secretKey', '');
cipher.setAutoPadding(false);
var encrypted = cipher.update(text, 'utf8', 'base64');
encrypted += cipher.final('base64');
return encrypted;
}
PHP中的类似语言
function aes128Encrypt($str,$key){
$block = mcrypt_get_block_size('rijndael_128', 'ecb');
$pad = $block - (strlen($str) % $block);
$str .= str_repeat(chr($pad), $pad);
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_ECB));
}