参数不正确。 RSA解密错误
我有一个使用RSA加密来发送rijindal键的服务器客户端系统。
这称为密钥封装。
现在,我已经发送了RSA公共钥匙,我用常规字符串测试了代码,
现在这些工作是在加密后转移钥匙的(加密的是使用字符串,所以可以吗?)。
我的rajindal键(我加密的内容)
61,87,46,105,139,250,42,153,173,72,205,42,26,36,227,156,50,160,38,95,180,237,17,183,232,37,215,136,179,75,227,204
,
1,10,38,205,13,99,127,55,139,71,212,100,221,181,175,93,84,228,31,116,190,254,220,41,64,253,135,146,128,115,174,234,52,84,220,98,34,253,167,77,24,166,104,119,133,173,199,155,236,32,107,10,7,153,253,242,197,80,136,117,177,217,153,97,65,132,29,23,42,157,206,91,183,133,34,204,143,83,13,244,120,115,59,50,196,176,8,146,90,189,195,249,171,120,96,54,85,6,234,129,166,94,255,202,76,249,153,107,146,64,221,45,50,50,237,113,138,152,85,39,113,90,215,197,235,121,23,191
现在
"###KEY191,23,121,235,197,215,90,113,39,85,152,138,113,237,50,50,45,221,64,146,107,153,249,76,202,255,94,166,129,234,6,85,54,96,120,171,249,195,189,90,146,8,176,196,50,59,115,120,244,13,83,143,204,34,133,183,91,206,157,42,23,29,132,65,97,153,217,177,117,136,80,197,242,253,153,7,10,107,32,236,155,199,173,133,119,104,166,24,77,167,253,34,98,220,84,52,234,174,115,128,146,135,253,64,41,220,254,190,116,31,228,84,93,175,181,221,100,212,71,139,55,127,99,13,205,38,10,1"
此时服务器将接收消息,并执行以下代码
if (messageReceived.StartsWith("###KEY"))
{
Console.WriteLine(messageReceived);
string tempkey = messageReceived.Remove(0, 6);
this.key = rsa.DecryptBytes(tempkey);
myRijndael.Key = key;
Console.WriteLine(tempkey);
SendMessage("keyOK");
}
温度键将是
191,23,121,235,197,215,90,113,39,85,152,138,113,237,50,50,45,221,64,146,107,153,249,76,202,255,94,166,129,234,6,85,54,96,120,171,249,195,189,90,146,8,176,196,50,59,115,120,244,13,83,143,204,34,133,183,91,206,157,42,23,29,132,65,97,153,217,177,117,136,80,197,242,253,153,7,10,107,32,236,155,199,173,133,119,104,166,24,77,167,253,34,98,220,84,52,234,174,115,128,146,135,253,64,41,220,254,190,116,31,228,84,93,175,181,221,100,212,71,139,55,127,99,13,205,38,10,1
好的。
进入接收一个名为数据的字符串并返回byte []
try
{
var dataArray = data.Split(',');
byte[] dataByte = new byte[dataArray.Length];
for (int i = 0; i < dataArray.Length; i++)
{
dataByte[i] = Convert.ToByte(dataArray[i]);
}
_rsa.FromXmlString(_privateKey);
var decryptedByte = _rsa.Decrypt(dataByte, false);
return decryptedByte;
}
catch (Exception e)
{
Console.WriteLine(e);
}
return new byte[7];
rsa类看起来像这样
public RSA()
{
_encoder = new UnicodeEncoding();
_rsa = new RSACryptoServiceProvider();
_privateKey = _rsa.ToXmlString(true);
_publicKey = _rsa.ToXmlString(false);
}
我没有问题的转换零件,而数据级将是AKA是AKA的键发送。
我的问题是在以下行中,
var decryptedByte = _rsa.Decrypt(dataByte, false);
这使我备份了例外{“参数不正确。”}
现在我在线搜索并找到了一些说明。
1-钥匙已更改 2-我需要将false
更改为true
(无效)。
让我知道是否需要更多代码
I have a server client system which uses rsa encryption to send rijindal keys.
this is called key encapsulation.
right now I already sent the RSA public keys, I tested the code with regular string and those work
now I'm transferring the keys after being encrypted(encrypted is working with strings so its ok?).
my rajindal key(what I encrypt)
61,87,46,105,139,250,42,153,173,72,205,42,26,36,227,156,50,160,38,95,180,237,17,183,232,37,215,136,179,75,227,204
what comes out
1,10,38,205,13,99,127,55,139,71,212,100,221,181,175,93,84,228,31,116,190,254,220,41,64,253,135,146,128,115,174,234,52,84,220,98,34,253,167,77,24,166,104,119,133,173,199,155,236,32,107,10,7,153,253,242,197,80,136,117,177,217,153,97,65,132,29,23,42,157,206,91,183,133,34,204,143,83,13,244,120,115,59,50,196,176,8,146,90,189,195,249,171,120,96,54,85,6,234,129,166,94,255,202,76,249,153,107,146,64,221,45,50,50,237,113,138,152,85,39,113,90,215,197,235,121,23,191
what is sent to the server
"###KEY191,23,121,235,197,215,90,113,39,85,152,138,113,237,50,50,45,221,64,146,107,153,249,76,202,255,94,166,129,234,6,85,54,96,120,171,249,195,189,90,146,8,176,196,50,59,115,120,244,13,83,143,204,34,133,183,91,206,157,42,23,29,132,65,97,153,217,177,117,136,80,197,242,253,153,7,10,107,32,236,155,199,173,133,119,104,166,24,77,167,253,34,98,220,84,52,234,174,115,128,146,135,253,64,41,220,254,190,116,31,228,84,93,175,181,221,100,212,71,139,55,127,99,13,205,38,10,1"
now at this point the server will receive the message and will execute the following code
if (messageReceived.StartsWith("###KEY"))
{
Console.WriteLine(messageReceived);
string tempkey = messageReceived.Remove(0, 6);
this.key = rsa.DecryptBytes(tempkey);
myRijndael.Key = key;
Console.WriteLine(tempkey);
SendMessage("keyOK");
}
temp key will be
191,23,121,235,197,215,90,113,39,85,152,138,113,237,50,50,45,221,64,146,107,153,249,76,202,255,94,166,129,234,6,85,54,96,120,171,249,195,189,90,146,8,176,196,50,59,115,120,244,13,83,143,204,34,133,183,91,206,157,42,23,29,132,65,97,153,217,177,117,136,80,197,242,253,153,7,10,107,32,236,155,199,173,133,119,104,166,24,77,167,253,34,98,220,84,52,234,174,115,128,146,135,253,64,41,220,254,190,116,31,228,84,93,175,181,221,100,212,71,139,55,127,99,13,205,38,10,1
which is good.
going into the decryptBytes which receives a string called data and returns a byte[]
try
{
var dataArray = data.Split(',');
byte[] dataByte = new byte[dataArray.Length];
for (int i = 0; i < dataArray.Length; i++)
{
dataByte[i] = Convert.ToByte(dataArray[i]);
}
_rsa.FromXmlString(_privateKey);
var decryptedByte = _rsa.Decrypt(dataByte, false);
return decryptedByte;
}
catch (Exception e)
{
Console.WriteLine(e);
}
return new byte[7];
RSA class looks like this
public RSA()
{
_encoder = new UnicodeEncoding();
_rsa = new RSACryptoServiceProvider();
_privateKey = _rsa.ToXmlString(true);
_publicKey = _rsa.ToXmlString(false);
}
I have no trouble with the conversion part and dataByte will be what it needs to be aka the key that was sent.
my issue is in the following line
var decryptedByte = _rsa.Decrypt(dataByte, false);
which gives me back the exception {"The parameter is incorrect."}
now I've searched online and found a few explanations.
1- the keys have changed
2- I need to change false
to true
(didn't work).
let me know if any more code is needed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于某种原因,您似乎已经扭转了密文。 RSA Ciphertext始终以相同的顺序进行编码,由PKCS#1标准(如今rfc)指定。它为RSA结果的密文编码(这是由模块化指数创建的数字)指定静态大小,无符号 大。
发送到服务器的内容应从
1
开始,而不是191
,第二个字符串和第三个字符串需要相同,除了标头。请注意,密钥未根据PKCS#1规格编码,因为Microsoft是特殊的。如果您使用其他系统,则可能需要逆转模量或指数。
请注意,通常您可以直接发送字节,许多传输协议允许进行二进制通信。如果您使用文本,则至少可以编码为64的基础,这是相对有效的,使用4个字符,每3个字节。
For some reason you seem to have reversed the ciphertext. RSA ciphertext is always encoded in the same order, specified by the PKCS#1 standard (nowadays an RFC). It specifies static sized, unsigned big endian for the ciphertext encoding of the RSA result (which is a number created by modular exponentiation).
What is being sent to the server should start with a
1
, not a191
, the 2nd and third string need to be identical except for the header.Note that keys are not encoded according to the PKCS#1 specifications, because Microsoft is special that way. You may need to reverse a modulus or an exponent if you're working with other systems.
Note that quite often you can sent the bytes directly, many transport protocols allow for binary communication. If you're using text you could at least encode to base 64, which is relatively efficient using 4 characters for each 3 bytes.