Having a decryption issue with the age crate

Hello all. I am building a tauri password manager application in order to learn rust. I know there are probably libs/tools out there to make some of this stuff easier, but again I am using this as a learning experience.

I am in the process of building a sync feature. The feature is merely sending a byte stream to a server (built using axum) and downloading it as a byte stream back to the tauri app. The issue I am coming across is when I am processing the down byte stream. When I try to decrypt the data I get an InvalidHeader error. I am definitely a novice when it comes to cryptography and to the age crate. The docs aren't really clear as to why that error arises. In case it helps I am gonna go into specifics about implementation with some links to actual code. First I will show how data is moved to / from the server. I have read that encryption can be hindered by handling encoding incorrectly, maybe someone can see if I messed something up there. After that I will go into how data is getting encrypted / decrypted. Here we go...

I have two tauri commands (basically just functions) called push_s and pull.

The push_s function gets a handle on a struct called LedgerDump that implements Read and sends those bytes to the server. You can find the LedgerDump impl here. The Read implementation grabs files in a directory and separates them with \n. This function takes the Read impl, shoves it into a tokio stream and sends it off to the server. Here is the handler for the route on the server. It stores the received bytes in a HashMap to be pulled down at a later time.

The pull function makes a GET request to the server and maps the byte stream response to a custom Stream impl found here. The route handler on the server can be found here. What this Stream impl does is gathers bytes until a newline \n byte is detected and then attempts to deserialize the bytes into a serde_json value. That stream is then passed to a merge method. This now brings us to how I am encrypting/decrypting and storing data.

The merge method is associated with a struct called LocalLedger. LocalLedger is responsible for encrypting and decrypting data along with providing a way to organize where data is stored / accessed. You can think of it as a table. Where I get the InvalidHeader error is at this call.

The error is caught and logged here, which is in a function called decrypt_load_doc. Here you can discern how encryption/decryption is done. TLDR is that a user provides a passphrase that is used as a key to encrypt/decrypt the data. I am pushing/pulling from the same client so I know the passphrase is correct. My hunch is that maybe the deserialize action that happens in the stream is causing my issue? I am not sure.

If anyone has any ideas/advice on a direction I take it would be greatly appreciated. I know there are probably gaps in the above explanation. I will be available to respond to any clarifying questions. Thank you in advance!

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.