Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add 2 encrypted strings together and decrypt them?

Tags:

c#

encryption

aes

I have 2 methods to generate 2 different data and need to save them in one encrypted file.

Can I achieve this by adding encrypted stringA + encrypted stringB, then decrypt them later?

Or

I have to encrpt stringA -> save as file -> read the file & decrypt to stringA -> stringA + stringB -> encrypt ?

NB, I am using Rijndael(AES) and someone suggested using customerized stream, will it work?

Any thoughts? Many thanks~

like image 757
Kelvin Avatar asked Feb 27 '23 22:02

Kelvin


2 Answers

If you're using ECB (electronic code book), then it should be possible (assuming they use the same encryption key), because each block is decrypted independent of other blocks. If you're using CBC (cipher block chaining) this will not work because each block is encrypted using data from the previous block. However, using ECB is way less secure than CBC.

like image 112
Graeme Perrow Avatar answered Mar 10 '23 10:03

Graeme Perrow


If you encrypt each separately, and know the length of each block of encrypted text, you can decrypt them later individually.

Something like this format would probably work:

Message1Length
Message1Content
Message2Content

Read the Message1Length number of bytes into the message and store it as encryptedMessage1 or something. Then read from that point to the End of File and store it as encryptedMessage2. Then decrypt them both individually.

like image 28
Crowe T. Robot Avatar answered Mar 10 '23 12:03

Crowe T. Robot