Convert TripleDESCryptoServiceProvider to javax.crypto.Cipher

1

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?

    
asked by anonymous 20.06.2018 / 21:02

1 answer

0

In the Encrypt() method of VB.NET, you used TransformFinalBlock() , so it's going wrong.

Do this: implement a Transform() method,

Private Shared Function Transform(ByVal input() As Byte, _
    ByVal CryptoTransform As ICryptoTransform) As Byte()

    Dim memStream As MemoryStream = New MemoryStream
    Dim cryptStream As CryptoStream = New _
        CryptoStream(memStream, CryptoTransform, _
        CryptoStreamMode.Write)

    cryptStream.Write(input, 0, input.Length)
    cryptStream.FlushFinalBlock()

    memStream.Position = 0
    Dim result(CType(memStream.Length - 1, System.Int32)) As Byte
    memStream.Read(result, 0, CType(result.Length, System.Int32))

    memStream.Close()
    cryptStream.Close()

    Return result
End Function

and change your Encrypt() to

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(Transform(Buffer, TripleDES.CreateEncryptor())
    Catch ex As Exception
        ' loga o erro
    End Try
    Return FvaRetorno
End Function

Symmetric cryptographic algorithm providers in .NET need a CryptoStream to do the service.

    
20.06.2018 / 22:43