473,795 Members | 2,922 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 TransformFinalB lock() call at the end it always returns
the error

"System.Securit y.Cryptography. CryptographicEx ception: 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 EncryptTripleDE S(string sIn, string sKey )
{
// takes a string and encodes it
TripleDESCrypto ServiceProvider DES = new TripleDESCrypto ServiceProvider ();
MD5CryptoServic eProvider hashMD5 = new MD5CryptoServic eProvider();

// Compute the MD5 hash.
DES.Key = hashMD5.Compute Hash(System.Tex t.ASCIIEncoding .ASCII.GetBytes (sKey));

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

// Create the encryptor.
ICryptoTransfor m DESEncrypt = DES.CreateDecry ptor();

// Tried with out any luck!
// int byteCount = System.Text.ASC IIEncoding.ASCI I.GetByteCount( sIn);
// Byte[] Buffer = new Byte[byteCount];

byte[] Buffer = System.Text.ASC IIEncoding.ASCI I.GetBytes(sIn) ;

// Transform and return the string.
return Convert.ToBase6 4String(DESEncr ypt.TransformFi nalBlock(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 1761
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.CreateDecry ptor();

to

DES.CreateEncry ptor();

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**@discussio ns.microsoft.co m> 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 TransformFinalB lock() call at the end it always returns
the error

"System.Securi ty.Cryptography .CryptographicE xception: 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 EncryptTripleDE S(string sIn, string sKey )
{
// takes a string and encodes it
TripleDESCrypt oServiceProvide r DES = new TripleDESCrypto ServiceProvider ();
MD5CryptoServi ceProvider hashMD5 = new MD5CryptoServic eProvider();

// Compute the MD5 hash.
DES.Key = hashMD5.Compute Hash(System.Tex t.ASCIIEncoding .ASCII.GetBytes (sKey));

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

// Create the encryptor.
ICryptoTransfo rm DESEncrypt = DES.CreateDecry ptor();

// Tried with out any luck!
// int byteCount = System.Text.ASC IIEncoding.ASCI I.GetByteCount( sIn);
// Byte[] Buffer = new Byte[byteCount];

byte[] Buffer = System.Text.ASC IIEncoding.ASCI I.GetBytes(sIn) ;

// Transform and return the string.
return Convert.ToBase6 4String(DESEncr ypt.TransformFi nalBlock(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.CreateDecry ptor();

to

DES.CreateEncry ptor();

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**@discussio ns.microsoft.co m> 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 TransformFinalB lock() call at the end it always returns
the error

"System.Securi ty.Cryptography .CryptographicE xception: 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 EncryptTripleDE S(string sIn, string sKey )
{
// takes a string and encodes it
TripleDESCrypt oServiceProvide r DES = new TripleDESCrypto ServiceProvider ();
MD5CryptoServi ceProvider hashMD5 = new MD5CryptoServic eProvider();

// Compute the MD5 hash.
DES.Key = hashMD5.Compute Hash(System.Tex t.ASCIIEncoding .ASCII.GetBytes (sKey));

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

// Create the encryptor.
ICryptoTransfo rm DESEncrypt = DES.CreateDecry ptor();

// Tried with out any luck!
// int byteCount = System.Text.ASC IIEncoding.ASCI I.GetByteCount( sIn);
// Byte[] Buffer = new Byte[byteCount];

byte[] Buffer = System.Text.ASC IIEncoding.ASCI I.GetBytes(sIn) ;

// Transform and return the string.
return Convert.ToBase6 4String(DESEncr ypt.TransformFi nalBlock(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
2396
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."', '".$_POST."')"); ?> <script language="JavaScript"> <!--
3
2043
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');" %>'>Need Help?</asp:HyperLink> -- mahsa
7
2233
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 me why? Its probably sooooooo obvious, but it is 1.19 am! Thanks. www.thunderin.co.uk/
5
2729
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 6,000 records this suddenly drops off to around 40 records per second. This is a big problem as the "live" list is over 4 million records long. I'd break it up but this is to be a regular test so that would be messy
1
1996
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 braces called
11
1466
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
1164
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 using c# <code deleted>
1
1400
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++ Code in perl. Please tell , what ever u understood . Thanks in advance #include <iostream> #include "stdafx.h"
5
2010
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 performing some checks.what to do ?
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7541
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.