Dim output As RandomAccessFile
Dim text As String
text = File.ReadString(File.DirRootExternal, "normal.txt")
output.Initialize(File.DirRootExternal, "secret.dat", False)
output.WriteEncryptedObject(text, "some secret password", output.CurrentPosition)
output.Close
Then you should first read the file to an array of bytes. You can use a temporary memory stream by using output2.InitializeToBytesArray. You will then be able to read the file with File.Copy2.
The last step is to write the array to 'output'.
Thank you Erel. Now I can encrypt/decrypt a jpg file but the speed is really slow. It takes about 7 seconds to decrypt a 130KB file on dual-core CPU device. My code below:
B4X:
Dim input As RandomAccessFile
Dim Buffer() As Byte
input.Initialize(File.DirRootExternal, "encrypted.dat", True)
Buffer = input.ReadEncryptedObject("password", input.CurrentPosition)
Dim InputStream1 As InputStream
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
InputStream1.Close
input.Close
Dim bm As Bitmap
bm.Initialize2(InputStream1)
Activity.SetBackgroundImage(bm)
Thank you Erel. How about this idea:
Add 10 random bytes into the beginning of jpg file. When loading, just read the content after first 10 bytes. My purpose is make it a bit harder to read these jpg files. Is it possible?
I tried to write 56 bytes into jpeg file then save as a new file => It's OK.
But I got problem when read it again. Please take a look at attached file. Thank you Erel.