Aric Levin

Hi,

I am getting the following error

Cryptographic Exception - Length of the data to decrypt is invalid

while trying to decrypt a byte from the database.

The encryption is performed using Legacy VB6 RC2 code.

private string Decrypt(string sGUID, byte[] source)
{
// string MS_DEF_PROV = "Microsoft Base Cryptographic Provider v1.0";
string MS_STR_PROV = "Microsoft Strong Cryptographic Provider";

CspParameters cspParams = new CspParameters(1, MS_STR_PROV);
PasswordDeriveBytes passwordBytes = new PasswordDeriveBytes(sGUID, null, "SHA-1", 1, cspParams);
byte[] rgbIV = new byteMusic;
byte[] key = new byteMusic;
key = passwordBytes.CryptDeriveKey("RC2", "SHA1", 0, rgbIV);

RC2CryptoServiceProvider provider = new RC2CryptoServiceProvider();
provider.KeySize = 128;
provider.Key = key;
provider.IV = rgbIV;

ICryptoTransform transform = provider.CreateDecryptor();
byte[] Decrypted;
Decrypted = transform.TransformFinalBlock(source, 0, source.Length - 1);
string sResult = System.Text.Encoding.Unicode.GetString(DecryptedString);
return sResult;

}

The line that gives me the error is: TransformFinalBlock(source, 0, source.Length-1);

Thanks,

Aric



Re: Visual C# General Cryptographic Exception - Length of the data to decrypt is invalid

TaylorMichaelL

Most crypto algorithms require data to be of a fixed size. Based upon my reading of MSDN this particular provider requires 8 byte blocks. Therefore you should pass it a block of 8 bytes (8, 16, 24, etc). You should have been given the entire block when you encrypted the data (it pads it before it is returned during encryption). If each block isn't 8 bytes then the function will fail.

Michael Taylor - 10/26/07

http://p3net.mvps.org





Re: Visual C# General Cryptographic Exception - Length of the data to decrypt is invalid

Aric Levin

Sorry,

The code must have gotten screwed up when I copied it:

The following lines should have been:

byte[] rgbIV = new byte(8);
byte[] key = new byte(8);

I am using round brackets cause the square brackets create the music sign...

Thanks.





Re: Visual C# General Cryptographic Exception - Length of the data to decrypt is invalid

TaylorMichaelL

For the algorithm you chose source must be in 8 byte blocks. If it isn't then you need to pad it so it is. Otherwise the algorithm will fail.

Michael Taylor - 10/26/07

http://p3net.mvps.org