Re: Cryptographic Exception - Bad Data (DESCryptoServiceProvider)
From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 02/13/04
- Next message: Frank Lesser [LSW]: "Re: .Net Decompiler - How do I protect my .Net Product?"
- Previous message: Dmitriy Lapshin [C# / .NET MVP]: "Re: Remoting and objects design problem"
- In reply to: Tyler: "Re: Cryptographic Exception - Bad Data (DESCryptoServiceProvider)"
- Next in thread: Tyler: "Re: Cryptographic Exception - Bad Data (DESCryptoServiceProvider)"
- Reply: Tyler: "Re: Cryptographic Exception - Bad Data (DESCryptoServiceProvider)"
- Reply: Tyler: "Re: Cryptographic Exception - Bad Data (DESCryptoServiceProvider)"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Frank Lesser [LSW]: "Re: .Net Decompiler - How do I protect my .Net Product?"
- Previous message: Dmitriy Lapshin [C# / .NET MVP]: "Re: Remoting and objects design problem"
- In reply to: Tyler: "Re: Cryptographic Exception - Bad Data (DESCryptoServiceProvider)"
- Next in thread: Tyler: "Re: Cryptographic Exception - Bad Data (DESCryptoServiceProvider)"
- Reply: Tyler: "Re: Cryptographic Exception - Bad Data (DESCryptoServiceProvider)"
- Reply: Tyler: "Re: Cryptographic Exception - Bad Data (DESCryptoServiceProvider)"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|