laravel mcrypt_encrypt():大小的密钥

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31125545/
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-09-14 11:51:31  来源:igfitidea点击:

mcrypt_encrypt(): Key of size

phplaravelencryptionmcrypt

提问by Ahmet Zeybek

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

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

http://i.stack.imgur.com/qE1ZD.png

http://i.stack.imgur.com/qE1ZD.png

How can I fix this??

我怎样才能解决这个问题??

回答by Tom Pimienta

Used to be if your key was too short that PHP would pad it with \0. This is no longer the case since PHP version 5.6.0. You should check how big required key is for the cipher being used: http://php.net/manual/en/function.mcrypt-get-key-size.phpNote there are other ways to check key size, check the documentation. Simple way I understand key size: a string like 'fubar' in ASCII is 5 * 8 = 40 bytes (8 bytes per char). But that's making assumptions about character set in use. Some comments at php.net better explain how to roll a key of correct size:

曾经是,如果您的密钥太短,PHP 会用 \0 填充它。自 PHP 5.6.0 版起不再是这种情况。您应该检查正在使用的密码所需的密钥有多大:http: //php.net/manual/en/function.mcrypt-get-key-size.php注意还有其他方法可以检查密钥大小,请查看文档. 我理解密钥大小的简单方法:像 ASCII 中的 'fubar' 这样的字符串是 5 * 8 = 40 个字节(每个字符 8 个字节)。但这是对使用中的字符集的假设。php.net 上的一些评论更好地解释了如何滚动正确大小的密钥:

$key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");

$key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");

Here the 64 char string will be converted into 32 byte key because bc is a byte, b0 is another, etc. From http://php.net/manual/en/function.mcrypt-encrypt.php

这里 64 个字符的字符串将被转换为 32 个字节的密钥,因为 bc 是一个字节,b0 是另一个等等。来自http://php.net/manual/en/function.mcrypt-encrypt.php

You can double check number of bytes with strlen(). From above example strlen($key) will print out 32.

您可以使用 strlen() 仔细检查字节数。从上面的例子 strlen($key) 将打印出 32。