C# RSACryptServiceProvider使用自己的公钥和私钥初始化

C# RSACryptServiceProvider使用自己的公钥和私钥初始化,c#,cryptography,rsacryptoserviceprovider,C#,Cryptography,Rsacryptoserviceprovider,我正在尝试使用自己的公钥和私钥初始化RSACryptServiceProvider 据我所知,实现这一点的方法是使用 RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams); cspParams如上所示。但是,当我查看msdn示例时,会发现: 我没有看到他们在哪里设置私钥或公钥。仅使用密钥容器。当我创建一个没有CSParam的RSACryptoServiceProvider时,默认情况下它被设置为仅使

我正在尝试使用自己的公钥和私钥初始化RSACryptServiceProvider

据我所知,实现这一点的方法是使用

RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams);
cspParams如上所示。但是,当我查看msdn示例时,会发现:

我没有看到他们在哪里设置私钥或公钥。仅使用密钥容器。当我创建一个没有CSParam的RSACryptoServiceProvider时,默认情况下它被设置为仅使用公钥。我在检查类本身的PublicOnly变量时注意到了这一点,它是一个只读变量

我的问题是如何初始化这个类,然后设置自己的私钥和公钥。 服务器将使用私钥,客户端将使用公钥。

我发现创建一个rsapameter对象并将其上的.index和.module参数分别设置为公共变量和私有变量

但是我得到了一个“缺少私钥”错误,因为我认为RSACryptServiceProvider没有用正确的构造函数初始化

下面是我的一些代码。不要担心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);
//}

这些字段的名称非常糟糕,让您感到困惑。
指数
字段实际上是公钥的公共指数。私钥的私钥指数是字段


MSDN文档糟糕透了,这不是你的错。

你必须转换base 64:

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

公钥由RSA参数的指数和模定义。私钥,在RSAPERAMETERS类中是D。那么,如何设置RSAPERAMETERS的公钥呢?我已经有了我的公钥,只需要设置rsapameter对象,这样我就可以使用它。这取决于你是否有一个公钥和一个私钥要使用,如果没有,那么你可以使用csparams来创建一对。我有,你能告诉我如何使用它们吗?是的,这些变量的名称非常糟糕。让我试试,然后再打给你。谢谢现在,我得到一个丢失的模错误。知道那是什么意思吗?或者我需要将模数变量设置为什么?这是否意味着我不需要用CSParams初始化?模数是。。。模量。如果你看一个结构,你的RSA公钥只有模和指数字段,而RSA私钥有所有的字段。我没有用这种方式完成我需要做的事情(我使用了Bouncy Castle),但你是对的。我确实设法用msdn的RSACryptoServiceProvider进行加密和解密。