我正在尝试将加密方法从VB转换为Kotlin,但是结果不一样。在vb中,它的实现方式如下:
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
这是我在Kotlin中的代码:
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)
}
我在做什么错了?