Android Question Read a big file an encrypt this using RandomAccessFile(RAF)

Hi Guys
i want to make a file encrypter ...
normally for larg files i get OutOfMemory error ...

how i can use RAF for this work ?


My test Read file and Encryptions functions:
Sub ReadFile(Dir As String, flname As String) As Byte()
    Dim out As OutputStream
    out.InitializeToBytesArray(100) 'size not really important
    File.Copy2(File.OpenInput(Dir, flname), out)
    Return out.ToBytesArray
End Sub

Sub AES_Encrypt(input() As Byte, IV As String, pass As String)
    
    Dim passB() As Byte = pass.GetBytes("UTF8")
    Dim IVb() As Byte = IV.GetBytes("UTF8")
    Dim  su As StringUtils
    Dim kg As KeyGenerator
    Dim C As Cipher
    kg.Initialize("AES")
    kg.KeyFromBytes(passB)
    C.Initialize("AES/CBC/PKCS5Padding")
    C.InitialisationVector = IVb
    Dim datas() As Byte = C.Encrypt(input, kg.Key, True) ' OutPut Is a Encrypted Aes Byte...

    File.WriteBytes(File.DirRootExternal,"encrypted.jpg",datas)' Save Encrypted File
End Sub

Sub GenerateIV As String
    Dim PWC As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
    Dim IV As String
    For i=0 To 15
        IV=IV & PWC.CharAt(Rnd(0,PWC.Length))
    Next
    Return IV
End Sub

'I Use This :
Dim Pass As String = "12345678901234567890123456789012" ' 32
Dim iv As String = GenerateIV
AES_Encrypt(ReadFile(File.DirRootExternal,"tt.jpg"),iv,Pass)


how i can use this codes by "RAF" for solve my 'OutOfMemory' Problem ?!

Thank All
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
 
Upvote 0
thank you man?
you solved my problem with this great lib?
 
Upvote 0
Top