Public Key / Private Key

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
I want to define a Public Key and Private Key and Encrypt / Decrypt data using RSACryptoServiceProvider. I tried the following:<br/>

<pre class="prettyprint public static String Encrypt(String data, String key, Encoding encoding) {

using (RSACryptoServiceProvider provider = new RSACryptoServiceProvider()) {
provider.FromXmlString(key);
Byte[] encrypted = provider.Encrypt(encoding.GetBytes(data), true);
provider.Clear();
return Convert.ToBase64String(encrypted);
}
} // Encrypt

public static String Decrypt(String data, String key, Encoding encoding) {

using (RSACryptoServiceProvider provider = new RSACryptoServiceProvider()) {
provider.FromXmlString(key);
Byte[] decrypted = provider.Decrypt(Convert.FromBase64String(data), true);
provider.Clear();
return encoding.GetString(decrypted);
}

} // Decrypt[/code]
This is working if I first define the Key as follows:
<pre class="prettyprint String key = new RSACryptoServiceProvider().ToXmlString(true);[/code]
But how can I explicity define a public key and a private key and then use them.
And can the public and private keys be guids? Or should they be guids?
Thank You,
Miguel




View the full article
 
Back
Top