开发者

read pem file, get 63 bytes in DQ parameter

开发者 https://www.devze.com 2023-03-19 12:12 出处:网络
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 usi

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 d开发者_JS百科ata" with parameter DQ has 63 byte (different from 64 byte). (the .pem file is said that been fine.) Is there any problem here?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号