问题描述
我正在开发一个加密通道来加密两个设备之间的通信.所以我正在创建一个帮助类来进行加密和解密.我在谷歌上搜索了很多,找到了一段可以将 RSA 公钥解析为 RSACryptoServiceProvider
的代码.
I'm working on an encryption channel to encrypt the communication between two devices. So I'm creating a helper class to do the encryption and decryption. I've googled a lot and found a piece of code that can parse RSA Public Key Into RSACryptoServiceProvider
.
这是代码:
public static RSACryptoServiceProvider DecodeX509PublicKey(byte[] x509key)
{
byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
byte[] seq = new byte[15];
MemoryStream mem = new MemoryStream(x509key);
BinaryReader binr = new BinaryReader(mem);
byte bt = 0;
ushort twobytes = 0;
try
{
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130)
binr.ReadByte();
else if (twobytes == 0x8230)
binr.ReadInt16();
else
return null;
seq = binr.ReadBytes(15);
if (!CompareBytearrays(seq, SeqOID))
return null;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8103)
binr.ReadByte();
else if (twobytes == 0x8203)
binr.ReadInt16();
else
return null;
bt = binr.ReadByte();
if (bt != 0x00)
return null;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130)
binr.ReadByte();
else if (twobytes == 0x8230)
binr.ReadInt16();
else
return null;
twobytes = binr.ReadUInt16();
byte lowbyte = 0x00;
byte highbyte = 0x00;
if (twobytes == 0x8102)
lowbyte = binr.ReadByte();
else if (twobytes == 0x8202)
{
highbyte = binr.ReadByte();
lowbyte = binr.ReadByte();
}
else
return null;
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
int modsize = BitConverter.ToInt32(modint, 0);
byte firstbyte = binr.ReadByte();
binr.BaseStream.Seek(-1, SeekOrigin.Current);
if (firstbyte == 0x00)
{
binr.ReadByte();
modsize -= 1;
}
byte[] modulus = binr.ReadBytes(modsize);
if (binr.ReadByte() != 0x02)
return null;
int expbytes = (int)binr.ReadByte();
byte[] exponent = binr.ReadBytes(expbytes);
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters RSAKeyInfo = new RSAParameters();
RSAKeyInfo.Modulus = modulus;
RSAKeyInfo.Exponent = exponent;
RSA.ImportParameters(RSAKeyInfo);
return RSA;
}
catch (Exception)
{
return null;
}
finally { binr.Close(); }
}
它与公钥一起工作得很棒.但我的问题是如何解析 X.509 私钥?我不太熟悉 RSA 密钥的结构.
It works awesome with a public key. But my question is how can I parse a X.509 private key? I'm not really familiar with structure of the RSA key.
密钥是从 node.js
提前致谢.
推荐答案
您当前有一个可运行的 RSAPublicKey 结构阅读器.该结构和 RSAPrivateKey 结构可以在(RFC 3447 附录 A.1)[https://www.rfc-editor.org/rfc/rfc3447#appendix-A] 中找到.
You currently have a functioning reader for the RSAPublicKey structure. That structure, and the RSAPrivateKey structure, can be found in (RFC 3447 Appendix A.1)[https://www.rfc-editor.org/rfc/rfc3447#appendix-A].
.NET 不支持多质数";(超过 2 个)RSA(但其他任何人都没有),因此您可以坚持使用版本 0 格式.字段名称应与注释中的替代名称清楚,但如果不清楚:
.NET does not support "multi-prime" (more than 2) RSA (but neither does anyone else), so you can stick with the version 0 format. The field names should be clear from the alternative names in the comments, but if it is unclear:
模数
->模数
publicExponent
->指数
privateExponent
->D
prime1
->P
prime2
->Q
exponent1
->DP
exponent2
->DQ
系数
->逆Q
modulus
->Modulus
publicExponent
->Exponent
privateExponent
->D
prime1
->P
prime2
->Q
exponent1
->DP
exponent2
->DQ
coefficient
->InverseQ
您还需要在值中添加(或删除)填充零(在左侧),以便
You will also need to add (or remove) padding zeros (on the left) to the values such that
- D.Length == Modulus.Length
hm
= (Modulus.Length + 1)/2//半舍入- P、Q、DP、DQ、InverseQ 每个都有 Length == hm.
- D.Length == Modulus.Length
hm
= (Modulus.Length + 1) / 2 // half round up- P, Q, DP, DQ, InverseQ each have Length == hm.
这篇关于如何在 C# 中解析(转换为 RSAParameters)X.509 私钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!