Get the public key from RSACryptoServiceProvider?

I am trying to use public key encryption to communicate with the client and server. It is assumed that the server will generate a 1024-bit public key and send it to the client, where the client will use this key to send encrypted data back to the server. So far, I have initialized RSACryptoServiceProvider with the following:

RSACryptoServiceProvider rsaEncryption = new RSACryptoServiceProvider(1024); 

Now I know that I can use ExportParameters to get the exponent and module from RSACryptoServiceProvider. However, I am wondering how I can use this data to send the public key to the client (which will also use RSACryptoServiceProvider ), and how can the client use this data to encrypt something to send me back? Or am I doing this completely wrong?

+4
source share
2 answers

Your logic looks fine, and it seems that you only need sample code.

  using System; using System.Security.Cryptography; using System.Text; namespace RSA { class Program { static void Main(string[] args) { try { var rsaServer = new RSACryptoServiceProvider(1024); var publicKeyXml = rsaServer.ToXmlString(false); var rsaClient = new RSACryptoServiceProvider(1024); rsaClient.FromXmlString(publicKeyXml); var data = Encoding.UTF8.GetBytes("Data To Be Encrypted"); var encryptedData = rsaClient.Encrypt(data, false); var decryptedData = rsaServer.Decrypt(encryptedData, false); Console.WriteLine(Encoding.UTF8.GetString(decryptedData)); Console.WriteLine("OK"); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); } } } 
+15
source

1 - Please use> = 2048 bits in your key to protect up to ~ 2025.

The above is from 2012, but I came across this while trying to create the ssh-rsa key for Putty / Linux server connections.

I just solved a similar problem of creating the ssh-rsa public key in the appropriate format to fit PuttyGen.

For Microsoft.net RSACryptoServiceProvider it will look like

  RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(3072); byte[] sshrsa_bytes = Encoding.Default.GetBytes("ssh-rsa"); byte[] n = RSA.ExportParameters(false).Modulus; byte[] e = RSA.ExportParameters(false).Exponent; string buffer64; using (MemoryStream ms = new MemoryStream()) { ms.Write(ToBytes(sshrsa_bytes.Length), 0, 4); ms.Write(sshrsa_bytes, 0, sshrsa_bytes.Length); ms.Write(ToBytes(e.Length), 0, 4); ms.Write(e, 0, e.Length); ms.Write(ToBytes(n.Length+1), 0, 4); //Remove the +1 if not Emulating Putty Gen ms.Write(new byte[] { 0 }, 0, 1); //Add a 0 to Emulate PuttyGen (remove it not emulating) ms.Write(n, 0, n.Length); ms.Flush(); buffer64 = Convert.ToBase64String(ms.ToArray()); } string pubssh = string.Format("ssh-rsa {0} generated-key", buffer64); 

You can see my secret key, which I used for testing, and the link to the source code putty gen https://www.cameronmoten.com/2017/12/21/rsacryptoserviceprovider-create-a-ssh-rsa-public-key/

I work at Microsoft, but this is not a personal answer from Microsoft.

Original mail for BouncyCastle ( Link )

0
source

All Articles