By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

Can anyone tell me whats wrong with this code?

P: n/a
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.CryptographicExcepti on: 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.ASCI I.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.TransformFinalBl ock(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
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

On Sun, 12 Sep 2004 17:51:03 -0700, "Tony"
<To**@discussions.microsoft.com> wrote:
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.CryptographicExcept ion: 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.ASCI I.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.TransformFinalBl ock(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


Nov 16 '05 #2

P: n/a
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" wrote:
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

On Sun, 12 Sep 2004 17:51:03 -0700, "Tony"
<To**@discussions.microsoft.com> wrote:
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.CryptographicExcept ion: 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.ASCI I.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.TransformFinalBl ock(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


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.