littlekellilee
littlekellilee

Reputation: 133

Decrypting with libsodium with a 64-bit nonce

I have a site set up which takes data from a form and encrypts it using libsodium for php. Due to something that happened on the server, somehow the CRYPTO_BOX_NONCEBYTES variable returned 64 instead of 24. We now have several files that are encrypted with libsodium using a nonce size of 64, and we also have the relating nonce files. Is there any way to decrypt this?

In other words, I have a file that has been encrypted with a 64-bit nonce. When I try to decrypt it with that same 64-bit nonce, it fails with the following error:

PHP Fatal error:  crypto_box_open(): nonce size should be CRYPTO_BOX_NONCEBYTES long in /data/www/docroot/filename.php on line 42

Upvotes: 0

Views: 691

Answers (1)

Scott Arciszewski
Scott Arciszewski

Reputation: 34103

Sure, given a valid ciphertext, keypair, and your 64-byte nonce, you should be able to do this:

// Keep increasing this from 0 to 39 to see if you can grab the 
// correct slice of the nonce string:
$try = 0;

// Then run the rest of the code and hope you didn't get a fatal error.
$substr = mb_substr($your_64byte_nonce, $try, $try + \Sodium\CRYPTO_BOX_NONCEBYTES, '8bit');
$decrypted = \Sodium\crypto_box_open($message, $substr, $keypair);
if ($decrypted !== false) {
    echo $decrypted, "\n";
    echo "The magic slicing point is {$try}.\n";
} else {
    exit(1);
}

If it doesn't work, there may be other things amiss.

Upvotes: 1

Related Questions