B4J Library jB4XEncryption

Discussion in 'B4J Libraries & Classes' started by Erel, Dec 14, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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.

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

    Attached Files:

    MarkusR, pesquera, swissmade and 5 others like this.
  2. jmon

    jmon Well-Known Member Licensed 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?

    Code:
    #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!
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Are you testing it in Release mode?

    For an encryption method to be secure it must be a "heavy" computational task. 2ms per item sounds reasonable.
     
  4. jmon

    jmon Well-Known Member Licensed User

    Yes I am.

    Thanks for your answer, as you said, 2ms is reasonable ;-)
     
  5. nw11

    nw11 Member Licensed 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
    Code:
    data = c.Encrypt2(data, key, "DES"Null, c.OPTION_ECBMode)

    bytes = c.Decrypt2(data, key, 
    "DES"Null, c.OPTION_ECBMode)
    ' android
    Code:
    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 = 1
    Then
    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
     
  6. Erel

    Erel Administrator Staff Member Licensed 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).
     
  7. nw11

    nw11 Member Licensed User

    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: Jan 20, 2015
  8. Erel

    Erel Administrator Staff Member Licensed User

    You can use the Encryption library in B4J. You might need to add the bouncy castle jar.

    However in the long run I do recommend you to switch to B4XEncryption. It will "just work" on all platforms. Maybe you can configure your server to support both methods.
     
  9. nw11

    nw11 Member Licensed 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
     
  10. swissmade

    swissmade Active Member Licensed User

    I wonder how many Bits this library uses.
    128, 192 of 256 bits
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    128 bit with a random salt and a random IV.
     
    swissmade likes this.
  12. swissmade

    swissmade Active Member Licensed User

    Ok.
    Thanks Erel
     
  13. Nokia

    Nokia Active Member Licensed 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?
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Write to a file:
    Code:
    Dim out As OutputStream = File.OpenOutput(...)
    out.WriteBytes(EncrypedData, 
    0, EncryptedData.Length)
    out.Close
    Read the file:
    Code:
    Dim ec() As Byte = Bit.InputStreamToBytes(File.OpenInput(...))
    You can encrypt any data you like. It might be too slow for large files.
     
  15. narek adonts

    narek adonts Well-Known Member Licensed User

    is there a way to increase the Bits to 256 or 512 ?
     
  16. swissmade

    swissmade Active Member Licensed User

    Maybe in a loop.
    Encrypt two or three times.
    Always with an other key.

    Just a idea
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    Currently no. However AES 256 is not considered safer than AES 128.
     
  18. adjie

    adjie Member Licensed 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: Sep 23, 2016
  19. adjie

    adjie Member Licensed 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 ?
     
  20. Shalu Samtani

    Shalu Samtani Member Licensed User

    Can I encrypt from b4j then decrypt with b4a
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice