我很难尝试解密在 PHP 中使用 Blowfish CBC 加密的字符串;
我正在使用 Node.js 在 Javascript 中使用 Mocha 编写的单元测试在客户端工作。我正在使用 Node 的内置加密功能。
这是用于解密 PHP 中的字符串的代码。
<?php
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
$iv = "my_iv";
$key = substr("my_key", 0, 56);
// I read that php automaticly pads my key with \0 to make it 56char long.
if (mcrypt_generic_init($cipher, $key, $iv) != -1) {
$text = mdecrypt_generic($cipher, urldecode("X%10%8F%AD%1C%1D%E1f%29%CD%D2%E1%81%B0%BE%D2M%8E3%D6%5DO%29%E3%E6%EF%24%A0%E0g%22%DA%D7YK%5B%AA%E6P%91%BA%B8U%C1%87k%7F%07%FD%9D%BF%1F%BD%95r%04%F0%18%A9%27%E17%2C%A8"));
mcrypt_generic_deinit($cipher);
echo rtrim($text, "\0");
} else {
echo "ERRROR";
}
?>
这段代码给了我正确的输出,它是一个作为字符串的序列化数组。
然后我尝试了两个库在 JS 中使用相同的流程进行解密,
使用加密(内置节点):
var crypto = require('crypto');
var iv = new Buffer('my_iv');
var key = new Buffer('my_key');
var text = new Buffer(unescape("X%10%8F%AD%1C%1D%E1f%29%CD%D2%E1%81%B0%BE%D2M%8E3%D6%5DO%29%E3%E6%EF%24%A0%E0g%22%DA%D7YK%5B%AA%E6P%91%BA%B8U%C1%87k%7F%07%FD%9D%BF%1F%BD%95r%04%F0%18%A9%27%E17%2C%A8"));
var decipher = crypto.createDecipheriv('bf-cbc', key, iv);
var decrypted = decipher.update(text, '', '');
decrypted += decipher.final('');
console.log(decrypted);
给我这个错误:
error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
TypeError: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
at Decipheriv.Cipher.final (crypto.js:320:27)
并使用节点的 mcrypt 模块:https ://github.com/tugrul/node-mcrypt
var MCrypt = require('mcrypt').MCrypt;
var bfCcb = new MCrypt('blowfish', 'cbc');
var iv = Buffer.from('my_iv');
var key = Buffer.from('my_key_padded_with_0\0\0');
var text = Buffer.from(unescape("X%10%8F%AD%1C%1D%E1f%29%CD%D2%E1%81%B0%BE%D2M%8E3%D6%5DO%29%E3%E6%EF%24%A0%E0g%22%DA%D7YK%5B%AA%E6P%91%BA%B8U%C1%87k%7F%07%FD%9D%BF%1F%BD%95r%04%F0%18%A9%27%E17%2C%A8"));
bfCcb.open(key, iv);
var plaintext = bfCcb.decrypt(text);
console.log(plaintext.toString());
它给了我错误的输出,
我不知道它是否与可能的编码有关,或者我是否在配置我的 javascripts 对象时遗漏了一些东西......
如果有人已经遇到过这个问题,请告诉我。