Encrypt/decrypt a .txt file

Dear all,
I would like to encrypt a .txt file and then including into the built App (in the Resources folder). For then be able to read (=decrypt) and use the information stored from within the App .
I tried to encrypt it using:

Var key As MemoryBlock = Crypto.GenerateRandomBytes(16)  
Var initVector As MemoryBlock = Crypto.GenerateRandomBytes(16) 
Var encrypted As MemoryBlock
encrypted = Crypto.AESEncrypt(key, theInputText, Crypto.BlockModes.CBC, initVector)

and then export this encrypted text into a new file. But when I ready this file in order to decrypt, using:
decrypted = Crypto.AESDecrypt(key, encrypted, Crypto.BlockModes.CBC, initVector)

I can recover the text but I loose the returns and tabs.

To read or write the file I use:

t = TextInputStream.Open(theFile)
t.Encoding = Encodings.UTF8

Any suggestion or working code (preferable using the new Crypto module in Xojo) I can use as a good starting point?
Thanks in advance.

L.

The encoding of your file is not UTF-8. Because it’s binary data, it actually has no encoding and you’d be better off using a BinaryStream to read and write it.

After you decrypt the data, you should set its encoding to UTF-8. Perhaps that’s the issue?

decrypted = Crypto.AESDecrypt( ... )
decrypted = decrypted.DefineEncoding( Encodings.UTF8 )

Thanks for the quick reply.
I will implement your suggestions and see if this solve the problem.

Using the binarystream it improves reading and writing text. But when I encrypt I still get tabs and returns not properly recognise.
As an example: if you take the code from the Xojo web blogs (Crypto Improvements – Xojo Programming Blog):

Var encrypted As MemoryBlock

Var dataToEncrypt As MemoryBlock = "Secret!"
Var key As MemoryBlock = Crypto.GenerateRandomBytes(16)
Var initVector As MemoryBlock = Crypto.GenerateRandomBytes(16)
encrypted = Crypto.AESEncrypt(key, dataToEncrypt, Crypto.BlockModes.CBC, initVector)

Var decrypted As MemoryBlock
decrypted = Crypto.AESDecrypt(key, encrypted, Crypto.BlockModes.CBC, initVector)
MessageBox(decrypted) // OK -> decrypted = "Secret!"

It works well only if you NOT include tabs or endofline.
But if you do:

Var dataToEncrypt As MemoryBlock = "Secret!" + endofline + "more secrets" 
//or 
Var dataToEncrypt As MemoryBlock = "Item1 " + Chr(9) + "Computer"

you will get: “Item1 � Computer”

Any idea how to overcome this?
Thanks !

If you want to use pure Xojo code, there’s a version of RC4 we created here: Faster Encyrption / Obscuration than RC4 for Large 24MB & 15Mb Files? - #14 by Mike_D

Thanks for the suggestion. But if you use as input a text with line breaks you get the same problem.

Code used is:

var cypheredText as string = rc4v6(TextAreaOriginal.text, TextFieldKey.text)
TextAreaCyphered.text = cypheredText

var DecypheredText as string = rc4v6(cypheredText, TextFieldKey.text)
// var DecypheredText as string = rc4v6(TextAreaCyphered.text, TextFieldKey.text) // THIS WILL NOT WORK
TextAreaDecyphered.text = DecypheredText

Try this:

TextAreaDecyphered.text = DefineEncoding(DecypheredText, Encodings.UTF8)

1 Like

Brilliant !!!
Thanks a lot Greg and to all !!!

Well it helps I expect if you decrypt to a string and then give the string an encoding.

Indeed. That was where I was stacking. Thanks !