Question

I'm trying to decrypt an encrypted string in a simple program I created in VB.NET but it seems like the Decryption Part isn't working properly.

Here's my code:

Imports System.Security.Cryptography

 Module Module1
 Dim data As String = "1234567887654321"
 Dim key As String = "1111222233334444"
 Dim output As String
 Dim bData() As Byte
 Dim bKey() As Byte
 Dim bEncrypted(7) As Byte
 Dim bDecrypted(7) As Byte
 Dim nullIV(7) As Byte
 Dim desprovider As New DESCryptoServiceProvider()

 Sub Main()
    bData = HexStringToBytes(data)
    bKey = HexStringToBytes(key)
    Console.WriteLine("Data: " + data)
    Console.WriteLine("Key: " + key)
    Encrypt()
    Console.WriteLine("Encryption Result :" + GetHexString(bEncrypted))
    Decrypt()
    Console.WriteLine("Decryption Result :" + GetHexString(bDecrypted))
    Console.ReadLine()
 End Sub

 Private Function GetHexString(ByVal bytes() As Byte, Optional ByVal len As Integer = -1, Optional ByVal spaces As Boolean = False) As String
    If len = -1 Then len = bytes.Length
    Dim i As Integer
    Dim s As String = ""
    For i = 0 To len - 1
        s += bytes(i).ToString("x2")
        If spaces Then s += " "
    Next
    If spaces Then s = s.TrimEnd()
    Return s
 End Function

 Function HexStringToBytes(ByVal hexstring As String) As Byte()
    Dim out((hexstring.Length / 2) - 1) As Byte
    For i = 0 To (hexstring.Length / 2) - 1
        out(i) = Convert.ToByte(hexstring.Substring(i * 2, 2), 16)
    Next
    Return out
 End Function

 Sub Encrypt()
    Dim icryptT As ICryptoTransform = desprovider.CreateEncryptor(bKey, nullIV)
    icryptT.TransformBlock(bData, 0, bData.Length, bEncrypted, 0)
 End Sub

 Sub Decrypt()
    Dim icryptT As ICryptoTransform = desprovider.CreateDecryptor(bKey, nullIV)
    icryptT.TransformBlock(bEncrypted, 0, bEncrypted.Length, bDecrypted, 0)
 End Sub

End Module

Here's the output:

Data: 1234567887654321

Key: 1111222233334444

Encryption Result :cb8304b91ce6f9a1

Decryption Result :0000000000000000

As you can see in the output, the Encrypt() subroutine works just fine but it all goes wrong with the decryption part. The decryption is supposed to return my original data but it seems like there's nothing happening in the Decrypt() subroutine of the program.

Was it helpful?

Solution

The ICryptoTransform provides a separate function: TransformFinalBlock that should be used when encrypting/decrypting the buffer that contains the last block of data, and ensures that necessary padding is added. Since you are only working with a single block, you should be using this method rather than TransformBlock. Note that returns the encrypted/decrypted data, rather than placing it in a buffer you pass as a parameter.

No answer regarding DES would be complete without mentioning that DES it's insecure, and more generally, that you should be using a unique, random IV each time the data is encrypted, but I'll assume this is mainly for practice, rather than intended for use protecting anything actually sensitive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top