Android Question Encryption Assistance

Derek Jee

Active Member
Licensed User
Hello there

I am having trouble with encryption. My VB code is below which encrypts some text but I need B4A to encrypt it.. I copied some code from another thread but of course I cannot get it to work as I don't know what needs changing.. can anyone help me please?



B4X:
Private key() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
    Private iv() As Byte = {11, 11, 11, 11, 11, 111, 111, 111} 'Not the real iv

    Public Function Encrypt(ByVal plainText As String) As Byte()
        Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
        Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)
        Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
        Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, Me.iv)
        Dim encryptedStream As MemoryStream = New MemoryStream()
        Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write)
        cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
        cryptStream.FlushFinalBlock()
        encryptedStream.Position = 0
        Dim result(encryptedStream.Length - 1) As Byte
        encryptedStream.Read(result, 0, encryptedStream.Length)
        cryptStream.Close()
        Return result
    End Function
and I have this:
B4X:
Dim kg As KeyGenerator
   Dim c As Cipher
   Dim B64 As Base64
   Dim bconv As ByteConverter

   Dim data(0) As Byte
   Dim iv(0) As Byte
   iv = Array As Byte(11, 11, 11, 11, 11, 111, 111, 111) ' 16 bytes for AES
    
   c.Initialize("DESEDE/CBC/PKCS5Padding")  
   c.InitialisationVector = iv
   kg.Initialize("DESEDE")
   kg.KeyFromBytes(bconv.StringToBytes("123456789012345678901234","UTF8"))
   data = bconv.StringToBytes(dataToEncrypt, "UTF8")    
   data = c.Encrypt(data, kg.Key, True)          
   Return B64.EncodeBtoS(data, 0, data.Length)
Thank you, as always..

Derek.
 

Derek Jee

Active Member
Licensed User
Thank you Erel.. Worked a treat!!
 
Top