I need to convert a VB encryption method to Kotlin, however the results are giving different. In vb it is implemented as follows:
Public Shared Function MD5Hash(ByVal value As String) As Byte()
Dim byteArray() As Byte = ASCIIEncoding.ASCII.GetBytes(value)
Return MD5.ComputeHash(byteArray)
End Function
Public Shared Function Encrypt(ByVal stringToEncrypt As String) As String
Dim FvaRetorno As String = ""
Try
TripleDES.Key = MD5Hash(key)
TripleDES.Mode = CipherMode.ECB
Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(stringToEncrypt)
FvaRetorno = Convert.ToBase64String(TripleDES.CreateEncryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
Catch ex As Exception
End Try
Return FvaRetorno
End Function
Now what I'm trying to implement in kotlin is this:
var ALGO = "DESede/ECB/PKCS7Padding"
fun getSecreteKey(secretKey: String): SecretKey {
val md = MessageDigest.getInstance("MD5")
val digestOfPassword = md.digest(secretKey.toByteArray(charset("ascii")))
val keyBytes = Arrays.copyOf(digestOfPassword, 24)
return SecretKeySpec(keyBytes, "DESede")
}
fun encrypt(message: String, secretKey: String): String {
val cipher = Cipher.getInstance(ALGO)
cipher.init(Cipher.ENCRYPT_MODE, getSecreteKey(secretKey))
val plainTextBytes = message.toByteArray(charset("ascii"))
val buf = cipher.doFinal(plainTextBytes)
val base64Bytes = Base64.encode(buf, Base64.DEFAULT)
return String(base64Bytes)
}
Can anyone explain what I'm doing wrong?