Thats an edited title

thats an edite content

Read docs for the io::Read::read method carefully. It indicates end of file by returning Ok(0) and can easily read less bytes than in the provided buffer, which will be incorrectly interpreted by your code as EoF.

For simplicity, start with reading the whole file into memory, split it into chunks using chunks, encrypt chunk, and write result to file. Decrypt file similarly. If you encounter any problems, compare chunks before/after.

Note that chunk size during decryption is bigger since it also contains MAC tag. In the code above you read 516 bytes from file, encrypt it, get 532 byte ciphertext, and write it to disk. In the decrypt function you read 516 bytes and then try to decrypt it. In other words, you truncated MAC tag.

BUFFER_LENGTH in the encrypt_file function is 516 bytes, not 500. During encryption you need read 500 byte chunk, encrypt it, and write resulting 516 bytes into output file. During decryption you need to read 516 byte chunk, decrypt it and write resulting 500 bytes into output file.

Encryption adds a MAC tag (16 bytes in your case) to each chunk as illustrated by the aead::stream docs (note the 𝜏 part).

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.