如何在PHP中进行AES256解密? [英] How to do AES256 decryption in PHP?

查看:850
本文介绍了如何在PHP中进行AES256解密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些需要解密的加密文本.它使用AES-256-CBC加密.我有加密的文本,密钥和iv.但是,无论我尝试什么,我似乎都无法使其正常工作.

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.

互联网建议mcrypt的Rijndael密码应该能够做到这一点,所以这就是我现在所拥有的:

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

目前,我收到2条警告,并且输出乱码:

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

任何帮助将不胜感激.

推荐答案

我对这些东西并不十分熟悉,但是尝试用MCRYPT_RIJNDAEL_256代替MCRYPT_RIJNDAEL_128将是显而易见的下一步...

I'm not terribly familiar with this stuff, but it seems like trying MCRYPT_RIJNDAEL_256 in place of MCRYPT_RIJNDAEL_128 would be an obvious next step...

编辑:您是对的,这不是您所需要的.实际上,MCRYPT_RIJNDAEL_128是正确的选择.根据您提供的链接,密钥和IV的长度是应有的两倍:

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:

// How do you do 256-bit AES encryption in PHP vs. 128-bit AES encryption???
// The answer is:  Give it a key that's 32 bytes long as opposed to 16 bytes long.
// For example:
$key256 = '12345678901234561234567890123456';
$key128 = '1234567890123456';

// Here's our 128-bit IV which is used for both 256-bit and 128-bit keys.
$iv =  '1234567890123456';

这篇关于如何在PHP中进行AES256解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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