读取pem文件,获取DQ参数中的63字节

发布于 2024-11-19 04:04:03 字数 415 浏览 6 评论 0原文

我有一个需要 RSA 加密/解密的函数。使用提供的 .pem 文件,其中包含 RSA 的配对密钥。通过使用 Org.BouncyCastle 与 如何读取来自.NET的PEM RSA私钥;或使用 http://www.jensign.com/opensslkey/opensslkey.cs。我收到的是“坏数据”,参数 DQ 有 63 字节(与 64 字节不同)。 (据说.pem文件没问题。)这里有什么问题吗?

I have a function that need RSA encryption/decryption. With provided .pem file, that contains pair key for RSA. By using Org.BouncyCastle as same as How to read a PEM RSA private key from .NET; or using the method in http://www.jensign.com/opensslkey/opensslkey.cs. What i received is "bad data" with parameter DQ has 63 byte (different from 64 byte). (the .pem file is said that been fine.) Is there any problem here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈下烟灰 2024-11-26 04:04:03

所有 RSA 参数都是简单的(巨大的)数字。它们中的大多数将具有相同的大小(对于相同的密钥对长度,例如 1024),但情况并非总是如此 - 您需要在代码中涵盖这一点。

为什么 ?因为有些数字会比其他数字小一些,并且适合的位数也较少。在您的情况下,该数字适合 63 字节,因此它是 base64 (PEM) 编码的。

解决方案是填充您正在读取的数据,即在您正在解码的 63 之前添加一个 0x00 字节。这仍然是相同的数字(数学方面),并且它将通过 RSA 密钥参数的所有 .NET 验证。

ps你可以查看Mono源代码来看看这是如何处理的。

All RSA parameters are simply (huge) numbers. Most of them will have identical sizes (for the same key pair length, e.g. 1024) but this is not always the case - and you need to cover this in your code.

Why ? because some numbers will be a bit smaller and will fit in less bits than others. In your case the number fits in 63 bytes, so it's base64 (PEM) encoded as such.

The solution is to pad the data you're reading, i.e. add a 0x00 byte before the 63 you're decoding. This will still be the same numbers (math wise) and it will pass all .NET validations for the RSA key parameters.

p.s. you can look at Mono source code to see how this is handled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文