简体   繁体   English

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

[英]Using AsymmetricAlgorithm Private and Public key with RSA C#

I have two AsymmetricAlgorithm objects that contain an RSA Private and RSA Public key. 我有两个包含RSA私钥和RSA公钥的AsymmetricAlgorithm对象。 The private key was retrieved out of the Windows-MY keystore and the Public key from a user's certificate. 私钥是从Windows-MY密钥库中检索出来的,而私钥是从用户的证书中检索出来的。 How can I use these keys along with RSACryptoServiceProvider to encrypt data using the RSA algorithm in C#? 如何在C#中使用RSA算法将这些密钥与RSACryptoServiceProvider一起使用来加密数据? In other words, how can I specify that I want to use keys that I already have? 换句话说,如何指定我要使用已经拥有的键?

#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 

The Public Key (arg) should look like this: <RSAKeyValue> <Modulus>yNi8BvATA77f+/6cU6z[...]9VULgU=</Modulus> <Exponent>AQAB</Exponent> </RSAKeyValue> 公钥(arg)应该如下所示:<RSAKeyValue> <Modulus> yNi8BvATA77f + / 6cU6z [...] 9VULgU = </ Modulus> <Exponent> AQAB </ Exponent> </ RSAKeyValue>

The Private Key (arg) should look like this: <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> 私钥(arg)应该如下所示:<RSAKeyValue> <Modulus> yNi8BvATA77f + / 6cU6z [...] 9VULgU = </ Modulus> <Exponent> AQAB </ Exponent> <P> 8ZlZPmko3sam9pvD / l [...] ba0MWLjj9dyUMvmTQ6L8m9IQ == </ P> <Q> 1NGHjXyEa9SjUwY [...] v + op2YyyglMeK / Gt5SL0v6xqQQQQ == </ Q> <DP> LpjE / aSKnWzzBt1E [... </ 5> [N] ...] 8x3qaD3wx + UbnM5v3aE5Q == </ DQ> <InverseQ> fQ4 + 7r3Nmgvz113L [...] uJqEgCNzw == </ InverseQ> <D> B4n7JNeGHzHe / nqEK [...] GaOBtuz0QTgE = </ D> RSAKeyValue>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM