tags:

views:

32

answers:

2

Hello guys I am trying to migrate a java code to vb, now I need to duplicate the des encription but I am having trouble with this part.

I admit I havent dont encryption since college.

This encrypt the key using md5, and send it to a fuction 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 lenght 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(challenge + 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

PD: Sorry for my bad english.

A: 

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.

drventure
While I agree with your proposition, the java method returns a hex hash Dim md5Key As String = digestUtils.md5Hex(challenge + password)Thats why I am unable to use other hash method but thanks for your replay.
Enrique
A: 

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
Enrique