使用RSA C#中的AsymmetricAlgorithm私钥和公钥

4

我有两个 AsymmetricAlgorithm 对象,其中包含 RSA 私钥和 RSA 公钥。私钥是从 Windows-MY keystore 中检索出来的,而公钥是从用户证书中获取的。如何在 C# 中使用这些密钥以及 RSACryptoServiceProvider 来使用 RSA 算法加密数据?换句话说,我该如何指定要使用已经拥有的密钥?


当您包含有关这两个对象的一些详细信息时,您可以获得更好的答案。 - H H
它们是我从用户证书中提取的匹配RSA密钥,还有什么需要说的呢? - Petey B
1个回答

3
#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> <Modulus>yNi8BvATA77f+/6cU6z[...]9VULgU=</Modulus> <Exponent>AQAB</Exponent> </RSAKeyValue>
私钥(arg)应该长这样: <RSAKeyValue> <Modulus>yNi8BvATA77f+/6cU6z[...]9VULgU=</Modulus> <Exponent>AQAB</Exponent> <P>8ZlZPmko3sam9pvD/l[...]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>

酷,我可以使用AsymmetricAlgorithm.ToXmlString(bool)将密钥转换为XML。谢谢。 - Petey B
-1,抱歉,这个例子糟糕到可能会误导未来的人。没有必要专门针对字符串进行特殊化处理,更不需要专门针对ASCII字符串进行特殊化处理,它也无法解密其加密方法输出的格式,并且包含一个未指定的HexToByteArr()方法。 - President James K. Polk
@gregs,你有点挑剔啊朋友。我几年前构建了这两个函数,因为加密/解密(ascii)字符串比byte[]更容易。它展示了这个概念。将十六进制转换为byte[]非常基础,所以我没有在这里包含无关的代码。快速搜索“HexToBytes”会得到很多例子。 - tgolisch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接