2012-11-30 57 views
7

我試圖用我自己的公鑰和私鑰初始化RSACryptoServiceProvider。RSACryptoServiceProvider用自己的公鑰和私鑰初始化

至於我可以研究,要做到這一點的方法是調用構造函數與

RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams); 

cspParams如上圖所示。但是,當我看着msdn的例子使用它: http://msdn.microsoft.com/en-us/library/ca5htw4f.aspx

我沒有看到他們設置私鑰或公鑰的地方。只使用KeyContainer。當我創建一個沒有cspParam的RSACryptoServiceProvider時,它默認設置爲只使用一個公鑰。當我檢查類本身的PublicOnly變量並且它是隻讀變量時,我注意到了這一點。

我的問題是如何初始化這個類,然後設置我自己的私鑰和公鑰。 服務器將使用私鑰並且客戶端將擁有公鑰。

我發現的是,創建一個RSAParameter對象,並將它的.Exponent和.Modulus參數分別設置爲公共和私有變量。

但我得到一個「丟失的私鑰」錯誤,因爲我相信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); 
//} 
+0

公鑰是由指數和RSA PARAMS模量定義。私鑰,在RSAParameters類中爲D – craig1231

+0

那麼,我該如何設置RSAParameter的公鑰呢?我已經有了我的公鑰,只需設置RSAParameter對象,以便我可以使用它。 – c0d3Junk13

+0

取決於是否有公鑰和私鑰使用,如果不是,則可以使用CSPParams創建一對。 – craig1231

回答

16

這些字段是可怕的命名,它是混淆你。 Exponent字段實際上是公鑰public的指數。 私鑰的指數是D字段。

MSDN文檔不是你的錯。

+0

是的,這些變量是非常有名的。讓我試試這個,回到你身邊。謝謝! – c0d3Junk13

+0

現在,我收到一個缺失模數的錯誤。任何想法是什麼意思?或者我需要將Modulus變量設置爲?這是否意味着我不需要使用cspParams進行初始化? – c0d3Junk13

+2

模數是...模數。如果您查看['RSAParameters'](http://msdn.microsoft.com/zh-cn/com/en-us/library/ke2te33h.aspx)結構RSA公鑰只有Modulus和Exponent字段,但RSA私有的所有字段。 –

0

你有基地64轉換:

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