jedisct1 / libsodium

A modern, portable, easy to use crypto library.
https://libsodium.org
Other
12.22k stars 1.74k forks source link

File encryption bug in my code #1046

Closed lprimeroo closed 3 years ago

lprimeroo commented 3 years ago

I'm working with the encryption and decryption samples as mentioned here: https://libsodium.gitbook.io/doc/secret-key_cryptography/secretstream#file-encryption-example-code

My code:

int main(int argc, char *argv[]) {

    if (sodium_init() != 0) {
        return 1;
    }
     if (encrypt("./encrypted", "./original", "key") != 0) {
         return 1;
     }

    if (decrypt("./decrypted", "./encrypted1", "key") != 0) {
        return 1;
    }
    return 0;
  return 0;
}

The example above works great. However, if I try to encrypt first (and comment out the decrypt) and then try to decrypt (and comment out the encrypt), it fails. I'm using the same key. Any idea as to why it doesn't work even when invoked with the same key? What I mean to ask is that, how can I split these 2 function calls across to separate invocations instead of them being in a single file? I think the example should reflect that.

Thanks.