B4J Library jB4XEncryption

This library allows you to encrypt or decrypt data using the AES encryption method.

It is simple to use and it is compatible with B4A B4XEncryption library and B4i Encryption library (Encrypt and Decrypt method) which means that you can encrypt the data on one platform and decrypt it on a different platform.

The B4J library depends on BouncyCastle. You need to download this jar and use #AdditionalJar:
https://www.b4x.com/android/forum/threads/share-encrypted-data-with-b4a.35482/#content
 

Attachments

  • jB4XEncryption.zip
    2.8 KB · Views: 2,141
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
It works great!

I have one question. In the next example, I was trying to compare the time between a non-encrypted process and an encrypted one. The non - encrypted takes 0ms to execute 1000 times, but the encrypted takes 5000+ ms to execute. I was wondering if the encryption / decryption was that slow, or did I do something wrong?

B4X:
#Region  Project Attributes

    #CommandLineArgs:
    #MergeLibraries: True
    #AdditionalJar: bcprov-jdk15on-150
#End Region

Sub Process_Globals
   
End Sub

Sub AppStart (Args() As String)
   
    'No encryption:
    Dim Start As Long = DateTime.Now
    For i = 1 To 1000
        Dim s As String = "mystring"
        Dim b() As Byte = s.GetBytes("UTF8")       
        Dim ss As String = BytesToString(b, 0, b.Length, "UTF8")       
    Next
    Log("no Encrypt/Decrypt time: " & (DateTime.Now - Start) & " ms.")

    'With encryption / decryption:
    Dim Cipher As B4XCipher   
    Dim Start As Long = DateTime.Now   
    For i = 1 To 1000
        'Encrypt
        Dim s As String = "mystring"
        Dim b() As Byte = Cipher.Encrypt(s.GetBytes("UTF8"), "pwd")           
        'Decrypt
        Dim bb() As Byte = Cipher.Decrypt(b, "pwd")
        Dim ss As String = BytesToString(bb, 0, bb.Length, "UTF8")       
    Next
    Log("Encrypt/Decrypt time: " & (DateTime.Now - Start) & " ms.")

End Sub

I need to encrypt and decrypt data in a database, and due to the large amount of data, I did this test first.

Thanks!
 

nw11

Member
Licensed User
Longtime User
hi Erel,

is there any way i can encrypt / decrypt with DES encryption method .. like i do with B4A and B4I

(all my communication packets are now encryped with DES encryption method and i'm trying to implement my ios / android App for PC / Mac too)


' ios
B4X:
data = c.Encrypt2(data, key, "DES", Null, c.OPTION_ECBMode)

bytes = c.Decrypt2(data, key, "DES", Null, c.OPTION_ECBMode)

' android
B4X:
Dim key(0) AsByte
Dim AppK AsString
AppK = "mypass"
key = AppK.GetBytes("ISO-8859-1") '"UTF8")
Dim data(0) AsByte
Dim bytes(0) AsByte
Dim Kg AsKeyGenerator
Dim c AsCipher
c.Initialize("DES/ECB/NoPadding")   
Kg.Initialize("DES")
Kg.KeyFromBytes(key)
key = Null
If Mode = 0 Then
Text = padString(Text)
data = Bconv.StringToBytes(Text, "ISO-8859-1")
data = c.Encrypt(data, Kg.key, False)
key = Null
ReturnBconv.HexFromBytes(data)
ElseIf Mode = 1Then
data = Bconv.HexToBytes(Text)
bytes = c.Decrypt(data, Kg.key, False)
key = Null
Dim AppStr AsString
AppStr = Bconv.StringFromBytes(bytes,"ISO-8859-1").Trim
bytes = Null
Return AppStr
End If

Thank you in advance

Fabrizio
 

nw11

Member
Licensed User
Longtime User
If the purpose is to share data with B4i and B4A apps then the simplest solution is to use this library (which uses AES).
It is compatible with B4A B4XEncryption library and with B4i Encryption (Encrypt / Decrypt methods).

thank you for your reply ..

as i can understand .. it isn't possible to crypt / decrypt with the DES method at moment in B4J.

the problem is that the android version of my app is online from about 1 year with about 2000 working users.

the packets arrives with the DES method and the databases are already crypted with DES.

Hope you can help me .. but if this is not possible i try to workaround the problem, crypting AES when an Android or iOS device send packet to a PC or MAC and leave DES method when iOS and Android send packs each other. you will understand that the thing would complicate a lot.

Please let me know.

thank you in advance.

F
 
Last edited:

nw11

Member
Licensed User
Longtime User
Thank you very much for pointing me in the right direction.

With the B4A Encryption Library works fine (even without bouncy castle jar).

In the near future i'll try to switch to B4XEcryption library. Thanks for your advice.

F
 

Swissmade

Well-Known Member
Licensed User
Longtime User
I wonder how many Bits this library uses.
128, 192 of 256 bits
 

Swissmade

Well-Known Member
Licensed User
Longtime User
Ok.
Thanks Erel
 

Nokia

Active Member
Licensed User
Longtime User
ok after you have the bytes encrypted.. can your write the bytes to a file or a text field?

what would you normally do after you have the data encrypted?

can you use a byte stream to encrypt files as well like images or word documents, pdf's?
 

Swissmade

Well-Known Member
Licensed User
Longtime User
Maybe in a loop.
Encrypt two or three times.
Always with an other key.

Just a idea
 

adjie

Member
Licensed User
Longtime User
I wan't to send the encrypted text from b4j to android, but the text is like this :
[{"token":"\u000e2�\u001cݳ\u001dau\u000b\u001b��^}J�ԸآG�6h�KV����\u001e\r����Ȍ"}]]
Am I doing wrong ?
note: I put the encrypted data to json
Is there any example how to send encrypted data from server to android ?
 
Last edited:

adjie

Member
Licensed User
Longtime User
I got it !!! I'll answer my own question ;)
The byte should first convert it to Hex. Then it will savely to send to other device and decrypt it.
Any other advice still accepted.

Another question, is this library compatible with other language like javascript ? because in my case, I'm sending the message to angularjs (java script framework) via socket. Currently the message is just a plain un-encrypted text. only token part is encrypted because it doesn't need to decrypt. I just need to encrypt the whole messages and decrypt it on angularjs. Any advice ?
 
Top