Rec*_*lic 2 php encryption openssl
我正在用PHP构建一个应用程序,我想使用非对称加密在两个用户之间交换消息.
为了这个问题忽略密钥管理和其他外部因素,这真的很简单吗?
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$res = openssl_pkey_new($config);
openssl_pkey_export($res, $privKey);
$pubKey = openssl_pkey_get_details($res);
$pubKey = $pubKey["key"];
openssl_public_encrypt('this was encrypted with the public key', $secret, $pubKey);
openssl_private_decrypt($secret, $decrypted, $privKey);
echo "Decrypted message: $decrypted
Run Code Online (Sandbox Code Playgroud)
为了这个问题忽略密钥管理和其他外部因素,这真的很简单吗?
获取一般工作的代码,用于短输入?是.
获取安全工作的代码,以获取任何长度的输入?我们都不希望!但不是.
为了好玩,尝试使用此代码加密整个博客文章.如果您的博文明显长于推文,那将会很难.
<?php
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$res = openssl_pkey_new($config);
openssl_pkey_export($res, $privKey);
$pubKey = openssl_pkey_get_details($res);
$pubKey = $pubKey["key"];
$message = file_get_contents('https://paragonie.com/blog/2018/04/protecting-rsa-based-protocols-against-adaptive-chosen-ciphertext-attacks/source');
openssl_public_encrypt($message, $secret, $pubKey);
var_dump($secret);
Run Code Online (Sandbox Code Playgroud)
这将输出NULL,因为消息大小明显长于RSA允许的大小.
你可能想把你的消息分成几块然后独立解密.消息可以随意删除或重新排序,它仍然可以解密.它在语义上不安全.
此外,默认填充模式(PKCS#1 v1.5)容易受到可追溯到1998年的攻击.
因此,如果您想在PHP中使用非对称加密而没有严重的限制或容易受到过多的旁道攻击,您最终需要执行以下操作之一:
sodium_crypto_box_seal()和sodium_crypto_box_seal_open().您甚至不必知道这些功能可以安全地使用它们.就这么简单.因此,实质上:是的,您的示例代码是"您需要做的所有"以获得有效的代码.然而,使RSA安全是一项艰巨的努力,不能轻视.
最后,如有疑问,请聘请密码学家.