Can anyone tell me whats wrong with this code?

G

Guest

I found similar code for encoding/decoding strings in VB which works fine.
However I wanted to use it in a C# projected and can't get it to work.
When it executes the TransformFinalBlock() call at the end it always returns
the error

"System.Security.Cryptography.CryptographicException: Length of the data to
decrypt is invalid."

Can anyone see what I am doing wrong - I have tried playing with the lengths
and the construction of the byte array without any luck:

public string EncryptTripleDES(string sIn, string sKey )
{
// takes a string and encodes it
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

// Compute the MD5 hash.
DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sKey));

// Set the cipher mode.
DES.Mode = CipherMode.ECB;

// Create the encryptor.
ICryptoTransform DESEncrypt = DES.CreateDecryptor();

// Tried with out any luck!
// int byteCount = System.Text.ASCIIEncoding.ASCII.GetByteCount(sIn);
// Byte[] Buffer = new Byte[byteCount];

byte[] Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(sIn);

// Transform and return the string.
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0,
Buffer.Length));
}

Its this very last line that fails - I have tried Length-1 but that doesnt
work, I've also tried creatinf the byte array first, with same result
TIA
Tony
 
S

Scott Allen

Hi Tony:

I think this is simple - the giveaway is the error message that says
it has a problem DECRYPTING data in your encryption function.

Change

DES.CreateDecryptor();

to

DES.CreateEncryptor();

and you should be fine. Looks like a copy and paste error :)
 
G

Guest

Hi Scott

A huge thank you for spotting this - you wont believe the hours I spent
scratching my head looking at the length of the Buffer

Thanks
Tony :)

Scott Allen said:
Hi Tony:

I think this is simple - the giveaway is the error message that says
it has a problem DECRYPTING data in your encryption function.

Change

DES.CreateDecryptor();

to

DES.CreateEncryptor();

and you should be fine. Looks like a copy and paste error :)

--
Scott
http://www.OdeToCode.com

I found similar code for encoding/decoding strings in VB which works fine.
However I wanted to use it in a C# projected and can't get it to work.
When it executes the TransformFinalBlock() call at the end it always returns
the error

"System.Security.Cryptography.CryptographicException: Length of the data to
decrypt is invalid."

Can anyone see what I am doing wrong - I have tried playing with the lengths
and the construction of the byte array without any luck:

public string EncryptTripleDES(string sIn, string sKey )
{
// takes a string and encodes it
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

// Compute the MD5 hash.
DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sKey));

// Set the cipher mode.
DES.Mode = CipherMode.ECB;

// Create the encryptor.
ICryptoTransform DESEncrypt = DES.CreateDecryptor();

// Tried with out any luck!
// int byteCount = System.Text.ASCIIEncoding.ASCII.GetByteCount(sIn);
// Byte[] Buffer = new Byte[byteCount];

byte[] Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(sIn);

// Transform and return the string.
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0,
Buffer.Length));
}

Its this very last line that fails - I have tried Length-1 but that doesnt
work, I've also tried creatinf the byte array first, with same result
TIA
Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top