iOS Question Encryption / iEncryption

toro1950

Active Member
Licensed User
Hi, using the Encryption library with the following code, recovered from the forum, I can encrypt a string with a password and decrypt it
con B4A
B4X:
Sub EncryptAES(strDataToEncrypt As String, strKey As String) As String
     Dim SU As StringUtils
    Dim kg As KeyGenerator
    Dim C As Cipher
    Dim md As MessageDigest
    Dim encrypted() As Byte
    kg.Initialize("AES")
    kg.KeyFromBytes(md.GetMessageDigest(strKey.GetBytes("UTF8"), "MD5"))
        C.Initialize("AES/ECB/PKCS5Padding")
    encrypted = C.Encrypt(strDataToEncrypt.GetBytes("UTF8"), kg.Key, False)
    Return SU.EncodeBase64(encrypted)
End Sub


Sub DecryptAES(strDataToDecrypt As String, strKey As String) As String
     Dim SU As StringUtils
    Dim kg As KeyGenerator
    Dim C As Cipher
    Dim md As MessageDigest
    Dim Decrypted() As Byte
    kg.Initialize("AES")
    kg.KeyFromBytes(md.GetMessageDigest(strKey.GetBytes("UTF8"), "MD5"))
    C.Initialize("AES/ECB/PKCS5Padding")
     Dim Bytes() As Byte = SU.DecodeBase64(strDataToDecrypt)
    Decrypted = C.Decrypt(Bytes, kg.Key, False)
    Return BytesToString(Decrypted, 0, Decrypted.Length, "UTF8")
End Sub
to encrypt: Dim crypt As String= EncryptAES(testo, password)
to decrypt: Dim decrypt As String= DecryptAES(crypt,password)
thinking that the iEncryption library with B4i would behave in the same way, I entered the same code, but I get an error on the code
Dim kg As KeyGenerator, basically KeyGenerator is not supported iEncryption.
How to solve the problem? Can anyone help me?
 

toro1950

Active Member
Licensed User
Fixed, thanks anyway
B4X:
Sub Encrypt(dataToEncrypt As String,key As String) As String
      Dim cifro As Cipher
    Dim ByteArray() As Byte
    ByteArray=cifro.Encrypt(dataToEncrypt.GetBytes("UTF8"), password)
    Dim s As StringUtils
    Return s.EncodeBase64(ByteArray)

End Sub
Sub Decrypt(encryptedData As String,key As String) As String
       Dim cifro As Cipher

    Dim ByteArray() As Byte
    Dim result() As Byte
   
    Dim s As StringUtils
    result=s.DecodeBase64(encryptedData)
    ByteArray=cifro.Decrypt(result,password)
   
    Return BytesToString(ByteArray, 0, ByteArray.Length, "UTF8")

End Sub


Sub Button1_Click
    TextField1.Text = Encrypt(TextField2.Text.Trim, password)
    TextField2.Text=""
   
End Sub
Sub Button2_Click
    TextField2.Text = Decrypt(TextField1.Text.Trim, password)
End Sub
 
Upvote 0
Top