CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2003
    Posts
    417

    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?

  2. #2
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Problem in 3DES decryption using CBC mode

    I've never seen anyone do the block transforms like that. Transform final block should do that for you shouldnt it?

  3. #3
    Join Date
    Feb 2003
    Posts
    417

    Re: Problem in 3DES decryption using CBC mode

    Quote Originally Posted by MadHatter
    I've never seen anyone do the block transforms like that. Transform final block should do that for you shouldnt it?
    No. TransformFinalBlock only works on the final block remaining after all the full-sized chunks have been completed.

    If you're using ECB as the cipher mode, then just a single call to TransformFinalBlock will do all the blocks together, but that is not so for CBC. Also, the name itself tells you -- transform only the final block.

    If your cipher mode is ECB, once again I repeat, it won't make much of a difference if you called just TransformFinalBlock.

    However, what you seem to be suggesting indicates confusion about the fundamental purpose of the functions TransformBlock and TransformFinalBlock. If you have 205 bytes to transform (either encrypt or decrypt), then CBC will work in chunks of 64-bits. Therefore, it will first transform the first 64-bits, then it'll pick up the next chunk of 64-bits and will work so on in multiples of 64-bits.

    What will be left will be the remainder (modulo) of 13 bits which will be handled by TransformFinalBlock.

  4. #4
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Problem in 3DES decryption using CBC mode

    blocks not 8 bytes wide are padded using whatever is specified in the PaddingMode.

    initial blocks are or'd w/ the IV and additional blocks are or'd w/ previous ones, so as far as I can see, you dont need to do this. I've used this w/ other non-microsoft tripledes libraries and I've never needed to transform the first blocks and they've encrtypted / decrypted correctly, so seeing as how you're getting some error you might re-check what you're doing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured