gpt4 book ai didi

php - 为什么 openssl_public_encrypt 不能处理这个明文?

转载 作者:可可西里 更新时间:2023-11-01 01:15:21 27 4
gpt4 key购买 nike

openssl_public_encrypt 显然无法处理任意明文。

<?php

$msg = '{"args":["argxx","argyy"],"data":"xx\nyyy\n","symkey":"0a6e860640413acfe6e4e461a28fab3fad3aff78ef95c962c5e63bef7e2b3439"}';

# If you uncomment this line, the function succeeds.
# $msg = 'test';

$pub = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC80g7AA5XexMCiJW3tKr/eeN8Q
EMNfGYG0qiUuLS/dtir7c3c1MmpNzrE8R+xqleOLNVkbbSZqqQ2qUJtPhwbLhQyL
yilRH5WMz9Pabx62v7k+vm81/6Xa9fnIV7DE0DZhMO5vQvBE3+5jkXbfU4yBZRv5
UOty5gqGXXaR6bim4QIDAQAB
-----END PUBLIC KEY-----';


if (openssl_public_encrypt ($msg, $enc, $pub))
{
print bin2hex ($enc);

exit (0);
}
else
{
print "Could not encrypt message.\n";
}
?>

这会在 Ubuntu PHP 7 上输出 Could not encrypt message.

为什么它在这个 $msg 上失败了?

最佳答案

如果我们凭经验测试这种情况,我们会发现,所有超过 117 字节的内容都会失败:

$msg = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm';

上面这一行总共代表了 117 个字符和 117 个字节。这在使用您提供的公钥加密时有效。

如果我添加另一个字符,n,加密失败:

$msg = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmn';

其他 unicode 字符也是如此。假设我尝试加密它,它有 85 个字符长,但恰好有 117 个字节:

$msg = ' i ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui  u';

这得到了完美的加密。但是如果我添加另一个字节,它会失败(86 个字符,118 个字节):

$msg = ' i ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui ♥ ui  uZ';

.. the openssl_public_encrypt() function will fail by design if you pass it more than 117 characters to encrypt.

Snyder, Chris, Myer, Thomas, Southwell, Michael, ISBN 978-1-4302-3318-3

另外,书上说:

Because RSA is expensive, and was never intended for encrypting quantities of data, if you are encrypting something that is routinely longer than 56 characters, you should be planning to encrypt your data using a fast and efficient symmetric algorithm like AES with a randomly generated key.

关于php - 为什么 openssl_public_encrypt 不能处理这个明文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40710825/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com