Android Question Cipher / Decipher Long Text

adriano.freitas

Active Member
Hi!

I'm working on an application that sometimes needs to deal with long texts. What I need is for these long texts to be encrypted to avoid eavesdropping. I can't use SQLLite's encryption functions because I also have to send data via FTP and other means that need to be encrypted.

I searched here on the forum and saw the B4XEncryption Cipher and Decipher, however, the encryption result cannot be stored or sent directly because it contains special characters, so, after encrypting, I encode using BASE64. Everything is working perfectly, except for one detail: the delay. As they are long texts, the application freezes for a long time in this process (especially when encrypting)...

Does anyone have any suggestions for better performance?

Thank you very much!
 

Quandalle

Member
Licensed User
1) FTP transfer allows you to send binary files. It is not necessary to encode in base64

2) To encrypt a very long file, it is possible to slice it and encrypt each slice

For example encryption of a file by 4KB slice :

B4X:
Sub CryptFile (path As String, infile As String, outfile As String)
    dim buffersize as int = 1024*4
    dim password = "mypassword"
    Dim c As B4XCipher
    Dim Ist As InputStream
    Dim ost As OutputStream
    Dim count As Int = 0
    ost = File.OpenOutput(path,outfile,False)
    Ist = File.OpenInput(path,infile)
    Dim buffer(buffersize) As Byte
    count = Ist.ReadBytes(buffer, 0, buffer.length)
    Do While count <> -1
        Dim outbuff () As Byte
        outbuff = c.Encrypt(buffer,password)
        ost.WriteBytes(outbuff,0,outbuff.Length)
        count = Ist.ReadBytes(buffer, 0, buffer.length)
    Loop
    Ist.Close
    ost.close
End Sub
 
Last edited:
Upvote 0

adriano.freitas

Active Member
1) FTP transfer allows you to send binary files. It is not necessary to encode in base64

2) To encrypt a very long file, it is possible to slice it and encrypt each slice

For example encryption of a file by 4KB slice :

B4X:
Sub CryptFile (path As String, infile As String, outfile As String)
    dim buffersize as int = 1024*4
    dim password = "mypassword"
    Dim c As B4XCipher
    Dim Ist As InputStream
    Dim ost As OutputStream
    Dim count As Int = 0
    ost = File.OpenOutput(path,outfile,False)
    Ist = File.OpenInput(path,infile)
    Dim buffer(buffersize) As Byte
    Do While count <> -1
        count = Ist.ReadBytes(buffer, 0, buffer.length)
        Dim outbuff () As Byte
        outbuff = c.Encrypt(buffer,password)
        ost.WriteBytes(outbuff,0,outbuff.Length)
        Log(outbuff.Length)
    Loop
    Ist.Close
    ost.close
End Sub
Hi!
Thank you very much for your answer, however, that doesn't solve it, as I actually cited FTP as an example. I need the same encrypted text so that I can use it for different purposes, FTP, PHP, SQL, Webservices, etc... The ideal would be a single encryption, but one that was as fast as possible and generated a string as a result (or then use base64), but the big issue is performance, given that they are large texts...
 
Upvote 0

Quandalle

Member
Licensed User
B4xcipher uses the AES encryption algorithm. It is one of the fastest encryption solutions with encryption/decryption capacities of several tens of MB per second.

Also what do you mean by "long text" and "long time"? And is it the problem come from the encryption speed?

If the problem is the "UI Freeze", one of the solutions is for example as I said above to proceed by slice by inserting a sleep at each iteration.

But according to the usage you indicate "FTP, PHP, SQL, Webservices,", it seems to me more reasonable to let the communication layers do their encryption work (https, sftp...) rather than trying to put an encryption/decryption in the application level
 
Last edited:
Upvote 0
Top