Hello Nicholas,
I think what you want is the TripleDESCryptoServiceProvider class in the System.Security.Cryptography namespace.
I have already tried the sample from the System.Security.Cryptography.TripleDESCryptoServiceProvider namespace. I need to decrypt a
string which was encrypted with openssl:
openssl enc -des-ede3-cbc -K 01234567890ABCEF12345678 -iv 12345678 -e -in in.txt -out out.txt
Decrypting the out.txt file with openssl works fine, but when I decrypt the out.txt with the following code, I get an error at
decStream.Close() "Bad Data.\r\n" and the written textfile contains unreadable text instead of the decoded text.
---- modified Code from MSDN ----
private static void DecryptData(String inName, String outName, byte[] tdesKey, byte[] tdesIV)
{
try
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Mode = CipherMode.CBC;
CryptoStream decStream = new CryptoStream(fout, tdes.CreateDecryptor(tdesKey, tdesIV), CryptoStreamMode.Write);
Console.WriteLine("Decrypting ...");
//Read from the input file, then encrypt and write to the output file.
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
decStream.Write(bin, 0, len);
rdlen = rdlen + len;
Console.WriteLine("{0} bytes processed", rdlen);
}
decStream.Close();
}
catch(System.Exception ex){
Console.WriteLine(ex.Message);
}
}
-----
Thank You
/Joachim
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
v.Seydewitz said:
Hello NG,
How can I decrypt an des ede3 encrypted string with .NET? The string was encrypted by using the OpenSSL Method des-ede3-cbc.
Thank You!
Joachim