php mcrypt_decrypt() 错误更改密钥大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27254432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:16:04  来源:igfitidea点击:

mcrypt_decrypt() error change key size

phpencryptionmcrypt

提问by Asaf Maoz

mcrypt_decrypt(): Key of size 15 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported

mcrypt_decrypt():此算法不支持大小为 15 的密钥。仅支持大小为 16、24 或 32 的键

How Can I fix this issue? my key is set - can not change it. It has to be a local change, I think my local PHP version is too advanced for the project I loaded. How can I fix this?

我该如何解决这个问题?我的密钥已设置 - 无法更改。它必须是本地更改,我认为我本地的 PHP 版本对于我加载的项目来说太高级了。我怎样才能解决这个问题?

回答by Hanky Panky

Did you update to 5.6? It says

你更新到5.6了吗?它说

Invalid key and iv sizes are no longer accepted. mcrypt_decrypt() will now throw a warning and return FALSE if the inputs are invalid. Previously keys and IVs were padded with '\0' bytes to the next valid size.

不再接受无效的密钥和 iv 大小。如果输入无效,mcrypt_decrypt() 现在将抛出警告并返回 FALSE。以前的键和 IV 用 '\0' 字节填充到下一个有效大小。

Reference

参考

Read the last line of that quote, and there you will find your solution :)

阅读该引用的最后一行,您将在那里找到您的解决方案:)

mcrypt_decrypt(): Key of size 15 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported

mcrypt_decrypt():此算法不支持大小为 15 的密钥。仅支持大小为 16、24 或 32 的键

That means you need to pad your key with \0(that's what previous versions were doing for you)

这意味着您需要用\0(这是以前版本为您所做的)来填充您的密钥

$key=$key."
function pad_key($key){
    // key is too large
    if(strlen($key) > 32) return false;

    // set sizes
    $sizes = array(16,24,32);

    // loop through sizes and pad key
    foreach($sizes as $s){
        while(strlen($key) < $s) $key = $key."
Application key [EaaJgaD0uFDEg7tpvMOqKfAQ46Bqi8Va] set successfully.
"; if(strlen($key) == $s) break; // finish if the key matches a size } // return return $key; }
";

回答by troskater

I went ahead and created a function based on Hanky ? Panky's answer.

我继续创建了一个基于Hanky的函数潘基的回答

This can be used with any key length to make sure it's the correct size.

这可以与任何密钥长度一起使用,以确保其大小正确。

function padKey($key) 
{
    // Get the current key size
    $keySize = strlen($key);

    // Set an array containing the valid sizes
    $validSizes = [16,24,32];

    // Loop through sizes and return correct padded $key
    foreach($validSizes as $validSize) {
        if ($keySize <= $validSize) return str_pad($key, $validSize, "
public function setKey($key) {
    $len = strlen($key);
    if($len < 24 && $len != 16){
        $key = str_pad($key, 24, "
<?php
  function encryptCookie($value){
    if(!$value){return false;}
    $key = 'aNdRgUkXp2s5v8y/B?E(H+MbQeShVmYq';
    $text = $value;
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
    return trim(base64_encode($crypttext)); //encode for cookie
   }
 function decryptCookie($value){
    if(!$value){return false;}
    $key = 'aNdRgUkXp2s5v8y/B?E(H+MbQeShVmYq';
    $crypttext = base64_decode($value); //decode cookie
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
    return trim($decrypttext);
   }
?>
", STR_PAD_RIGHT); } elseif ($len > 24 && $len < 32) { $key = str_pad($key, 32, "##代码##", STR_PAD_RIGHT); }elseif ($len > 32){ $key = substr($key, 0, 32); } $this->key = $key; }
"); } // Throw an exception if the key is greater than the max size throw new Exception("Key size is too large"); }

回答by cyber8200

For Laravel 5

对于 Laravel 5

Just run php artisan key:generate:

只需运行php artisan key:generate

##代码##

If you don't see your key updated, just paste it in your .envfile.

如果您没有看到您的密钥已更新,只需将其粘贴到您的.env文件中即可。

APP_KEY=EaaJgaD0uFDEg7tpvMOqKfAQ46Bqi8Va

APP_KEY=EaaJgaD0uFDEg7tpvMOqKfAQ46Bqi8Va

Refresh your page

刷新页面

回答by zgr024

You can just use str_pad() for this. In its simplest form, this will suffice.

您可以为此使用 str_pad() 。以最简单的形式,这就足够了。

##代码##

The other answers will do just fine. I'm just taking advantage of the built in PHP function str_pad here instead of appending "\0" in a loop.

其他答案会很好。我只是在这里利用内置的 PHP 函数 str_pad 而不是在循环中附加“\0”。

回答by Brac

You don't need to pad the key with "\0".

您不需要用“\0”填充键。

I had the same issue when migrating to a new PHP 7 server and I got the message :

我在迁移到新的 PHP 7 服务器时遇到了同样的问题,我收到了消息:

mcrypt_decrypt(): Key of size 19 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported.

mcrypt_decrypt():此算法不支持大小为 19 的密钥。仅支持大小为 16、24 或 32 的键。

The key I had in the code was a string of 19 characters, I simply changed it to a string of 32 characters and everything was fine again.

我在代码中的关键是一个 19 个字符的字符串,我只是将它更改为一个 32 个字符的字符串,一切又好了。

So as the error message suggests, use a valid size key.

因此,正如错误消息所暗示的那样,请使用有效的大小键。

回答by dan-iel

I had this issue with OSTicket 1.6 ST (yes old version I know). Hosting company just went to PHP 5.6 and it broke the Mail Fetch for cron.php. I'm posting this hoping it helps others fix this issue faster.

我在 OSTicket 1.6 ST 上遇到了这个问题(是的,我知道是旧版本)。托管公司刚刚使用 PHP 5.6,它破坏了 cron.php 的邮件获取。我发布这个希望它可以帮助其他人更快地解决这个问题。

You have to edit the file "include/class.misc.php".

您必须编辑文件“include/class.misc.php”。

Add the function "pad_key" provided in the answer authored by @troskater to the "include/class.misc.php" file and then on line 51 in the function "decrypt" change

将@troskater 撰写的答案中提供的函数“pad_key”添加到“include/class.misc.php”文件中,然后在函数“decrypt”中的第 51 行更改

return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt,...

返回修剪(mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$salt,...

to instead use

改为使用

return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, pad_key($salt),...

返回修剪(mcrypt_decrypt(MCRYPT_RIJNDAEL_256,pad_key($salt),...

回答by Dimitar Kalenderov

I had the same problem, but fixed it with this

我有同样的问题,但用这个修复了它

##代码##

回答by JCBrown

If your encryption code looks like this:

如果您的加密代码如下所示:

##代码##

You will want to change the $key to a 128 or 256 bit encrypted code. I simply copied a code that I generated from here: Generate Code

您需要将 $key 更改为 128 或 256 位加密代码。我只是复制了我从这里生成的代码生成代码

I created a 256 bit code for mine which consists of 32 characters and thus fixes the issue of the invalid key size of 15 or whatever number is causing the error. So whatever is set for $key you need to change that to a valid code and then it should work fine.

我为我的代码创建了一个 256 位代码,它由 32 个字符组成,从而解决了无效密钥大小 15 或导致错误的任何数字的问题。因此,无论为 $key 设置什么,您都需要将其更改为有效代码,然后它应该可以正常工作。