2010-07-08 92 views
4

我有兩個AsymmetricAlgorithm對象包含一個RSA私鑰和RSA公鑰。私鑰從Windows-MY密鑰存儲區中取出,並從用戶證書中取回公鑰。我如何使用這些密鑰和RSACryptoServiceProvider一起使用C#中的RSA算法加密數據?換句話說,我如何指定我想要使用我已有的密鑰?使用不對稱算法私鑰和公鑰與RSA C#

+0

當你包含有關這些 '兩個對象' 的一些細節你可以得到更好的答案。 – 2010-07-08 18:39:41

+0

它們與我從用戶證書中提取的RSA密鑰相匹配,還有什麼可說的? – 2010-07-08 19:12:28

回答

2
#region "RSA Encrypt/Decrypt" 
public string RSAEncrypt(string str, string publicKey) 
{ 
    //---Creates a new instance of RSACryptoServiceProvider--- 
    try { 
    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); 
    //---Loads the public key--- 
    RSA.FromXmlString(publicKey); 
    byte[] EncryptedStr = null; 

    //---Encrypts the string--- 
    EncryptedStr = RSA.Encrypt(ASCII.GetBytes(str), false); 
    //---Converts the encrypted byte array to string--- 
    int i = 0; 
    System.Text.StringBuilder s = new System.Text.StringBuilder(); 
    for (i = 0; i <= EncryptedStr.Length - 1; i++) { 
     //Console.WriteLine(EncryptedStr(i)) 
     if (i != EncryptedStr.Length - 1) { 
      s.Append(EncryptedStr[i] + " "); 
     } else { 
      s.Append(EncryptedStr[i]); 
     } 
    } 

    return s.ToString(); 
    } catch (Exception err) { 
    Interaction.MsgBox(err.ToString()); 
    } 
} 

public string RSADecrypt(string str, string privateKey) 
{ 
    try { 
    //---Creates a new instance of RSACryptoServiceProvider--- 
    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); 
    //---Loads the private key--- 
    RSA.FromXmlString(privateKey); 

    //---Decrypts the string--- 
    byte[] DecryptedStr = RSA.Decrypt(HexToByteArr(str), false); 
    //---Converts the decrypted byte array to string--- 
    System.Text.StringBuilder s = new System.Text.StringBuilder(); 
    int i = 0; 
    for (i = 0; i <= DecryptedStr.Length - 1; i++) { 
     //Console.WriteLine(DecryptedStr(i)) 
     s.Append(System.Convert.ToChar(DecryptedStr[i])); 
    } 
    //Console.WriteLine(s) 
    return s.ToString(); 
    } catch (Exception err) { 
    Interaction.MsgBox(err.ToString()); 
    } 
} 
#endregion 

公共密鑰(ARG)應該是這樣的: <RSAKeyValue> <模> yNi8BvATA77f +/6cU6z [...] 9VULgU = < /模> <指數> AQAB < /指數> </RSAKeyValue >

私鑰(arg)應該如下所示: <RSAKeyValue> <模> yNi8BvATA77f +/6cU6z [...] 9VULgU = < /模> <指數> AQAB < /指數> <P> 8ZlZPmko3sam9pvD /升[...] ba0MWLjj9dyUMvmTQ6L8m9IQ == </P > <Q> 1NGHjXyEa9SjUwY [...] v + op2YyyglMeK/Gt5SL0v6xqQZQ == </Q > <DP> LpjE/aSKnWzzBt1E [...] i5f63Ak9wVG3ZPnwVDwefNkMAQ == </DP > <DQ> qAgb8AGNiJom [...] 8x3qaD3wx + UbnM5v3aE5Q == </DQ > <InverseQ> fQ4 + 7r3Nmgvz113L [...] uJqEgCNzw == </InverseQ > <d> B4n7JNeGHzHe/nqEK [... ] GaOBtuz0QTgE = </d > </RSAKeyValue >

+0

酷,我可以使用AsymmetricAlgorithm.ToXmlString(布爾)將密鑰轉換爲XML。謝謝。 – 2010-07-08 18:35:48

+0

-1,對不起,這個例子已經足夠糟糕了,以致未來的人誤入歧途。不需要專門化字符串,不需要專門化ASCII字符串,也不需要解密從其加密方法輸出的格式,並且包含未指定的方法HexToByteArr()。 – 2010-07-09 00:19:04

+0

@gregs,你有點挑剔好友。幾年前,我建立了這兩個函數,因爲它比字節[]更容易加密/解密(ascii)字符串。它展示了這個概念。將Hex轉換爲byte []非常重要,所以我沒有在這裏包含不相關的代碼。快速搜索「HexToBytes」可以得到很多例子。 – tgolisch 2010-07-09 16:10:20