一尘不染

如何在PHP中进行AES256解密?

php

我有一些需要解密的加密文本。它使用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

任何帮助,将不胜感激。


阅读 582

收藏
2020-05-29

共1个答案

一尘不染

我并不十分熟悉的东西,但它似乎是试图MCRYPT_RIJNDAEL_256替代MCRYPT_RIJNDAEL_128将是一个明显的下一个步骤…

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

// 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';
2020-05-29