阅读PEM文件,得到DQ参数63个字节字节、参数、文件、PEM

2023-09-06 19:33:37 作者:你长得真玩命

我有需要的RSA加密/解密功能。随着提供的.pem文件,它包含对关键RSA。通过使用Org.BouncyCastle作为同如何阅读PEM RSA从.NET 私钥;或者使用方法 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?

推荐答案

所有的RSA参数只是(巨大)的数字。他们中的大多数都将具有相同的尺寸(对于同一个密钥对的长度,如1024),但是这并非总是如此 - 你需要在你的code包括这

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.

为什么呢?因为有些数字将是一个有点小,将适合的比特数少于其他人。你的情况的数量在63个字节的配合,所以它的base64(PEM)EN codeD本身。

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.

解决的办法是垫您正在阅读的数据,即增加63之前,你解码为0x00字节。这将仍然是相同的数字(数学明智的),它会通过所有.NET验证的RSA密钥参数。

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。你可以看看单源$ C ​​$ C,看看它是如何处理的。

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