rahulzz

My project is Email project. In my project i want to Encrypt the sender message with his public key and decrypt the at Recepient side message using Senders private Key.The problem means When i entered some characters in textbox its encryptd.If i entered Some big files or more then two lines the error has been generated.The generated error is Key value is not valid.Im using 1024 bit key length.I have applied the brake point and checked my error its shows the key value is 472.But im using 1024 bit key length.So i could not get it Where i made the blunder.Here is the code

string plaintext = txttext.Text;

int keysize = 1024;

// By default it will create RSA keys

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keysize);

// Write to this file all rsa data

FileStream fs = new FileStream("rsa.xml", FileMode.OpenOrCreate, FileAccess.Write);

// This will produce all RSA data, including private key

byte[] rsadata = Encoding.ASCII.GetBytes(/*"<BitStrength>" + keysize.ToString()+*/ rsa.ToXmlString(true));

fs.Write(rsadata, 0, (int)rsadata.Length);

fs.Close();

//Initialize the the public key information.

const string publickey = ("\\keygeneration\\rsa.xml");

const string exponent = @"AQAB";

RSAParameters rsaKeyInfo = new RSAParameters();

//Set RSAKeyInfo to the public key values.

rsaKeyInfo.Modulus = Encoding.ASCII.GetBytes(publickey);

rsaKeyInfo.Exponent = Encoding.ASCII.GetBytes(exponent);

//Import key parameters into RSA.

rsa.ImportParameters(rsaKeyInfo);

try

{

byte[] ciphertext = rsa.Encrypt(Encoding.ASCII.GetBytes(plaintext), false);

encrypt = Encoding.ASCII.GetString(ciphertext);

}

catch (Exception ex)

{

Response.Write(ex.Message);

}

}

Please some one help me how to avoid this problem.........................

Regards

M.Rahul




Re: Common Language Runtime Problem in RSA.Need Help

rahulzz

Hi

I want to encrypt my message with sendrs public key.Then i send the message to receipient and decrypt the message using senders private key.The problem means if i entered few words in text box its encrypted.If i entered more then one or two lines i got the error message Key size is not valid.But im using the key size 1024 bit length.i have tried to check the error with help of using breakepoint its shows the key size value is 472 only.So i dont know how to reduce that error.Please some one can u help me................................

My code is here

string plaintext = txttext.Text;

int keysize = 1024;

// By default it will create RSA keys

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keysize);

// Write to this file all rsa data

FileStream fs = new FileStream("C:\\Documents and Settings\\gts\\Desktop\\keygeneration\\rsa.xml", FileMode.OpenOrCreate, FileAccess.Write);

// This will produce all RSA data, including private key

byte[] rsadata = Encoding.ASCII.GetBytes( rsa.ToXmlString(true));

fs.Write(rsadata, 0, (int)rsadata.Length);

fs.Close();

//Initialize the the public key information.

const string publickey = ("C:\\Documents and Settings\\gts\\Desktop\\keygeneration\\rsa.xml");

const string exponent = @"AQAB";

RSAParameters rsaKeyInfo = new RSAParameters();

//Set RSAKeyInfo to the public key values.

rsaKeyInfo.Modulus = Encoding.ASCII.GetBytes(publickey);

rsaKeyInfo.Exponent = Encoding.ASCII.GetBytes(exponent);

//Import key parameters into RSA.

rsa.ImportParameters(rsaKeyInfo);

try

{

byte[] ciphertext = rsa.Encrypt(Encoding.ASCII.GetBytes(plaintext), false);

encrypt = Encoding.ASCII.GetString(ciphertext);

}

catch (Exception ex)

{

Response.Write(ex.Message);

}






Re: Common Language Runtime Problem in RSA.Need Help

Rob Teixeira

RSA is not a block cipher. It only encrypts small pieces of data relative to the size of the RSA key.

If you want to encrypt entire messages, you should use a block cipher (such as AES/Rijndael). The problem is that most block ciphers are private key only (instead of 2 keys - public and private). So you must ensure that each side contains the same block cipher key, but they must share that key in a secure way. If someone catches that key in the middle of the exchange, they will be able to decrypt the message.

And that's where these two technologies merge. You use RSA to encrypt the block cipher key so that each side has a copy of the same block cipher key. You then encrypt/decrypt the messages using the block cipher.

Look into the RsaKeyExchangeFormatter/Deformatter classes for more info on how to use RSA to safely exchange block cipher keys.






Re: Common Language Runtime Problem in RSA.Need Help

Rob Teixeira

See my response to your other message here: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=2088893&SiteID=1






Re: Common Language Runtime Problem in RSA.Need Help

timvw

I found http://msdn.microsoft.com/msdnmag/issues/07/03/NETSecurity/ quite interesting to read...





Re: Common Language Runtime Problem in RSA.Need Help

rahulzz

Ok.Thanks for your reply....I have changed my coadings.....My new code is here

int keysize = 1024;

RSACryptoServiceProvider RSAProvider;

RSAProvider = new RSACryptoServiceProvider(keysize);

string publicAndPrivateKeys = RSAProvider.ToXmlString(true);

string PublicKey = RSAProvider.ToXmlString(false);

StreamWriter fs = new StreamWriter("C:\\Documents and Settings\\gts\\Desktop\\public.xml");

fs.Write(PublicKey);

fs.Close();

fs = new StreamWriter("C:\\Documents and Settings\\gts\\Desktop\\private.xml");

fs.Write(publicAndPrivateKeys);

fs.Close();

// read the keys

XmlTextReader xtr = new XmlTextReader("C:\\Documents and Settings\\gts\\Desktop\\private.xml");

publicAndPrivateKeys = ""; // reset keys

PublicKey = "";

while (xtr.Read())

{

publicAndPrivateKeys += xtr.ReadOuterXml();

}

xtr.Close();

xtr = new XmlTextReader("C:\\Documents and Settings\\gts\\Desktop\\public.xml");

while (xtr.Read())

{

PublicKey += xtr.ReadOuterXml();

}

xtr.Close();

// encrypt the message

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

string txt= txttext.Text;

rsa.FromXmlString(PublicKey);

byte[] plainbytes = Encoding.ASCII.GetBytes(txt);

byte[] cipherbytes = rsa.Encrypt(plainbytes,false);

encrypt=Convert.ToBase64String(cipherbytes);

}

Here the problem is if i send 117 byte characters it will not show any error message.If i send more then 117 byte char its displayed the error message as Key is not valid.I want to sent N no of char to my recepient.How cai i troubleshoot this problem..And can u suggest me how can i create the signature using RSA...........






Re: Common Language Runtime Problem in RSA.Need Help

Rob Teixeira

rahulzz, listen carefully Smile

You should NOT use RSA to encrypt messages.
You CANNOT encrypt more than 117 bytes with a 1024 RSA key.
RSA is not meant to encrypt large messages. It is meant to encrypt small pieces of data.

Use Rijndael to encrypt messages.
Use RSA to encrypt the Rijndael key, so you can send the Rijndael key to the other side.
Once both sides have the same Rijndael key, they can use the Rijndael algorithm to encrypt and decrypt messages of any size to each other.