Android Question i want crypt string to same result in b4a and b4i

shugeyi

Member
Licensed User
Longtime User
i use crypt and Decrypt function below in b4i,but in b4a i can't find same function, can somebody help me? thans very much!

B4X:
Sub Encrypt(dataToEncrypt As String,key As String) As String
    'Return dataToEncrypt

    Dim c As Cipher
    Dim bconv As ByteConverter
    Dim opzioni As Int
    Dim data(0) As Byte
    Dim data2(0) As Byte
    Dim iv(0) As Byte
    iv = Array As Byte(211, 22, 233, 24, 55, 166, 7, 88)
    Dim kgBytes() As Byte
    Dim output As String

    Dim pw() As Byte
    pw = bconv.StringToBytes(key, "UTF8")
     kgBytes = c.GenerateKey(pw, "SHA-1", iv, 1)

    'kgBytes = key.GetBytes("UTF8")

    data = bconv.StringToBytes(dataToEncrypt, "UTF8") 
    opzioni = Bit.Or(c.OPTION_ECBMode, c.OPTION_PKCS7Padding)
    data2 = c.Encrypt2(data, kgBytes, "AES", iv, opzioni)  ' DESEDE/CBC/PKCS5Padding

    Dim s As StringUtils
    output = s.EncodeBase64(data2)
    Return output
End Sub


Sub Decrypt(encryptedData As String,key As String) As String
    Try
      
  
       Dim c As Cipher
    Dim bconv As ByteConverter
    Dim opzioni As Int
    Dim data(0) As Byte
    Dim data2(0) As Byte
    Dim iv(0) As Byte
    iv = Array As Byte(211, 22, 233, 24, 55, 166, 7, 88) 'not the original iV
    Dim kgBytes() As Byte
    Dim pw() As Byte
    'Dim p As String
    Dim output As String
    'pw = bconv.StringToBytes(key, "UTF8")
    pw = bconv.StringToBytes(key, "UTF8")
    Dim md As MessageDigest
    'pw=md.GetMessageDigest(pw,"MD5")
    kgBytes = c.GenerateKey(pw,"SHA-1", iv, 1)

    'kgBytes = md.GetMessageDigest(pw,"MD5")
       Dim s As StringUtils
    data=s.DecodeBase64(encryptedData)
    'data = bconv.StringToBytes(data, "UTF8")
    opzioni = Bit.Or(c.OPTION_ECBMode, c.OPTION_PKCS7Padding)
    data2 = c.Decrypt2(data, kgBytes,"AES", iv,opzioni)
    output = bconv.StringFromBytes(data2, "UTF8")
    Return output.Replace(Chr(0),"")
  
    Catch
        Log(LastException)
      
    End Try
  
    Return ""
End Sub
 
Last edited:
Top