Re: Cryptographic Exception - Bad Data (DESCryptoServiceProvider)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 02/13/04


Date: Fri, 13 Feb 2004 15:25:07 -0000

Tyler <tyler@work.com> wrote:

<snip>
 
> However, that's not what I'm trying to do. To give you the whole picture,
> what I'm trying to do is to mimic a hardware hash algorithm. I want to copy
> the hardware algorithm in software so that I can output the intermediate
> steps. The hash algorithm processes the stream in 8 byte pieces as follows
> (assuming the stream provided has a length that is a multiple of 8 bytes):
> 1. Take the 1st 8 bytes as buffer
> 2. Encrypt the buffer (with an 8 byte DES key) and store the result in the
> buffer
> 3. If all bytes have been processed, goto 6
> 4. XOR the buffer with the next 8 bytes and store the result in the buffer
> 5. Goto 2
> 6. Decrypt the buffer (with a different 8 byte DES key - this is
> intentional) and store the result in the buffer
> 7. Encrypt the buffer (with the original DES key) and store the result in
> the buffer
> 8. Buffer now contains the desired result

Right. I don't know the details of DES, but the "bad data" exception
you're getting *suggests* to me that you can't just decrypt a whole
buffer with the wrong key.

<snip>

> To address your 2nd point, I did not call FlushFinalBlock because the final
> block of data is not relevant to the DES encryption result in which I am
> interested. I take the following sample data from FIPS 113
> (http://www.itl.nist.gov/fipspubs/fip113.htm) - it covers steps 1-5 I
> identified above:

<snip>

Right. I don't think you want a CryptoStream at all in that case - I
think you just want an ICryptoTransform which lets you transform a
block at a time. Here's a sample program which follows the above and
comes out with the "right" answer:

using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;

public class Test
{
    static void Main()
    {
        // Encryption key and initialization vector to use
        byte [] key = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
        byte [] iv = new byte [8];

        // Pad data if necessary
        byte[] inputData = Encoding.ASCII.GetBytes
             ("7654321 Now is the time for ");
        if ((inputData.Length % 8) != 0)
        {
            byte[] tmp = new byte[((inputData.Length+7)/8)*8];
            Array.Copy (inputData, 0, tmp, 0, inputData.Length);
            inputData = tmp;
        }
        
        // Start with empty buffer
        byte[] buffer = new byte[8];
        for(int i=0; i < inputData.Length; i+=8)
        {
            // XOR result with previous contents of buffer
            for (int j=0; j < 8; j++)
            {
                buffer[j] ^= inputData[i+j];
            }
            // Now encrypt the buffer
            buffer = Encrypt (buffer, key, iv);
            Console.WriteLine (BitConverter.ToString(buffer));
        }
    }
    
    static byte[] Encrypt (byte[] buffer, byte[] key, byte[] iv)
    {
        DESCryptoServiceProvider provider = new
             DESCryptoServiceProvider();
        
        ICryptoTransform transform = provider.CreateEncryptor(key, iv);
        
        byte[] ret = new byte[8];
        transform.TransformBlock (buffer, 0, 8, ret, 0);

        return ret;
    }
}

Does that help?

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: Bounds checking functions
    ... I guess if you do have a fixed size buffer, then yes, you want to be ... "I won't write correct code anyway, so I'll be as careless as I like". ... saying that nobody's perfect, though. ...
    (comp.lang.c)
  • Re: "Sorting" assignment
    ... The next optimization step is not to use a "buffer". ... I have deliberately used "extempore untested C based pseudo code" ... because given the utter randomness of C libraries, ...
    (comp.programming)
  • Re: track positions in arrays= index variables || pointers to elements?
    ... > and it holds an address of a member of the buffer. ... > It's type is unrelated to the type of the array. ...
    (comp.lang.c)
  • Re: Endless loop question
    ... > actually turn this while loop into an endless loop instead of waiting ... This reads a character but doesn't do anything with it, ... into a buffer with fgets, and then pick the desired data value ...
    (comp.lang.c)
  • Re: Simple Hash algorithm to detect duplicate content
    ... DotNetNewbie wrote: ... In that case you really don't want a hash algorithm. ... hash is to give an *indication* of equality in a reasonably cheap (in ...
    (microsoft.public.dotnet.languages.csharp)