开发者

VB, DES and MD5

开发者 https://www.devze.com 2023-01-19 07:11 出处:网络
Hello guys I am trying to migrate a Java code to VB, now I need to duplicate the DES encryption but I am having trouble with this part.

Hello guys I am trying to migrate a Java code to VB, now I need to duplicate the DES encryption but I am having trouble with this part.

I admit I haven't done encryption since college.

This encrypt the key using MD5, and send it to a function for the DES encryption, seems I got a clue of the error, the key must be a 8 digit key and I am sending a 16 length key.

Dim MD5 As New MD5CryptoServiceProvider()
        Dim dataHash() As Byte = MD5.ComputeHash(Encoding.UTF8.GetBytes(challenge + password))
        Dim sb As New StringBuilder
        Dim b As Byte
        For Each b In dataHash
            sb.Append(b.ToString("x2").ToLower())
        Next
        Dim md5Key As String = sb.ToString
        ''Dim md5Key As String = digestUtils.md5Hex(challeng开发者_运维百科e + password)
        Dim geoEncrypt As New GeoEncriptamiento
        Dim challengeAnswer As String = geoEncrypt.EncryptFile(challenge, md5Key)

This is the code that does the encryption

Function EncryptFile(ByVal esquema As String, ByVal llave As String) As String

    Dim DES As New DESCryptoServiceProvider()

    'Establecer la clave secreta para el algoritmo DES.
    'Se necesita una clave de 64 bits y IV para este proveedor
    DES.Key = UTF8Encoding.UTF8.GetBytes(llave)
    DES.IV = UTF8Encoding.UTF8.GetBytes(llave)
    Try
        Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(esquema)
        Dim ms As New MemoryStream
        Dim cs As New CryptoStream(MS, DES.CreateEncryptor(DES.Key, DES.IV), CryptoStreamMode.Write)
        cs.Write(inputByteArray, 0, inputByteArray.Length)
        cs.FlushFinalBlock()
        Return Convert.ToBase64String(ms.ToArray())
    Catch ex As Exception
        Return "Error"
    End Try
End Function

The error is when I try to parse the MD5 to the DES.Key


I'd check your use of UTF8Encoding.UTF8.GetBytes(llave) That's likely converting the incoming 16 byte key into a format createencryptor isn't expecting.

createencryptor expects to see a key the same size as the BLOCKSIZE, which, according to the docs, is 64 BITS, or 8 bytes.

You're passing in a key of 16 bytes, because of this loop For Each b In dataHash sb.Append(b.ToString("x2").ToLower()) Next

Also note that the computehash function returns an array of 16 bytes, not 8 bytes "The ComputeHash methods of the MD5 class return the hash as an array of 16 bytes. Note that some MD5 implementations produce a 32-character, hexadecimal-formatted hash. To interoperate with such implementations, format the return value of the ComputeHash methods as a hexadecimal value."

Looks like you'll either need to use a different hash, or only use part of the 16 byte hash.


The solution was simple, first we need to cut the string to just 8 positions (The 8 bytes it can get), and lastly to make it compatible add the cyphermode. Here is the code

   Function EncryptFile(ByVal esquema As String, ByVal llave As String) As String
    Dim DES As New DESCryptoServiceProvider()
    DES.Mode = CipherMode.ECB
    Dim md5 As New MD5CryptoServiceProvider()
    DES.Key = UTF8Encoding.UTF8.GetBytes(llave.Substring(0, 8))
    DES.IV = UTF8Encoding.UTF8.GetBytes(llave.Substring(0, 8))
    Try
        Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(esquema)
        Dim ms As New MemoryStream
        Dim cs As New CryptoStream(ms, DES.CreateEncryptor(DES.Key, DES.IV), CryptoStreamMode.Write)
        cs.Write(inputByteArray, 0, inputByteArray.Length)
        cs.FlushFinalBlock()
        Return Convert.ToBase64String(ms.ToArray())
    Catch ex As Exception
        Return "Error"
    End Try
End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号