我正在尝试查看消息是否在中间被损坏,我应该能够得到一个错误,但我看到的只是一个白色页面。
<?php
$keypair = hex2bin('66b70b4e93416f8a7a82a40a856fe9884fd7a6e5018837c5421f507307026b40b2c8fbaf820ee38198af1dcf23143ec7ae21da1c785f58d1053940b9f317180e');
$encrypted_text = hex2bin('de261df126463f57b6c38bf42b69252b2f9382267b51e137e20e27ace37c5853279b00c95536cc9a44945146376c5d94355ae0bab5c1eb0ceb9669002ee5dd13e7aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
$decrypted_text = sodium_crypto_box_seal_open($encrypted_text, $keypair);
echo $decrypted_text;
?>正如你所看到的,在$encrypted_text的末尾,我应该得到一个错误,但是没有错误。
发布于 2019-01-14 04:12:49
如果消息无法解密,则sodium_crypto_box_seal_open()返回FALSE。
您应该将它的输出与FALSE进行比较,而不是检查它是否为空,因为加密一条空消息是完全可以的。将对空消息进行身份验证,如果密钥不正确,将拒绝空消息。
此外,如果涉及机密,则应使用sodium_bin2hex()和sodium_hex2bin(),它们旨在避免旁路
发布于 2019-01-11 15:51:52
level的功能是低级的。可以使用任何wrapper package来简化使用,也可以自己创建一个:
interface Decryptor
{
public function decrypt(string $input): string;
}
final class LibsodiumDecryptor implements Decryptor
{
private $keyPair;
public function __construct(string $keyPair)
{
$this->keyPair = hex2bin($keyPair);
}
public function decrypt(string $input): string
{
$decrypted = sodium_crypto_box_seal_open(hex2bin($input), $this->keyPair);
if (empty($decrypted)) {
throw new \RuntimeException('Encryption failed');
}
return $decrypted;
}
}
$crypto = new LibsodiumDecryptor('66b70b4e93416f8a7a82a40a856…');
echo $crypto->decrypt('de261df126463f57b6aaaaaaaaaaa…');https://stackoverflow.com/questions/54141953
复制相似问题