B4A Library [B4X] B4XEncryption

Status
Not open for further replies.
This library allows you to encrypt or decrypt data using the AES encryption method.

It is simple to use and it is compatible with B4J jB4XEncryption 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.

Usage example:
B4X:
Sub EncryptText(text As String, password As String) As Byte()
   Dim c As B4XCipher
   Return c.Encrypt(text.GetBytes("utf8"), password)
End Sub

Sub DecryptText(EncryptedData() As Byte, password As String) As String
   Dim c As B4XCipher
   Dim b() As Byte = c.Decrypt(EncryptedData, password)
   Return BytesToString(b, 0, b.Length, "utf8")
End Sub

Dim encryptedData() As Byte = EncryptText("confidential", "123456")
Log(DecryptText(encryptedData, "123456"))

B4XPages example project is attached.
 

Attachments

  • Project.zip
    14 KB · Views: 757
Last edited:

xeriko

Member
Licensed User
Longtime User
Hi,

can we use this with php?

i tried AES encription in php but can't get result
 

MarcoRome

Expert
Licensed User
Longtime User
Hello,

I don't understand why we don't have a full exemple to how use this library ?
The way to look B4XEncryption in the search is not very good cause i don't find a real full exemple.
Regards

Edit : maybe it's this ? https://www.b4x.com/android/forum/t...or-b4j-b4a-and-b4i-also-in-url.58773/#content


B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim cifro As B4XCipher
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim test() As Byte
    Dim string_crypt As String
   
    test = Encrypt("make test with this sentence 123 e %&/")
    string_crypt = BytesToString(test, 0, test.Length, "UTF8")
    Log(string_crypt)
   
    Dim string_encrypt As String
    string_encrypt = Decrypt(test)
    Log(string_encrypt)
   
   
End Sub

Sub Encrypt(testo As String) As Byte()
    Dim ByteArray() As Byte
    ByteArray=cifro.Encrypt(testo.GetBytes("UTF8"), "xxxxxx")
    Return ByteArray
End Sub



Sub Decrypt(testo() As Byte) As String
    Dim ByteArray() As Byte
    ByteArray=cifro.Decrypt(testo, "xxxxxx")
    Return BytesToString(ByteArray, 0, ByteArray.Length, "UTF8")   
End Sub
 

lomosami

Member
Licensed User
Longtime User
Is it possible to use this library to encrypt/decrypt over internet between 2 android phone? The question is if the encription is strong to consider safe the data. I think it is like wpa2 with simmetric encryption AES.
 

Celso

Member
Licensed User
Longtime User
But how to use in fact, some little example pls?
 

Celso

Member
Licensed User
Longtime User
Can be Text, just to understand the syntax!

Thanks a lot.
 

Celso

Member
Licensed User
Longtime User
Thanks Erel
 

Bel

Member
Licensed User
Hello
I need to encrypt data with aes in php and decrypt it in b4a
In b4a the encrypt and decrypt working but between php and b4a not working
 

Valeriy Lakhtin

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 B4J jB4XEncryption 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.

Usage example:
B4X:
Sub EncryptText(text As String, password As String) As Byte()
   Dim c As B4XCipher
   Return c.Encrypt(text.GetBytes("utf8"), password)
End Sub

Sub DecryptText(EncryptedData() As Byte, password As String) As String
   Dim c As B4XCipher
   Dim b() As Byte = c.Decrypt(EncryptedData, password)
   Return BytesToString(b, 0, b.Length, "utf8")
End Sub

Dim encryptedData() As Byte = EncryptText("confidential", "123456")
Log(DecryptText(encryptedData, "123456"))

Hi Erel!. I tride use this example, but does not work gives an error

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 2373)
java.lang.NullPointerException: Attempt to read from field 'anywheresoftware.b4a.BA$SharedProcessBA anywheresoftware.b4a.BA.sharedProcessBA' on a null object reference
at anywheresoftware.b4a.B4AClass$ImplB4AClass.getActivityBA(B4AClass.java:20)
at adr.stringfunctions.stringfunctions._vvvvvv3(stringfunctions.java:809)
at Click.Click.main._activity_create(main.java:2373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at Click.Click.main.afterFirstLayout(main.java:102)
at Click.Click.main.access$000(main.java:17)
at Click.Click.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
 

zani

Member
Licensed User
Longtime 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 B4J jB4XEncryption 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.

Usage example:
B4X:
Sub EncryptText(text As String, password As String) As Byte()
   Dim c As B4XCipher
   Return c.Encrypt(text.GetBytes("utf8"), password)
End Sub

Sub DecryptText(EncryptedData() As Byte, password As String) As String
   Dim c As B4XCipher
   Dim b() As Byte = c.Decrypt(EncryptedData, password)
   Return BytesToString(b, 0, b.Length, "utf8")
End Sub

Dim encryptedData() As Byte = EncryptText("confidential", "123456")
Log(DecryptText(encryptedData, "123456"))
hi can i

use this for file Encryption?
 

billyn_hk

Member
Licensed User
Longtime User
Hi Erel.

Could you advise how to use this B4xEncryption Library to decrypt the encrypted string data from PHP side ?
I cannot go pass the decryptByteToStr( PHP_issued_Byte ), the app halts at this point.

the PHP side, I use this
http://php.net/manual/en/function.mcrypt-encrypt.php
with the Key value changed to same as b4xCipher password

I need a solution handling encrypted data between App and PHP server.
If the above method is not correct, please highlight me where to go for.

thanks
kindest regards
 

Mikonios

Active Member
Licensed User
Longtime User
So that :::

B4X:
Dim Su As StringUtils
    Dim Crypt() As Byte         = EncryptText("confidential", "123456")
    Dim TxtEnCrypt As String    = Su.EncodeBase64(Crypt)              ' To Write/Put
       
    Dim BtCrypt() As Byte       = Su.DecodeBase64(TxtEnCrypt)           
    Dim TxtDeCrypt As String    = DecryptText(BtCrypt, "123456")      ' To Read/Get

    Log(TxtDeCrypt)
 
Status
Not open for further replies.
Top