RSA 导入手动 RSAParams 因错误数据错误而失败 [英] RSA Import manual RSAParams failing with bad data error

查看:37
本文介绍了RSA 导入手动 RSAParams 因错误数据错误而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整个上午都在这样做,但仍然无法弄清楚我错过了什么.

I have been at this all morning and still can't figure out what I am missing.

我正在尝试使用我自己生成的值执行 RSA 加密.有几个关于此的 SO 帖子,其中几个让我走得更远.其中一些帖子让 OP 放弃并使用 BouncyCastle 或其他东西,而有答案和评论说它确实有效.所以我决定学习他们的知识并让 .Net RSACryptoServiceProvider 工作.

I am trying to perform RSA encryption using my own generated values. There are several SO post on this and several have gotten me further down the path. Some of those same post have the OP giving up and using BouncyCastle or something else while there are answers and comments saying it does work. So I'm kinda determined to learn what they know and get the .Net RSACryptoServiceProvider working.

为此,我在这里有一个简单的小示例,但我什至无法导入 RSAParameters.

So to that end I have a simple little sample here and I can't even get the RSAParameters to import.

我还没有发布用于生成和测试素数或我的 e 值的代码.如果需要,我可以,但我认为这不是故障所在,因为那是非常好的股票代码.

I haven't posted the code used to generate and test primality or my e value. I can if needed but I don't think that is where the breakdown is as that is pretty stock code.

BigInteger p = BigInteger.Parse("17234...948417"); //Large Prime A
BigInteger q = BigInteger.Parse("16750...157759"); //Large Prime B
BigInteger n = BigInteger.Multiply(p, q); //Modulus
BigInteger e = BigInteger.Parse("16168...372355");

RSAParameters _rsaParams new RSAParameters();
_rsaParams.Modulus = n.ToByteArray();
_rsaParams.Exponent = e.ToByteArray();

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(_rsaParms);

当然也有例外.

好的,所以根据@Iridium 的回答,我加倍考虑了我计算公共指数的方式,但我确实做错了,因此得出了太大的公共指数.在我对正确方法的研究中,我发现这个非常有用的网站可以计算指数,我可以通过它来检查自己并找到我的错误.

OK so based upon @Iridium's answer I doubled down on how I am calculating the public exponent and I was indeed doing it wrong thus coming up with too large of a public exponent. In my research for the correct way I found this very helpful site to calculate the exponent by which I was able to check myself and find my error.

http://nmichaels.org/rsa.py

推荐答案

我记得,RSACryptoServiceProvider 不接受大于 4 的导入参数中的 Exponent字节(即使字节数组表示的数字适合较小的字段,即通过添加前导零字节).您的指数似乎是 129 字节,显然超出了此限制.

As I recall, the RSACryptoServiceProvider doesn't accept an Exponent in the imported parameters greater than 4 bytes (even if the number represented by the byte array would fit in a smaller field i.e. by adding leading zero bytes). Your exponent appears to be 129 bytes, which obviously exceeds this limit.

如果您使用的指数实际上是私有指数,那么让它与 .NET RSACryptoServiceProvider 一起工作的唯一方法是填写其余字段(PQDPDQInverseQ),并设置Exponent 到通常小得多的公共指数.

If the exponent you're using is in fact the private exponent, then the only way to get it to work with the .NET RSACryptoServiceProvider will be to fill in the remainder of the fields (P, Q, DP, DQ, InverseQ) correctly, and set the Exponent to the usually much smaller public exponent.

这篇关于RSA 导入手动 RSAParams 因错误数据错误而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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