RSA密钥生成问题 [英] RSA keys generation problem
问题描述
嗨
我正在尝试使用RSA加密和解密某些数据.我在创建自己的公钥和私钥并使用它们时遇到问题.这是我到目前为止所做的
我创建了RSACryptoServiceProvider和CspParameters,因为我将密钥存储在密钥容器中:
Hi
I''m trying to encrypt and decrypt some data with RSA. I''m having problems in creating my own public and private keys and use them. This is what I did so far
I create an RSACryptoServiceProvider and a CspParameters because Im storing my keys into a key container:
CspParameters cp = new CspParameters();
cp.KeyContainerName = stContainerName; //stContainerName is a string
// 256 bytes RSA key pair
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048, cp);
然后,我以此检索公共RSA密钥.我使用XmlDocument处理RSA密钥数据
Then I retrieve the public RSA key with this. I use a XmlDocument to handle the RSA Key Data
XmlDocument xmlDoc = new XmlDocument();
//true is for retrieve both the public and private RSA key
xmlDoc.InnerXml = rsa.ToXmlString(true);
XmlNodeList xmlPublicModulus = xmlDoc.GetElementsByTagName("Modulus");
XmlNodeList xmlPublicExp = xmlDoc.GetElementsByTagName("Exponent");
之后,我尝试加密和解密一些数据.我声明了一个新的RSACryptoServiceProvider,它将具有公共密钥信息.
After that i try to encrypt and decrypt some data. I declare a new RSACryptoServiceProvider which will have the public key info.
string stDataToEncrypt = "Hey there";
string stDeencryptedData;
byte[] btEncryptedData;
//Getting the Modulus and exp from XML Data
byte[] publicModulus = Encoding.ASCII.GetBytes(xmlPublicModulus[0].InnerText);
byte[] publicExp = Encoding.ASCII.GetBytes(xmlPublicExp[0].InnerText);
//Setting Up the public key
RSAParameters publicRSAKeyInfo = new RSAParameters(); publicRSAKeyInfo.Modulus = publicModulus;
publicRSAKeyInfo.Exponent = publicExp;
//Creating the Public Key - I think this is wrong...
RSACryptoServiceProvider rsaPublic = new RSACryptoServiceProvider(2048);
rsaPublic.ImportParameters(publicRSAKeyInfo);
//Encrypting data. Im assuming that if I encrypt with the
//RSACryptoServiceProvider rsa it will do it with the private key
btEncryptedData= rsa.Encrypt(Encoding.ASCII.GetBytes(stDataToEncrypt ),false);
//Decrypting data. Im using the RSACryptoServiceProvider rsaPublic, which has the
//public key data. HERE I GOT AN EXCEPTION
stDeencryptedData= Encoding.ASCII.GetString(rsaPublic.Decrypt(btDatoEncriptado,false));
Console.WriteLine("datoDesencriptado: " + datoDesencriptado);
我有一个例外:不正确的密钥.检查rsaPublic对象的参数,KeyLenght属性为2752.rsa对象中的此参数为2048.这告诉我我做错了什么.
我对RSACryptoServiceProvider对象的了解是,它创建了一个RSA密钥对.因此,如果我想给其他人公开密钥,并且想用私有密钥加密或解密,这就是我要做的事情
任何帮助将不胜感激,请原谅我的英语
问候
Iván
I got an exception: incorrect key. Cheking the parameters of the rsaPublic object, the KeyLenght property is 2752. This parameter in rsa object is 2048. This tells me that I am doing somethin wrong.
What I understand of RSACryptoServiceProvider object is that it creates a RSA Key pair. So if I want to give someone else the public key, and I want to encrypt or decrypt with the private key, this is what I have to do
Any help would be appreciated and excuse my english
Regards
Iván
推荐答案
该模式将如何工作?不!您完全错过了公钥密码术的整个想法.
读我的嘴唇::-)
您始终使用一个密钥进行加密,而使用另一个密钥进行解密! 要了解其为何以这种方式工作,请阅读以下内容: ^ ].
只需遵循爱丽丝和鲍勃之间的交换逻辑即可.
这完全基于单向功能的思想: http://en. wikipedia.org/wiki/单向功能 [^ ].
另请参阅: http://en.wikipedia.org/wiki/RSA [
How this schema would possible work?! No! You completely miss the whole idea of the Public-key Cryptography.
Read my lips: :-)
You always encrypt with one key but decrypt with another one! To understand why it works this way, please read this: http://en.wikipedia.org/wiki/Public-key_cryptography[^].
Just follow the logic of exchange between Alice and Bob.
It''s all based on the idea of one-way function: http://en.wikipedia.org/wiki/One-way_function[^].
See also: http://en.wikipedia.org/wiki/RSA[^].
—SA
我找到的解决方案
使用以下内容中提供的RSA类:
RSA私钥加密 [
The solution I found
Use the RSA classes provided in:
RSA Private Key Encryption[^]
I modified some methods in order to get the private and public keys from a strings instead of a xml file, because I want to use the .NET Key containers feature.
These are how my main methods looks:
public static void CreateRSAKeys(string stContainerName)
{
// Create the CspParameters object and set the key container
// name used to store the RSA key pair.
CspParameters cp = new CspParameters();
cp.KeyContainerName = stContainerName;
// Create a new instance of RSACryptoServiceProvider that accesses
// the key container MyKeyContainerName.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048,cp);
Console.WriteLine("RSA KEY Created: " + rsa.ToXmlString(false));
}
public static void EncryptWithPrivateKey(string stContainerName)
{
RSAEncryption myRsaPrivateEncryption = new RSAEncryption();
string stDataToEncrypt = "Hey There";
byte[] btEncryptedData;
byte[] btDeencryptedData;
string stDeencryptedData;
// Create the CspParameters object and set the key container
// name used to store the RSA key pair.
CspParameters cp = new CspParameters();
cp.KeyContainerName = stContainerName;
// Create a new instance of RSACryptoServiceProvider that accesses
// the key container MyKeyContainerName.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048, cp);
//Loading the private and the key into the MyRSA object
myRsaPrivateEncryption.LoadPrivateFromString(rsa.ToXmlString(true));
btEncryptedData = myRsaPrivateEncryption.PrivateEncryption(Encoding.ASCII.GetBytes(stDataToEncrypt));
btDeencryptedData = myRsaPrivateEncryption.PublicDecryption(btEncryptedData);
stDeencryptedData = Encoding.ASCII.GetString(btDeencryptedData );
Console.WriteLine(stDeencryptedData);
}
解决了! :)
Solved! :)
这篇关于RSA密钥生成问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!