I'm working on the encryption using libsodium my problem is the decryption part, its not going through and displaying error.
Fatal error: Uncaught SodiumException: ops limit must be greater than 0 in C:\xampp\htdocs\encrypter\decrypt.php:18 Stack trace: #0 C:\xampp\htdocs\encrypter\decrypt.php(18): sodium_crypto_pwhash() #1 {main} thrown in C:\xampp\htdocs\encrypter\decrypt.php on line 18
I tried to copy some lines in the encryption code but didn't work.
I also receive warning.
But I don't know if this is the cause. I also receive this on encryption.
Warning: unpack(): 64-bit format codes are not available for 32-bit versions of PHP in C:\xampp\htdocs\encrypter\decrypt.php on line 11
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
UPDATE
The warning is fixed by changing the pack() code from
P
toV
.Upon changing the code the
$opslimit
has a value more than 0.
DECRYPTION CODE
$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');
}
This is the source code I use from the libsodium example.