|
-
August 24th, 2006, 02:06 AM
#1
Problem in 3DES decryption using CBC mode
I am using CBC as the cipher mode to decrypt some text I'd encrypted using the same mode for 3DES algorithm. My decrypt does not return the correct result because the TransformBlock method returns 0 as bytes read. Here's my code:
Code:
public static string Decrypt(string seed)
{
System.IO.MemoryStream stream = new System.IO.MemoryStream();
TripleDESCryptoServiceProvider TripleDESProvider = new TripleDESCryptoServiceProvider();
TripleDESProvider.Key = new System.Text.ASCIIEncoding().GetBytes(ComputeHash(NativeEncryptionKey));
TripleDESProvider.Mode = CipherMode.CBC;
ICryptoTransform Decryptor = TripleDESProvider.CreateDecryptor();
byte[] bSeed = new System.Text.ASCIIEncoding().GetBytes(seed);
int numBlocks = (int)(bSeed.Length / TripleDESProvider.IV.Length);
int finalBlockSize = (int) (bSeed.Length % TripleDESProvider.IV.Length);
int pos = 0;
byte[] btmp;
for (int i = 0; i < numBlocks; i++)
{
btmp = new byte[TripleDESProvider.IV.Length];
int bytesRead = Decryptor.TransformBlock(bSeed, i * TripleDESProvider.IV.Length, TripleDESProvider.IV.Length, btmp, 0);
stream.Write(btmp, 0, btmp.Length);
pos += btmp.Length;
}
if (finalBlockSize > 0)
{
btmp = null;
int bytesRead = numBlocks * TripleDESProvider.IV.Length;
btmp = Decryptor.TransformFinalBlock(bSeed, pos, finalBlockSize);
if (btmp.Length > 0)
stream.Write(btmp, 0, btmp.Length);
}
Decryptor = null;
bSeed = null;
TripleDESProvider = null;
return new System.Text.ASCIIEncoding().GetString(stream.ToArray());
}
Anyone?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|