如何从.NET读取PEM RSA私钥

我有格式的RSA私钥PEM,是否有直接的方式从.NET读取并实例化RSACryptoServiceProvider解密用相应的公钥加密的数据?



江户川乱折腾
浏览 1338回答 3
3回答

红糖糍粑

关于轻松导入RSA私钥,不使用BouncyCastle等第三方代码,我认为答案是“不,不是单独使用私钥的PEM”。但是,正如Simone所提到的,您可以简单地将私钥(* .key)的PEM和使用该密钥(* .crt)的证书文件组合成* .pfx文件,然后可以轻松导入该文件。要从命令行生成PFX文件:openssl pkcs12 -in a.crt -inkey a.key -export -out a.pfx然后正常使用.NET证书类,例如:using System.Security.Cryptography.X509Certificates;X509Certificate2 combinedCertificate = new X509Certificate2(@"C:\path\to\file.pfx");现在,您可以按照MSDN中的示例通过RSACryptoServiceProvider进行加密和解密:我遗漏了解密,你需要使用PFX密码和Exportable标志导入。(参见:BouncyCastle RSAPrivateKey到.NET RSAPrivateKey)X509KeyStorageFlags flags = X509KeyStorageFlags.Exportable;X509Certificate2 cert = new X509Certificate2("my.pfx", "somepass", flags);RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PrivateKey;RSAParameters rsaParam = rsa.ExportParameters(true); 
打开App,查看更多内容
随时随地看视频慕课网APP