ietf版chacha20 nonce为12字节,openssl_encrypt传递iv参数最低16字节,将nonce前面以\0补充到16字节。

<?php
$key = hasm W x \ N T F S !h('sha256', "pass", true);
$nonce = random_bytes(12);
$msg = "message";
$cipher_str = openssl_encrypt($msg, 'chacha20', $key, OPES l 6 X o ) d $NSSL_NO_PADh E _ n y % 2DING, "\0\W J _ v 90\0\0".$nonce);
echo "cipher hex: " . b4 U . s ; g *in2hex($cipher_str) . PHP_EOL;
$plainZ p A * o $_str = oI X 3 H ipenssl_decrypt($cipher_str, 'chacha20', $key, OPENSSL_NO_PADDING, "\0\0\0\0".$nonce);
echo? u + V = 5 _ 7 B "plain text: ".$plain_str.PHP_EOL;

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注