Sub Class_Globals
Private Root As B4XView
Private CI As Cipher
Private SU As StringUtils
End Sub
Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
Dim Message As String = "Hello B4i Cipher"
Dim Password As String = "Password"
Dim EncryptedText As String = Encrypt(Message.GetBytes("UTF8"), Password.GetBytes("UTF8"), True)
Log(EncryptedText)
Dim DecryptedText As String = Decrypt(SU.DecodeBase64(EncryptedText), Password.GetBytes("UTF8"), True)
Log(DecryptedText)
End Sub
Sub Encrypt(data() As Byte, passB() As Byte, EncodeToBase64 As Boolean) As Object
Dim MD As MessageDigest
Dim Key() As Byte = MD.GetMessageDigest(passB, "MD5")
Dim iOption As Int = Bit.Or(CI.OPTION_PKCS7Padding, CI.OPTION_ECBMode)
Dim datas() As Byte = CI.Encrypt2(data, Key, "AES", Null, iOption)
If EncodeToBase64 Then
Return SU.EncodeBase64(datas)
Else
Return datas
End If
End Sub
Sub Decrypt(data() As Byte, passB() As Byte, DecodeToString As Boolean) As Object
Dim MD As MessageDigest
Dim Key() As Byte = MD.GetMessageDigest(passB, "MD5")
Dim iOption As Int = Bit.Or(CI.OPTION_PKCS7Padding, CI.OPTION_ECBMode)
Dim datas() As Byte = CI.Decrypt2(data, Key, "AES", Null, iOption)
If DecodeToString Then
Return BytesToString(datas, 0, datas.Length, "UTF8")
Else
Return datas
End If
End Sub