473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Can anyone tell me whats wrong with this code?

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
2 1745
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris Geerdink | last post by:
combo with PHP. what is wrong with the Javascript? else { include("mysql.php"); $query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES ('".$_POST."', '".$_POST."',...
3
by: mahsa | last post by:
Hi do you know whats wrong with this code? <asp:HyperLink id="HLink_Help" runat="server" NavigateUrl='<%# "javascript:window.open('comments.aspx?id=1,width=500,height=600, scrollBars=yes');"...
7
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell...
5
by: tony | last post by:
I'm using PHP 5 on Win-98 command line (ie no web server involved) I'm processing a large csv file and when I loop through it I can process around 275 records per second. However at around...
1
by: yogesh | last post by:
i have a code as follows TServerDB() { fDB = Server()->dbStorage->Create(); } or fDB=TServer->TMySQLBD->Create(); can any one tell the relationship between two and whats the <between...
11
thatos
by: thatos | last post by:
The code is below import java.io.*; class Storage{ int num = 0; String name = null; } public class gift1 { public static void main(String args) throws IOException{
1
by: readnlearn | last post by:
Hii here is the program for check availabilty of username in registeration page can u please tell me whts wrong with this and i cannt get the exact format which i want to.. please give sample code...
1
by: koti688 | last post by:
Hi Mates, Can u please tell me whats happpenning in those. Please tell what ever u got. Outline the functinaliy in normal words. i am new to C++. I need to write SWIG wrapper which checks this C++...
5
by: Shalini Bhalla | last post by:
hi , A strange error is coming , when i run a php file , all the php and java script executes before submit only which actually should execute after clicking on submit button and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.