Tool B4XEncryption .net DLL class library

Hi All,

I recently had the need to send encrypted data from my B4A/B4J/B4i apps to a .net web service.
Because the B4XEncryption library works in all the B4X environments, I wanted a way to decrypt and encrypt data in the same way as the B4XEncryption libraries do.

Details of the B4XEncryption library can be found here:

https://www.b4x.com/android/forum/threads/b4xencryption.48177/


With a little help from Erel I build a .net DLL for this purpose.
A Visual Studio 2017 project with the DLL code and a test console application can be found on github at the following URL:

https://github.com/chrisleeuk/B4XEncryption.net

If you want to use the DLL without compiling it first, just copy the B4XEncryption.dll and BouncyCastle.Crypto.dll to the bin folder of your Visual Studio project and add them as references.

Typically if you are sending encrypted data, you will want some way of encoding it for transmission. In my case I'm sending it to a rest .net web service. In this case I'm encoding it in base 64 before sending it to my service.

The great part about this is that you can drop your base64 encoded string into a JSON object and send it to the .net service.

I don't have time to provide an example of that right now, but the DLL will hopefully save some time.
 

dcoun

Member
Licensed User
Longtime User
I need such a dll for a delphi application. But I can not use a .NET dll. Can this be a normal C dll ? Thank you in advance
 

Chris Lee

Member
Licensed User
Hi dcoun,

Unfortunately I don't have the spare time to create a native C library. Keep in mind that the DLL has a dependency on bouncy castle encryption, which supports java and c# but maybe not C based on a cursory check at:

https://www.bouncycastle.org

I basically ported the java source that Erel provided, which was discussed in the following post. The full source is also on GitHub.

https://www.b4x.com/android/forum/threads/cross-platform-encryption-with-net.89943/

You might try adding a COM layer to the DLL to make it digestible by Delphi.

Someone suggests this might help...

http://dotnetruntimelibraryfordelphi.sourceforge.net/

Sorry that I can't offer more but b4x is a hobby on top of my day job and family commitments so my time is limited!
 

OliverA

Expert
Licensed User
Longtime User

dcoun

Member
Licensed User
Longtime User
Hi dcoun,

Unfortunately I don't have the spare time to create a native C library. Keep in mind that the DLL has a dependency on bouncy castle encryption, which supports java and c# but maybe not C based on a cursory check at:

https://www.bouncycastle.org

I basically ported the java source that Erel provided, which was discussed in the following post. The full source is also on GitHub.

https://www.b4x.com/android/forum/threads/cross-platform-encryption-with-net.89943/

You might try adding a COM layer to the DLL to make it digestible by Delphi.

Someone suggests this might help...

http://dotnetruntimelibraryfordelphi.sourceforge.net/

Sorry that I can't offer more but b4x is a hobby on top of my day job and family commitments so my time is limited!
Thank you for your response. Your code really helped me. I arrived to a solution without DLL and I posted the code in the link OliverA showed above.
I forgot to remove my question here.
 

Unobtainius

Active Member
Licensed User
Longtime User
Thank you profoundly for your work Chris and Erel
I have changed Erel's example somewhat as I send and receive base64 between my app and my website
There's probably an easier way accomplish this but this is how I did it
B4X:
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button1_Click
    EditText2.Text = MyEncryptText(EditText1.Text, ForestGumpsPassword)
    Log("Encypted: " & EditText2.text)
    Log("Decypted: " & myDecryptText(EditText2.text, ForestGumpsPassword))
    EditText3.Text = ""
    ' write result to a file to see if windows can decypt it at the other end
    File.WriteString(rp.GetSafeDirDefaultExternal(""), $"${EditText1.Text}.txt"$, EditText2.text)
End Sub

Sub Button2_Click
    EditText3.Text = myDecryptText(EditText2.text, ForestGumpsPassword)
    Log(EditText3.Text)
End Sub

Sub MyEncryptText(text As String, password As String) As String
    Dim su As StringUtils
    Dim c As B4XCipher
    Return su.EncodeBase64(c.Encrypt(text.GetBytes("utf8"), password))
  
    ' exploded version
    ' A - Turn the original text into a array of bytes
    ' B - Encrypt that array into another array of btyes
    ' C - Convert the encrypted array of bytes into a base64 string and return it
'    Dim OriginalTextToByteArray() As Byte = text.GetBytes("utf8")
'    Dim ReturnedEncryptedByteArray() As Byte = c.Encrypt(OriginalTextToByteArray, password)
'    Return su.EncodeBase64(ReturnedEncryptedByteArray)
End Sub

Sub myDecryptText(text As String, Password As String) As String
    Dim su As StringUtils
    Dim c As B4XCipher
    Dim b() As Byte = c.Decrypt(su.DecodeBase64(text), Password)
    Return BytesToString(b, 0, b.Length, "utf8")
  
    ' exploded version
    ' A - Decode the text from base64 into a byte array
    ' B - decrypt that byte array
    ' C - convert the byte array into a string and return it
'    Dim OriginalEncryptedStringAsByteArray() As Byte = su.DecodeBase64(text)
'    Dim OriginalDecryptedStringAsBtyeArray() As Byte = c.Decrypt(OriginalEncryptedStringAsByteArray, Password)
'    Return BytesToString(OriginalDecryptedStringAsBtyeArray,0,OriginalDecryptedStringAsBtyeArray.Length, "utf8")
End Sub
Forgive the file saving line in there, it was so I could copy the string into my windows app to see if it could be decrypted at that end

At the vb.net end (web site) I use this
B4X:
Private Sub btnDecrypt_Click(sender As Object, e As EventArgs) Handles btnDecrypt.Click
        lblDecryptedResult.Text = Encoding.UTF8.GetString(
                                  B4XEncryption.B4XCipher.Decrypt(
                                  Convert.FromBase64String(txtBase64String.Text),
                                  txtPassword.Text))
    End Sub

    Private Sub btnEncrypt_Click(sender As Object, e As EventArgs) Handles btnEncrypt.Click
        Dim Bytes As Byte() = B4XEncryption.B4XCipher.Encrypt(Encoding.UTF8.GetBytes(txtSource.Text), txtPassword.Text)
        txtEncryptedBtyes.Text = Encoding.UTF8.GetString(Bytes)
        txtBase64String.Text = Convert.ToBase64String(Bytes)
        lblDecryptedResult.Text = ""
    End Sub

Might be helpful to someone that was struggling with this as much as I was
Once again thanks Chris and Erel I really appreciate everything you do.
 
Top