如何在PHP中进行AES256解密?
我有一段加密的文本需要解密。它使用 AES-256-CBC 加密。我有加密文本、密钥和 iv。然而,无论我如何尝试,我似乎都无法让它发挥作用。
互联网建议 mcrypt 的 Rijndael 密码应该能够做到这一点,所以这就是我现在所拥有的:
function decrypt_data($data, $iv, $key) {
$cypher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
// initialize encryption handle
if (mcrypt_generic_init($cypher, $key, $iv) != -1) {
// decrypt
$decrypted = mdecrypt_generic($cypher, $data);
// clean up
mcrypt_generic_deinit($cypher);
mcrypt_module_close($cypher);
return $decrypted;
}
return false;
}
就目前情况而言,我收到 2 个警告,并且输出是乱码:
Warning: mcrypt_generic_init() [function.mcrypt-generic-init]: Key size too large; supplied length: 64, max: 32 in /var/www/includes/function.decrypt_data.php on line 8
Warning: mcrypt_generic_init() [function.mcrypt-generic-init]: Iv size incorrect; supplied length: 32, needed: 16 in /var/www/includes/function.decrypt_data.php on line 8
任何帮助将不胜感激。
I have an encrypted bit of text that I need to decrypt. It's encrypted with AES-256-CBC. I have the encrypted text, key, and iv. However, no matter what I try I just can't seem to get it to work.
The internet has suggested that mcrypt's Rijndael cypher should be able to do this, so here's what I have now:
function decrypt_data($data, $iv, $key) {
$cypher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
// initialize encryption handle
if (mcrypt_generic_init($cypher, $key, $iv) != -1) {
// decrypt
$decrypted = mdecrypt_generic($cypher, $data);
// clean up
mcrypt_generic_deinit($cypher);
mcrypt_module_close($cypher);
return $decrypted;
}
return false;
}
As it stands now I get 2 warnings and the output is gibberish:
Warning: mcrypt_generic_init() [function.mcrypt-generic-init]: Key size too large; supplied length: 64, max: 32 in /var/www/includes/function.decrypt_data.php on line 8
Warning: mcrypt_generic_init() [function.mcrypt-generic-init]: Iv size incorrect; supplied length: 32, needed: 16 in /var/www/includes/function.decrypt_data.php on line 8
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对这些东西不是很熟悉,但似乎尝试用
MCRYPT_RIJNDAEL_256
代替MCRYPT_RIJNDAEL_128
将是明显的下一步...编辑: 你是对的——这不是你需要的。
MCRYPT_RIJNDAEL_128
实际上是正确的选择。根据您提供的链接,您的密钥和 IV 的长度是应有的两倍:I'm not terribly familiar with this stuff, but it seems like trying
MCRYPT_RIJNDAEL_256
in place ofMCRYPT_RIJNDAEL_128
would be an obvious next step...Edit: You're right -- this isn't what you need.
MCRYPT_RIJNDAEL_128
is in fact the right choice. According to the link you provided, your key and IV are twice as long as they should be:我发给你一个例子,
请检查一下代码,好的
I send to you one example,
Please, check the code, ok