This is my first post, so I hope that I have not missed anything important. I am doing a C # project where I need to use public / private key encryption to encrypt a message, and then send it over an SSL connection.
I decided to use RSACryptoService , as per the documentation, it was the only asymmetric encryption scheme used to encrypt data. The problem is that I have a lot of problems with this. (I wanted to do symmetric encryption, but thatโs not what my teacher wants me to do, and, according to him, it should be easy to just determine the size of the block, and then it should do all the work for you.) Well, not yet I was lucky and I tried different approaches, but now I returned to the basics and try again, this is my current code:
public string[] GenerateKeysToStrings(string uniqueIdentifier) { string[] keys; using (var rsa = new RSACryptoServiceProvider(4096)) { try { string privateKey = rsa.ToXmlString(true); string publicKey = rsa.ToXmlString(false); this.pki.StoreKey(publicKey, uniqueIdentifier); keys = new string[2]; keys[0] = privateKey; keys[1] = publicKey; } finally {
As you can see, I generate the keys, and I cheat on PKI by sending the public key to a simple class that stores it, and then the private key is written to the file (Note that I also have another method that does the same, but saves it to an array instead, simply because I wanted to check and simplify things, because I get No such key exceptions and sometimes cryptographic exceptions when I do it as shown in the example, so I wanted to simplify it just by saving the line rsa.ToXmlString , as a string in the array, but not behavior lo.)
Now I have an encryption and decryption method as follows:
public string Encrypt(string keyString, string message) { string encryptedMessage; using (var rsa = new RSACryptoServiceProvider()) { try {
decryption:
public string Decrypt(string keyString, string message) { string decryptedText; using (var rsa = new RSACryptoServiceProvider()) { try {
I know this is a wall with text, but I hope you can help me because I have been banging my head on the wall for so long, it's not funny :)
The problem is how can I encrypt messages using RSA (or any other public / private key)
Here is a test client:
public static void Main(string[] args) { PublicKeyInfrastructure pki = new PublicKeyInfrastructure(); Cryptograph crypto = new Cryptograph(); string[] keys = crypto.GenerateKeysToStrings(" simonlanghoff@gmail.com "); string plainText = "Hello play with me, please"; string publicKey = crypto.GetPublicKey(" simonlanghoff@gmail.com "); string encryptedText = crypto.Encrypt(keys[0], plainText); string decryptedText = crypto.Decrypt(keys[1], encryptedText); }
As I mentioned, there are string arrays because I wanted to fix a bad parsing error from XML documents ...
When I run the test client, if I use the private key for encryption and the public key for decryption, I get โThe key does not exist exceptionโ, and if I do it the other way around, I get an exception for bad data.
Please help me guys, if you know about some good guidance or can tell me how to somewhat hide the public / private key encryption implementation in string messages, please help me.
I appreciate any help.