0

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
littlekellilee
  • 123
  • 1
  • 5

1 Answers1

1

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.

Scott Arciszewski
  • 33,610
  • 16
  • 89
  • 206
  • So do you mean that some 24 bit substring of my 64 bit nonce will decrypt my message??? – littlekellilee Oct 01 '15 at 19:59
  • 1
    Is it truly a 64-bit nonce or a 64-byte nonce? Because if you somehow got away with a 64-bit nonce, I don't know what to tell you. I would say: give it a try, if not, you've lost data. – Scott Arciszewski Oct 01 '15 at 20:57
  • Thanks! You're a huge help, I'll look a this tonight. And you're definitely right on the byte haha I clearly haven't had enough Red Bull. – littlekellilee Oct 01 '15 at 23:25