Convert Tripledescryptoserviceprovider to javax.crypto.Cipher

Asked

Viewed 35 times

1

I’m needing to convert a VB encryption method to Kotlin, however, the results are giving different. In Vb 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

Already what I’m trying to implement in Kotlin is the following:

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 someone explain to me what I’m doing wrong?

1 answer

0

In the method Encrypt() from VB.NET, you used TransformFinalBlock(), that’s why it’s going wrong.

Do it like this: implement a method Transform(),

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() for

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 on . NET need a Cryptostream to do the job.

  • My problem is that I am wanting to convert the VB code to Java, because the VB code is legacy and I need the result to be equal...

  • 1

    Spoil the Java code then, because this VB is shit.

  • Right, how would I "break" that code?

  • Decompiling the class Cipher of your Java code and re-implementing with the erroneous behavior of your VB code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.