我想使用自己的公钥和私钥初始化RSACryptoServiceProvider。
据我所知,这样做的方法是调用构造函数并传入参数:
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams);
如上所示,cspParams是一个重要的参数。但当我查看msdn上有关其用法的示例时: http://msdn.microsoft.com/en-us/library/ca5htw4f.aspx
我没有看到任何地方设置私钥或公钥,只使用了KeyContainer。如果我创建一个没有cspParam的RSACryptoServiceProvider,那么默认情况下它会仅使用公钥。 当我在类本身上检查PublicOnly变量时,我注意到这一点,它是一个只读变量。
我的问题是如何初始化此类并设置自己的私钥和公钥。 服务器将使用私钥,而客户端将拥有公钥。
我发现创建一个RSAParameter对象,并将其上的.Exponent和.Modulus参数分别设置为公共和私有变量。
但是我收到一个"Missing Private Key"错误,因为我认为RSACryptoServiceProvider没有使用正确的构造函数进行初始化。
以下是我的一些代码。不用担心BigInteger类,它只是一个实验。即使我使用它或不使用它,我也会得到相同的错误。
//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("19579160939939334264971282204525611731944172893619019759209712156289528980860378672033164235760825723282900348193871051950190013953658941960463089031452404364269503721476236241284015792700835264262839734314564696723261501877759107784604657504350348081273959965406686529089170062268136253938904906635532824296510859016002105655690559115059267476786307037941751235763572931501055146976797606538425089134251611194500570922973015579287289778637105402129208324300035518642730384616767241853993887666288072512402523498267733725021939287517009966986976768028023180137546958580922532786773172365428677544232641888174470601681", 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);
//}