如何将私钥导入c#RSAParameters对象?

时间:2015-10-09 21:34:16

标签: c# encryption rsa

我想用rsa加密和解密c#中的数据。我写了这段代码,它适用于加密,但我有解密问题。我的问题是什么?!

这是我的代码:

using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace Cry
{
    public class CryptoUtils
    {
        public CryptoUtils ()
        {    
            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
            byte[] encryptedData;
            byte[] decryptedData;
            string decrptedStr = "";

            var pub = Convert.FromBase64String("MCgCIQCfkl4xV5T/v3r1bifOc1mVHa9yak5pGjUfAv0r+s6+AwIDAQAB");
            var prv = Convert.FromBase64String("MIGsAgEAAiEAn5JeMVeU/7969W4nznNZlR2vcmpOaRo1HwL9K/rOvgMCAwEAAQIg\nMce6pM/6xpIYrMoxluE7JBkVe9Sme9d6NPPJJX3NyBECEgCmwIarl1hSBnTqZNwJ\n8hZhqwIQAPT6CO/l/ma1sDi7eM7tCQISAKH90lYLlr9IinfSN3hp95g1AhAAlyNf\nuioqX1G+y/GVogyJAhEmQQB52juSQ574HnampzXUpQ==");

            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
                RSAParameters myRSAParameters = RSA.ExportParameters(false);
                myRSAParameters.Modulus = pub;
                myRSAParameters.Exponent = ByteConverter.GetBytes("65537");
                myRSAParameters.D = prv;
                encryptedData = RSAEncrypt(dataToEncrypt, myRSAParameters, false);
                decryptedData = RSADecrypt(encryptedData, myRSAParameters, false);
                decrptedStr = ByteConverter.GetString(decryptedData);
            }
        }

        static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) {
            try {
                byte[] encryptedData;
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
                    RSA.ImportParameters(RSAKeyInfo);
                    encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
                }
                return encryptedData;
            } catch (CryptographicException e) {
                Console.WriteLine(e.Message);

                return null;
            }

        }

        static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) {
            try {
                byte[] decryptedData;
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
                    RSA.ImportParameters(RSAKeyInfo);
                    decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
                }
                return decryptedData;
            } catch (CryptographicException e) {
                Console.WriteLine(e.ToString());

                return null;
            }

        }
    }
}

在行encryptedData = RSAEncrypt(dataToEncrypt, myRSAParameters, false);数据加密,但在下一行我有这个错误:

System.Security.Cryptography.CryptographicException: PKCS1 decoding error.
  at System.Security.Cryptography.RSAPKCS1KeyExchangeDeformatter.DecryptKeyExchange (System.Byte[] rgbIn) [0x00000] in <filename unknown>:0 
  at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt (System.Byte[] rgb, Boolean fOAEP) [0x00000] in <filename unknown>:0

我认为我确实填写了myRSAParameters属性,但我不知道如何填写它!

1 个答案:

答案 0 :(得分:2)

在.NET中,如果指定RSAParameters.D,则还必须为P,Q,DP,DQ和InverseQ指定(更正)值。

虽然模数,指数和D都是技术上需要进行RSA所需的,但很少有实现使用D,因为RSA via the Chinese Remainder Theorem的效率要高得多。

例如,OpenSSL将回归到只使用D; .NET不会。