views:

131

answers:

3

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~

+2  A: 

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.

Crowe T. Robot
+2  A: 

It depends on which encryption algorithm you use. For block ciphers in ECB mode, it would work - but ECB mode is considered very insecure exactly because of this (it makes certain kinds of attack on the cipher very easy).

Michael Borgwardt
Hey Michael - I'll upvote your answer if you upvote mine. :-)
Graeme Perrow
Conspirators!:)
Crowe T. Robot
+3  A: 

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.

Graeme Perrow