问题描述
我正在使用libsodium进行加密,我的问题是解密部分,它没有通过并显示错误.
I'm working on the encryption using libsodium my problem is the decryption part, its not going through and displaying error.
我试图在加密代码中复制一些行,但是没有用.
I tried to copy some lines in the encryption code but didn't work.
我也收到警告.
但是我不知道这是否是原因.我也收到加密的邮件.
But I don't know if this is the cause. I also receive this on encryption.
警告:unpack():64位格式代码不适用于32位 第12行的C:\ xampp \ htdocs \ encrypter \ decrypt.php中的PHP版本
Warning: unpack(): 64-bit format codes are not available for 32-bit versions of PHP in C:\xampp\htdocs\encrypter\decrypt.php on line 12
更新
-
通过将pack()代码从
P
更改为V
来解决警告.
更改代码后,$opslimit
的值大于0.
Upon changing the code the $opslimit
has a value more than 0.
解密代码
$password = 'password';
$encrypted_file = 'tmp/inc.php';
$decrypted_file = 'tmp/inc.dec';
$fd_in = fopen($encrypted_file, 'rb');
$fd_out = fopen($decrypted_file, 'wb');
$alg = unpack('C', fread($fd_in, 1))[1];
$opslimit = unpack('V', fread($fd_in, 8))[1];
$memlimit = unpack('V', fread($fd_in, 8))[1];
$salt = fread($fd_in, SODIUM_CRYPTO_PWHASH_SALTBYTES);
$header = fread($fd_in, SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES);
$secret_key = sodium_crypto_pwhash(SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES,
$password, $salt, $opslimit, $memlimit, $alg);
$stream = sodium_crypto_secretstream_xchacha20poly1305_init_pull($header, $secret_key);
do {
$chunk = fread($fd_in, $chunk_size + SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES);
$res = sodium_crypto_secretstream_xchacha20poly1305_pull($stream, $chunk);
if ($res === FALSE) {
break;
}
list($decrypted_chunk, $tag) = $res;
fwrite($fd_out, $decrypted_chunk);
} while (!feof($fd_in) && $tag !== SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL);
$ok = feof($fd_in);
fclose($fd_out);
fclose($fd_in);
if (!$ok) {
die('Invalid/corrupted input');
}
这是我从 libsodium示例.
推荐答案
该代码确实不是为32位版本的PHP设计的.
The code was indeed not designed for 32 bit versions of PHP.
如果将P
更改为V
,则需要:
If you change P
to V
, you need to:
- 在对
unpack()
的调用和对pack()
的调用中都执行此操作 - 将读取/写入的字节数从8更改为4.
- Do it both in calls to
unpack()
and calls topack()
- Change the number of bytes read/written from 8 to 4.
但是最好的办法实际上是尝试理解代码的作用.
But the best thing to do would actually be trying to understand what the code does.
它在文件的开头存储内存限制和迭代次数,以便稍后在读取文件时可以恢复这些参数,而不必对其进行硬编码.
It stores the memory limit and iterations at the beginning of the file, so that these parameters can be recovered later when reading the file, without having to hard-code them.
pack()
以固定的字节数编码值. unpack()
则相反.pack('P')
将64位值编码为8个字节. unpack('P')
读取8个字节并将其转换为值.
pack()
encodes a value in a fixed number of bytes. unpack()
does the opposite.pack('P')
encodes a 64 bit value into 8 bytes. unpack('P')
reads 8 bytes and converts them into a value.
如果您的环境不支持64位值,请打包/解压缩为4个字节,但是随后您需要写入4个字节而不是8个字节,并且还需要读取4个字节而不是8个字节.
If your environment doesn't support 64 bit values, pack/unpack to 4 bytes, but you then need to write 4 bytes, not 8. And read 4 bytes as well, not 8.
这篇关于无法使用libsodium解密加密的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!