I'm using the following code to encrypt and decrypt each files of my apps under B4A, B4I and B4J. It works without problems with each file and the speed is also very fast. The crypted file has the same size as the decrypted. The only problem I have is, that it is not possible to encrypt big files, because of running in an out of memory, I think. Under windows, I have tested it with files up to 200 MB, no problem. On my Galaxy S6, I get problems with files up to 30 MB. Is there a way to make the subs more efficient?
Thanks for help...
Thanks for help...
B4X:
Sub encryptFile(DirSource As String, fileSource As String, DirTarget As String, FileTarget As String, PW As String) As Boolean
Try
Dim in As InputStream
in = File.OpenInput(DirSource, fileSource)
Dim out As OutputStream
out.InitializeToBytesArray(0)
File.Copy2(in, out)
Dim data() As Byte
data = out.ToBytesArray
data=Cipher.Encrypt(data, PW)
Dim Dokumentdatei As RandomAccessFile
Dokumentdatei.Initialize(DirTarget, FileTarget, False)
Dokumentdatei.WriteB4XObject(data, 0)
Dokumentdatei.Close
Return True
Catch
Log(LastException.Message)
Return False
End Try
End Sub
'Verschlüsselte Datei mit B4XObject laden, entschlüsseln und als Byte-Array in ursprünglicher Form speichern'
Sub decryptFile(DirSource As String, FileSource As String, DirTarget As String, FileTarget As String, PW As String) As Boolean
Try
Dim data() As Byte
Dim Dokumentdatei As RandomAccessFile
Dokumentdatei.Initialize(DirSource, FileSource, False)
data=Dokumentdatei.ReadB4XObject(0)
Dokumentdatei.Close
data=Cipher.Decrypt(data, PW)
Dim in As InputStream
in.InitializeFromBytesArray(data, 0, data.Length)
Dim out As OutputStream
out=File.OpenOutput(DirTarget, FileTarget, False)
File.Copy2(in, out)
out.Close
Return True
Catch
Log(LastException.Message)
Return False
End Try
End Sub