使用 .NET 加密库从 c# 中的 .pem 文件使用私钥解密
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1162504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Decrypting with private key from .pem file in c# with .NET crypto library
提问by Tim Jarvis
I know this is a similar question to this onebut before I head down the Bouncey Castle route, does anyone know if its possible to load an RSA KeyPair from a .pem file, e.g.:
我知道这是一个类似的问题这一个,但之前,我低着头Bouncey城堡路线,没有人知道如果可能从.PEM文件,例如加载RSA密钥:
-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBALKzy66nRuof8Fg0ItatyHS9RiDIKH0m5lorKzKn4y5wR6BXpVUv
ZwnevrAJWBd6EPr/lcV3hjObxD6+q9vmN8ECAwEAAQJAGNcxWwfZrbXe3QPyS9FA
aindU7U/G5aKssIJcTMxO0UYpGU+WArJbboKeEIE7bpNfhDOKTL7ZL6kWBR1Svlh
WQIhAOhtx+xXuSrIot59tmXZaypBDjA4n+Xare0ObFLQxWuvAiEAxNMwm6w33bVr
FHS9slkOh59Le2mgs0uNT6perHaRP48CIGMyRzrlDY/m5SvTtz6slgIIlceawxNU
Sxp7J1wI4djdAiA6+BchHNjkCP2a9Fr9OydaRMSFpiDqduFQk/enbiKYSwIhANO3
SQ51oLFtWN9gX3tfKTXflyO6BV8rgPo980d9CEsb
-----END RSA PRIVATE KEY-----
directly with the .NET 3.5 crypto library without having to go to a 3rd party or roll my own?
直接使用 .NET 3.5 加密库,而不必去第三方或自己动手?
采纳答案by Stobor
http://www.jensign.com/opensslkey/index.html
http://www.jensign.com/opensslkey/index.html
with source at http://www.jensign.com/opensslkey/opensslkey.csUpdate: Source code is no longer available at this url. It can be found at https://gist.github.com/stormwild/7887264or https://web.archive.org/web/20170731015547/http://www.jensign.com/opensslkey/opensslkey.csnow.
源代码位于http://www.jensign.com/opensslkey/opensslkey.cs更新:此 URL 不再提供源代码。现在可以在https://gist.github.com/stormwild/7887264或https://web.archive.org/web/20170731015547/http://www.jensign.com/opensslkey/opensslkey.cs找到它。
edit: excerpted relevant code:
编辑:摘录相关代码:
first, extract the text between the ---- BEGIN ---- and ---- END ---- sections, and base64-decode it into a byte array (see link above for details), then pass it to:
首先,提取 ---- BEGIN ---- 和 ---- END ---- 部分之间的文本,并将其 base64 解码为字节数组(有关详细信息,请参见上面的链接),然后将其传递给:
//------- Parses binary ans.1 RSA private key; returns RSACryptoServiceProvider ---
public static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)
{
byte[] MODULUS, E, D, P, Q, DP, DQ, IQ ;
// --------- Set up stream to decode the asn.1 encoded RSA private key ------
MemoryStream mem = new MemoryStream(privkey) ;
BinaryReader binr = new BinaryReader(mem) ; //wrap Memory Stream with BinaryReader for easy reading
byte bt = 0;
ushort twobytes = 0;
int elems = 0;
try {
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == 0x8230)
binr.ReadInt16(); //advance 2 bytes
else
return null;
twobytes = binr.ReadUInt16();
if (twobytes != 0x0102) //version number
return null;
bt = binr.ReadByte();
if (bt !=0x00)
return null;
//------ all private key components are Integer sequences ----
elems = GetIntegerSize(binr);
MODULUS = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
E = binr.ReadBytes(elems) ;
elems = GetIntegerSize(binr);
D = binr.ReadBytes(elems) ;
elems = GetIntegerSize(binr);
P = binr.ReadBytes(elems) ;
elems = GetIntegerSize(binr);
Q = binr.ReadBytes(elems) ;
elems = GetIntegerSize(binr);
DP = binr.ReadBytes(elems) ;
elems = GetIntegerSize(binr);
DQ = binr.ReadBytes(elems) ;
elems = GetIntegerSize(binr);
IQ = binr.ReadBytes(elems) ;
Console.WriteLine("showing components ..");
if (verbose) {
showBytes("\nModulus", MODULUS) ;
showBytes("\nExponent", E);
showBytes("\nD", D);
showBytes("\nP", P);
showBytes("\nQ", Q);
showBytes("\nDP", DP);
showBytes("\nDQ", DQ);
showBytes("\nIQ", IQ);
}
// ------- create RSACryptoServiceProvider instance and initialize with public key -----
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters RSAparams = new RSAParameters();
RSAparams.Modulus =MODULUS;
RSAparams.Exponent = E;
RSAparams.D = D;
RSAparams.P = P;
RSAparams.Q = Q;
RSAparams.DP = DP;
RSAparams.DQ = DQ;
RSAparams.InverseQ = IQ;
RSA.ImportParameters(RSAparams);
return RSA;
}
catch (Exception) {
return null;
}
finally {
binr.Close();
}
}
回答by Stef Heyenrath
I've created a small helper NuGet package to create a X509 certificate based on public key and private (rsa) key.
我创建了一个小的帮助程序 NuGet 包来创建基于公钥和私钥 (rsa) 密钥的 X509 证书。
See NuGetand Github-projectfor functionality and code-examples based on opensslkey.
有关基于opensslkey 的功能和代码示例,请参阅NuGet和Github-project。