Link to home
Start Free TrialLog in
Avatar of Krazz
Krazz

asked on

DES Decryption of XML-file

I have an XML file that is encrypted with DES encryption. I don't have any encryption knowledge so I basically only need enough information to get going ont he correct track here.

The XML file is encrypted and I do have the key for it, I just don't know how to write a program to decrypt it. All the examples I have found always include a KEY and an IV. As I said, I have the key, but what is the IV and how do I figure it out? Or is there a way around this where I don't need the IV?

Ohhh, and of course I would know how to encrypt a file back using the same type of code (not needing to know the IV).

I would prefer any code examples in VB.Net if possible.
Avatar of NetSecX
NetSecX

The Initialization Vector (IV) serves as a seed that is used to encrypt and decrypt the first block of bytes. This ensures that no two blocks of data produce the same block of encrypted text.

Try using same IV and Key to decrypt.
=================================

Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO


Public Class Tester
    Public Shared Sub Main
        Try
            Dim myDESProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider()

            myDESProvider.Key = ASCIIEncoding.ASCII.GetBytes("12345678")
            myDESProvider.IV = ASCIIEncoding.ASCII.GetBytes("12345678")

            Dim DecryptedFile As FileStream = New FileStream("testDes.txt", FileMode.Open, FileAccess.Read)
            Dim myICryptoTransform As ICryptoTransform = myDESProvider.CreateDecryptor(myDESProvider.Key, myDESProvider.IV)
            Dim myCryptoStream As CryptoStream = New CryptoStream(DecryptedFile, myICryptoTransform, CryptoStreamMode.Read)

            Dim myDecStreamReader As New StreamReader(myCryptoStream)
            Dim myDecStreamWriter As New StreamWriter("test.txt")

            myDecStreamWriter.Write(myDecStreamReader.ReadToEnd())

            myCryptoStream.Close()
            myDecStreamReader.Close()
            myDecStreamWriter.Close()
        Catch ex As Exception

            Console.WriteLine(ex.ToString())
        End Try
    End Sub
End Class
Avatar of Krazz

ASKER

Thanks for your help. I have tried this before but the key is too long for this approach. The key is 45 characters long if that makes any difference?!

I also get an exception that says exactly this:
System.ArgumentException: The provided key size is not allowed for this algoritm.
   vid System.Security.Cryptography.DES.set_Key(Byte[] value)
   vid DESDecrypt.Tester.Main() i D:\Private\Dokument\Visual Studio 2005\Projects\DESDecrypt\DESDecrypt\Class1.vb:rad 11
Avatar of Krazz

ASKER

I found a sollution that works.

Imports System.Security.Cryptography
Imports System.IO

Public Class XmlDecrypter
    Public Shared Sub Decrypt(ByVal FileIn As String, ByVal FileOut As String, ByVal DecryptKey As String)
        Dim fs As FileStream = Nothing
        Dim cs As CryptoStream = Nothing
        Dim ds As DataSet = New DataSet
        Dim buffer2 As Byte() = Nothing
        Dim bytes As New PasswordDeriveBytes(DecryptKey, Nothing)
        Dim rgbIV As Byte() = New Byte(8 - 1) {}
        Dim provider As New DESCryptoServiceProvider

        buffer2 = bytes.CryptDeriveKey("DES", "MD5", 0, rgbIV)

        fs = New FileStream(FileIn, FileMode.Open, FileAccess.Read, FileShare.None)

        cs = New CryptoStream(fs, provider.CreateDecryptor(buffer2, rgbIV), CryptoStreamMode.Read)

        ds.ReadXml(cs)
        ds.WriteXml(FileOut)
    End Sub
End Class
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial