RSACryptoServiceProvider initializes its own public key and private key

I am trying to initialize RSACryptoServiceProvider using my own public and private keys.

As far as I could research, the way to do this is to call the constructor with

RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams); 

cspParams as shown above. However, when I look at the msdn example for its use: http://msdn.microsoft.com/en-us/library/ca5htw4f.aspx

I do not see the place where they install private or public keys. Only using KeyContainer. When I create RSACryptoServiceProvider without cspParam, then only the public key is set by default. I notice this when I check the PublicOnly variable on the class itself and is a read-only variable.

My question is how to initialize this class and then set my own private and public keys. The server will use the private key, and the client will have the public key.

What I learned is creating an RSAParameter object and setting .Exponent and .Modulus parameters as public and private variables, respectively.

But I get the " Private key is missing" error, since I believe that RSACryptoServiceProvider is not initialized with the correct constructor.

The following is part of my code. Don't worry about the BigInteger class, it's just an experiment. Even if I use it or not, I get the same error.

 //Create a UnicodeEncoder to convert between byte array and string. UnicodeEncoding ByteConverter = new UnicodeEncoding(); byte[] dataToEncrypt = ByteConverter.GetBytes(password); byte[] encryptedData; byte[] decryptedData; //RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSAParameters rsap = new RSAParameters(); BigInteger n = new BigInteger("", 10); BigInteger e = new BigInteger("65537", 10); //rsap.Modulus = ByteConverter.GetBytes(publicKey); rsap.Exponent = e.getBytes(); rsap.Modulus = n.getBytes(); /*rsap.Exponent = ByteConverter.GetBytes(publicKey); rsap.D = ByteConverter.GetBytes(publicKey); rsap.DP = ByteConverter.GetBytes(publicKey); rsap.DQ = ByteConverter.GetBytes(publicKey); rsap.P = ByteConverter.GetBytes(publicKey); rsap.Q = ByteConverter.GetBytes(publicKey); rsap.InverseQ = ByteConverter.GetBytes(publicKey);*/ using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) { //RSA.PublicOnly = false; RSA.ImportParameters(rsap); Debug.Log ("PublicOnly: " + RSA.PublicOnly); Debug.Log (rsap.Modulus.Length); //Debug.Log (RSA.ToString()); //Pass the data to ENCRYPT, the public key information //(using RSACryptoServiceProvider.ExportParameters(false), //and a boolean flag specifying no OAEP padding. //encryptedData = RSACSPSample.RSAEncrypt(dataToEncrypt, rsap, false); encryptedData = RSACSPSample.RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false); Debug.Log ("encryptedData: " + encryptedData); //Display the decrypted plaintext to the console. //Debug.Log("Decrypted plaintext: " + ByteConverter.GetString("")); //Pass the data to DECRYPT, the private key information //(using RSACryptoServiceProvider.ExportParameters(true), //and a boolean flag specifying no OAEP padding. decryptedData = RSACSPSample.RSADecrypt(encryptedData, RSA.ExportParameters(true), false); } //encryptedData = RSACSPSample.RSAEncrypt(dataToEncrypt, rsap, false); //if (encryptedData != null) { password = ByteConverter.GetString(decryptedData); //} 
+7
source share
2 answers

Fields are terribly named, and it confuses you. The Exponent field is indeed a public indicator of the public key. The private exponent for the private key is D

It is not your fault that the MSDN documentation sucks.

+16
source

You need to convert base 64:

 byte[] modulusBytes = Convert.FromBase64String(modulus); byte[] exponentBytes = Convert.FromBase64String(exponent); 
0
source

All Articles