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.
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?
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"
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