用的RSACryptoServiceProvider自己的公钥和私钥初始化 [英] RSACryptoServiceProvider initialize with own public key and private key

查看:2499
本文介绍了用的RSACryptoServiceProvider自己的公钥和私钥初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我自己的公钥和私钥来初始化的RSACryptoServiceProvider。



据我研究,要做到这一点的方法是调用构造函数以

 的RSACryptoServiceProvider RSAalg =新的RSACryptoServiceProvider(cspParams); 

如上所示

cspParams。然而,当我看看MSDN的例子就使用它:
http://msdn.microsoft.com/en-us/library/ca5htw4f.aspx



我不明白他们设置专用的任何地方或公共密钥。仅使用KEYCONTAINER。当我没有cspParam创建的RSACryptoServiceProvider,那么它是默认设置为只使用公共密钥。我注意到这一点,当我检查类本身的PublicOnly变量,它是一个只读变量。



我的问题是我怎么初始化这个类,然后建立我自己的私钥和公钥。
中的服务器将使用私有密钥和客户端将有公共密钥。



我发现了什么是创建一个RSAParameter对象并设置就可以了.Exponent和.Modulus参数分别为公共和私有变量。



但我发现了一个丢失密码错误,因为我相信的RSACryptoServiceProvider没有用正确的构造进行初始化。



下面是我的一些代码。不要担心的BigInteger类,它只是一个实验。即使我使用与否,我得到了同样的错误。

  //创建一个UnicodeEncoder到字节数组和字符串之间的转换。 
UnicodeEncoding ByteConverter =新UnicodeEncoding();

字节[] = dataToEncrypt ByteConverter.GetBytes(密码);
字节[]的EncryptedData;
字节[] decryptedData;

//的RSACryptoServiceProvider RSA =新的RSACryptoServiceProvider();
RSAParameters RSAP =新RSAParameters();

BigInteger的N =新的BigInteger(19579160939939334264971282204525611731944172893619019759209712156289528980860378672033164235760825723282900348193871051950190013953658941960463089031452404364269503721476236241284015792700835264262839734314564696723261501877759107784604657504350348081273959965406686529089170062268136253938904906635532824296510859016002105655690559115059267476786307037941751235763572931501055146976797606538425089134251611194500570922973015579287289778637105402129208324300035518642730384616767241853993887666288072512402523498267733725021939287517009966986976768028023180137546958580922532786773172365428677544232641888174470601681,10);

BigInteger的E =新的BigInteger(65537,10);

//rsap.Modulus = ByteConverter.GetBytes(公钥);
rsap.Exponent = e.getBytes();
rsap.Modulus = n.getBytes();
/*rsap.Exponent = ByteConverter.GetBytes(公钥);
rsap.D = ByteConverter.GetBytes(公钥);
rsap.DP = ByteConverter.GetBytes(公钥);
rsap.DQ = ByteConverter.GetBytes(公钥);
rsap.P = ByteConverter.GetBytes(公钥);
rsap.Q = ByteConverter.GetBytes(公钥);
rsap.InverseQ = ByteConverter.GetBytes(公钥);使用(RSA的RSACryptoServiceProvider =新的RSACryptoServiceProvider())
{
* /

//RSA.PublicOnly =假;

RSA.ImportParameters(RSAP);

的debug.log(PublicOnly:+ RSA.PublicOnly);

的debug.log(rsap.Modulus.Length);
//Debug.Log(RSA.ToString());
//传递给加密,公钥信息
//(使用RSACryptoServiceProvider.ExportParameters(假),
//一个布尔标志没有指定OAEP冗余
中的数据// =的EncryptedData RSACSPSample.RSAEncrypt(dataToEncrypt,RSAP,FALSE);
的EncryptedData = RSACSPSample.RSAEncrypt(dataToEncrypt,RSA.ExportParameters(假),FALSE);


调试的.log(的EncryptedData:+的EncryptedData);
//显示解密后的明文到控制台
//Debug.Log(\"Decrypted明文:+ ByteConverter.GetString());

//传递数据解密,私钥信息
//(使用RSACryptoServiceProvider.ExportParameters(真),
//一个布尔标志,指定没有OAEP填充。
decryptedData = RSACSPSample.RSADecrypt(的EncryptedData,RSA.ExportParameters(真),FALSE);
}


//的EncryptedData = RSACSPSample.RSAEncrypt(dataToEncrypt,RSAP ,FALSE);

//如果(的EncryptedData!= NULL){
密码= ByteConverter.GetString(decryptedData);
//}


解决方案

字段是可怕的命名,这是混淆了你。在指数字段是真正的的公共的一个公共密钥指数。在私人的指数为私钥是 D 领域。



这不是你的错,MSDN文档吮吸。


I'm trying to initialize RSACryptoServiceProvider with my own public and private keys.

As far as I could research, the way to do this is to call the constructor with

RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams);

cspParams as shown above. However, when I look at the msdn example on the use of it: http://msdn.microsoft.com/en-us/library/ca5htw4f.aspx

I don't see any place where they set the private or public keys. Only using a KeyContainer. When I create an RSACryptoServiceProvider without a cspParam, then it is by default set to only use a Public key. I notice this when I check the PublicOnly variable on the class itself and it is a read only variable.

My question is how do I initialize this class and then set my own private and public keys. The server will be using the private key and the client will have the public key.

What I found out is that creating an RSAParameter object and setting the .Exponent and .Modulus parameters on it as the public and private variables respectively.

But I'm getting a "Missing Private Key" error since I believe the RSACryptoServiceProvider isn't initialized with the correct constructor.

Below is some of my code. Don't worry about BigInteger class, it's just an experiment. Even if I use it or not, I get the same error.

//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);
//}

解决方案

The fields are horribly named and it is confusing you. The Exponent field is really the public exponent for a public key. The private exponent for a private key is the D field.

It is not your fault that MSDN documentation sucks.

这篇关于用的RSACryptoServiceProvider自己的公钥和私钥初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆