Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,can you help me?
I want to encrypt text, when my private key is in the privat.xml file. My code doesn't work.

Thank you so much for help and your time.

What I have tried:

private void button2_Click(object sender, EventArgs e)
{
// convert the string into byte array
byte[] str = ASCIIEncoding.Unicode.GetBytes(textBox1.Text);

// read the private key
string privat = System.IO.File.ReadAllText(@"C:\Users\Gretaa\Desktop\RSA\privat.xml");

// compute the hash with any hash algorithm
SHA1Managed sha1hash = new SHA1Managed();
byte[] hashdata = sha1hash.ComputeHash(str);

// sign the hash data with private key
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(privat);

// signature hold the sign data of plaintext , signed by private key
byte[] signature = rsa.SignData(str, "SHA1");

textBox14.Text = signature.ToString();
}
Posted
Updated 11-Feb-17 5:46am

1 solution

Simple: privat.xml is not your key, it is an xml file that contain your key embedded in the xml structure.
You have to read the file as an xml file and then get the key in the structure.
 
Share this answer
 
Comments
Member 12995193 11-Feb-17 12:01pm    
How can I do that?
Patrice T 11-Feb-17 12:06pm    
Try "Google C# reading xml file", you will certainly get something useful.
Member 12995193 11-Feb-17 12:23pm    
But I read the file:
string privat = System.IO.File.ReadAllText(@"C:\Users\Gretaa\Desktop\RSA\privat.xml");
Patrice T 11-Feb-17 12:33pm    
Yes, but privat.xml is not your key.
There is no magic to extract the key.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900