473,322 Members | 1,736 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,322 software developers and data experts.

Problem with RSA crypto code

Hello all,

I've been trying to get a public key solution working but have been having a
few problems. For starters there is a lot of contradictory information out
there, MSDN is not much help, and a lot of the sample code available I have
found are buggy so don't provide much insight.

Unfortunately for me every developer I know either gives me a completely
blank look when I try to talk crypto, or they have some wildly inaccurate
and insecure views on how to use it (with no working code to back them up).

Anyway, I am hoping somebody here can shed some light on the class I have
attached. The code is a little rough, and I've cut some bits out so I hope I
got the right bits and the code still works. I seem to be able to encrypt
the string I pass in ok (although how do you tell?) but when I decrypt I get
the infamous "bad data" error. Maybe I've been staring at the screen too
long but I can't see the problem.

Oh yeah, another thing I have found - some sites state the maximum block
size is keylength / 32, some say (keylength /8) - 11, and I've seen others.
I have tried them all and not sure what effect it is having. Any solid
information on this? I know RSA is for small messages but I still want to be
able to process as big a string as I want if need be.

If anybody can help me get this working I'd appreciate the effort.

Regards, and thanks
public class Asymmetric
{
public Asymmetric()
{
}

public class RSAProvider
{
public static RSACryptoServiceProvider GetCryptoProvider()
{
try
{
CspParameters cspParam = new CspParameters();
cspParam.Flags=CspProviderFlags.UseMachineKeyStore ;
return new RSACryptoServiceProvider(cspParam);
}
catch(Exception ex)
{
return null;
}
}

public static string GeneratePublicKey(RSACryptoServiceProvider
rsaCrypto)
{
try
{
if (rsaCrypto != null)
{
return rsaCrypto.ToXmlString(false);
}
return "";
}
catch(Exception ex)
{
return "";
}
}

public static string GeneratePublicKey()
{
RSACryptoServiceProvider rsaCrypto;
try
{
rsaCrypto = new RSACryptoServiceProvider();
if (rsaCrypto != null)
{
return rsaCrypto.ToXmlString(false);
}
return "";
}
catch(Exception ex)
{
return "";
}
}

public static string GeneratePrivateKey(RSACryptoServiceProvider
rsaCrypto)
{
try
{
if (rsaCrypto != null)
{
return rsaCrypto.ToXmlString(true);
}
return "";
}
catch(Exception ex)
{
return "";
}
}

public static string GeneratePrivateKey()
{
RSACryptoServiceProvider rsaCrypto;
try
{
rsaCrypto = new RSACryptoServiceProvider();
if (rsaCrypto != null)
{
return rsaCrypto.ToXmlString(true);
}
return "";
}
catch(Exception ex)
{
return "";
}
}
}
public static string EncryptString(string strPublicKey, string
strClearText)
{
string strCipherText="";
string strTemp = "";
string strOriginal = "";
RSACryptoServiceProvider rsaCrypto=null;
try
{
rsaCrypto = RSAProvider.GetCryptoProvider();
if (rsaCrypto != null)
{
strOriginal=strClearText;
rsaCrypto.FromXmlString(strPublicKey);

Int32 intBlockSize = rsaCrypto.KeySize / 8; // - 11;
while(strClearText.Length 0)
{
if (strClearText.Length intBlockSize)
{
strTemp=strClearText.Substring(0,intBlockSize);
strCipherText += EncryptBlock(strTemp, rsaCrypto);
strClearText=strClearText.Substring(intBlockSize);
}
else
{
strCipherText += EncryptBlock(strClearText, rsaCrypto);
strClearText="";
}

}
}
return strCipherText;
}
catch(Exception ex)
{
return "Failed to encrypt string: " + ex.Message;
}
}

private static string EncryptBlock(string strClearText,
RSACryptoServiceProvider rsaCrypto)
{
return
UnicodeEncoding.Default.GetString(rsaCrypto.Encryp t(UnicodeEncoding.Default.GetBytes(strClearText),
false));
}
public static string DecryptString(string strPrivateKey, string
strCipherText)
{
string strClearText = "";
string strTemp = "";
RSACryptoServiceProvider rsaCrypto=null;
try
{
rsaCrypto = RSAProvider.GetCryptoProvider();
if (rsaCrypto != null)
{
// Apply the decryption key to the crypto provider
rsaCrypto.FromXmlString(strPrivateKey);

Int32 intBlockSize = rsaCrypto.KeySize-11; // / 8;
while(strCipherText.Length 0)
{
if (strCipherText.Length intBlockSize)
{
strTemp=strCipherText.Substring(0,intBlockSize);
strClearText += DecryptBlock(strTemp, rsaCrypto);
strCipherText=strCipherText.Substring(intBlockSize );
}
else
{
strClearText += DecryptBlock(strCipherText, rsaCrypto);
strCipherText="";
}
}
}
return strClearText;
}
catch(Exception ex)
{
return "Failed to decrypt string: " + ex.Message;
}
}
private static string DecryptBlock(string strCipherText,
RSACryptoServiceProvider rsaCrypto)
{
return
UnicodeEncoding.Default.GetString(rsaCrypto.Decryp t(UnicodeEncoding.Default.GetBytes(strCipherText),
false));
}

}
Dec 6 '06 #1
0 2095

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: crawlerxp | last post by:
This is the problem: I do not get the output I need when encoding and decoding data using rijndael alghoritm. Look at the code and see what the problem is actually: Please paste this code into...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
6
by: Michael Sparks | last post by:
Hi, I suspect this is a bug with AMK's Crypto package from http://www.amk.ca/python/code/crypto , but want to check to see if I'm being dumb before posting a bug report. I'm looking at...
7
by: c duden | last post by:
I am attempting to encrypt some text and be able to decrypt it at a later time. I have two methods to do this: public static Byte EncryptText(string textToEncrypt, string encryptionHash) {...
1
by: Saper\(ek\) | last post by:
I need to encrypt some data in my program, so I've created 2 functions to encrypt and decrypt data. I've created a simple program to test it, and... it crashes. It wors ok on XP, but on win98 it...
2
by: ThunderMusic | last post by:
Hi, I encrypted a string using the RijndaelManaged class. When decrypted, it happens that the string is longer then the original, but the exceeding chars are nothing...
3
by: magnets | last post by:
I have two C# files. To make it simple I've now put them in the same directory. The first few lines of the web page are as follows: <%@ Page language="C#" %> <%@ Reference Page="Crypto.cs" %>...
0
by: magnets | last post by:
I have two C# files. To make it simple I've now put them in the same directory. The first few lines of the web page are as follows: <%@ Page language="C#" %> <%@ Reference Page="Crypto.cs" %> <%...
12
by: Fett | last post by:
I need a crypto package that works on windows with python 2.5. Can anyone suggest one for me? I have been searching for a couple days for a good cryptography package to use for public/private...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.