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.
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.
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.Cryptograp hy.DES.set _Key(Byte[ ] value)
vid DESDecrypt.Tester.Main() i D:\Private\Dokument\Visual Studio 2005\Projects\DESDecrypt\D ESDecrypt\ Class1.vb: rad 11
I also get an exception that says exactly this:
System.ArgumentException: The provided key size is not allowed for this algoritm.
vid System.Security.Cryptograp
vid DESDecrypt.Tester.Main() i D:\Private\Dokument\Visual
ASKER
I found a sollution that works.
Imports System.Security.Cryptograp hy
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(Decryp tKey, 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(b uffer2, rgbIV), CryptoStreamMode.Read)
ds.ReadXml(cs)
ds.WriteXml(FileOut)
End Sub
End Class
Imports System.Security.Cryptograp
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(Decryp
Dim rgbIV As Byte() = New Byte(8 - 1) {}
Dim provider As New DESCryptoServiceProvider
buffer2 = bytes.CryptDeriveKey("DES"
fs = New FileStream(FileIn, FileMode.Open, FileAccess.Read, FileShare.None)
cs = New CryptoStream(fs, provider.CreateDecryptor(b
ds.ReadXml(cs)
ds.WriteXml(FileOut)
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try using same IV and Key to decrypt.
==========================
Imports System.Security
Imports System.Security.Cryptograp
Imports System.Text
Imports System.IO
Public Class Tester
Public Shared Sub Main
Try
Dim myDESProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider()
myDESProvider.Key = ASCIIEncoding.ASCII.GetByt
myDESProvider.IV = ASCIIEncoding.ASCII.GetByt
Dim DecryptedFile As FileStream = New FileStream("testDes.txt", FileMode.Open, FileAccess.Read)
Dim myICryptoTransform As ICryptoTransform = myDESProvider.CreateDecryp
Dim myCryptoStream As CryptoStream = New CryptoStream(DecryptedFile
Dim myDecStreamReader As New StreamReader(myCryptoStrea
Dim myDecStreamWriter As New StreamWriter("test.txt")
myDecStreamWriter.Write(my
myCryptoStream.Close()
myDecStreamReader.Close()
myDecStreamWriter.Close()
Catch ex As Exception
Console.WriteLine(ex.ToStr
End Try
End Sub
End Class