• <tfoot id='yuH20'></tfoot>
  • <small id='yuH20'></small><noframes id='yuH20'>

  • <i id='yuH20'><tr id='yuH20'><dt id='yuH20'><q id='yuH20'><span id='yuH20'><b id='yuH20'><form id='yuH20'><ins id='yuH20'></ins><ul id='yuH20'></ul><sub id='yuH20'></sub></form><legend id='yuH20'></legend><bdo id='yuH20'><pre id='yuH20'><center id='yuH20'></center></pre></bdo></b><th id='yuH20'></th></span></q></dt></tr></i><div id='yuH20'><tfoot id='yuH20'></tfoot><dl id='yuH20'><fieldset id='yuH20'></fieldset></dl></div>

          <bdo id='yuH20'></bdo><ul id='yuH20'></ul>
        <legend id='yuH20'><style id='yuH20'><dir id='yuH20'><q id='yuH20'></q></dir></style></legend>

        如何从 .NET 读取 PEM RSA 私钥

        时间:2023-06-01

            <bdo id='68YFc'></bdo><ul id='68YFc'></ul>
              <i id='68YFc'><tr id='68YFc'><dt id='68YFc'><q id='68YFc'><span id='68YFc'><b id='68YFc'><form id='68YFc'><ins id='68YFc'></ins><ul id='68YFc'></ul><sub id='68YFc'></sub></form><legend id='68YFc'></legend><bdo id='68YFc'><pre id='68YFc'><center id='68YFc'></center></pre></bdo></b><th id='68YFc'></th></span></q></dt></tr></i><div id='68YFc'><tfoot id='68YFc'></tfoot><dl id='68YFc'><fieldset id='68YFc'></fieldset></dl></div>

                <tfoot id='68YFc'></tfoot><legend id='68YFc'><style id='68YFc'><dir id='68YFc'><q id='68YFc'></q></dir></style></legend>
                  <tbody id='68YFc'></tbody>

                <small id='68YFc'></small><noframes id='68YFc'>

                • 本文介绍了如何从 .NET 读取 PEM RSA 私钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个 PEM 格式的 RSA 私钥,有没有一种直接的方法可以从 .NET 中读取它并实例化一个 RSACryptoServiceProvider 解密用相应公钥加密的数据?

                  I've got an RSA private key in PEM format, is there a straight forward way to read that from .NET and instantiate an RSACryptoServiceProvider to decrypt data encrypted with the corresponding public key?

                  推荐答案

                  更新 03/03/2021

                  .NET 5 现在支持此功能.

                  Update 03/03/2021

                  .NET 5 now supports this out of the box.

                  要尝试下面的代码片段,请在 http://travistidwell.com/生成密钥对并加密一些文本jsencrypt/demo/

                  To try the code snippet below, generate a keypair and encrypt some text at http://travistidwell.com/jsencrypt/demo/

                  var privateKey = @"-----BEGIN RSA PRIVATE KEY-----
                  { the full PEM private key } 
                  -----END RSA PRIVATE KEY-----";
                  
                  var rsa = RSA.Create();
                  rsa.ImportFromPem(privateKey.ToCharArray());
                  
                  var decryptedBytes = rsa.Decrypt(
                      Convert.FromBase64String("{ base64-encoded encrypted string }"), 
                      RSAEncryptionPadding.Pkcs1
                  );
                  
                  // this will print the original unencrypted string
                  Console.WriteLine(Encoding.UTF8.GetString(decryptedBytes));
                  

                  原答案

                  我解决了,谢谢.万一有人感兴趣,bouncycastle 成功了,只是因为我缺乏知识而花了我一些时间和文档.这是代码:

                  Original answer

                  I solved, thanks. In case anyone's interested, bouncycastle did the trick, just took me some time due to lack of knowledge from on my side and documentation. This is the code:

                  var bytesToDecrypt = Convert.FromBase64String("la0Cz.....D43g=="); // string to decrypt, base64 encoded
                   
                  AsymmetricCipherKeyPair keyPair; 
                   
                  using (var reader = File.OpenText(@"c:myprivatekey.pem")) // file containing RSA PKCS1 private key
                      keyPair = (AsymmetricCipherKeyPair) new PemReader(reader).ReadObject(); 
                   
                  var decryptEngine = new Pkcs1Encoding(new RsaEngine());
                  decryptEngine.Init(false, keyPair.Private); 
                   
                  var decrypted = Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length)); 
                  

                  这篇关于如何从 .NET 读取 PEM RSA 私钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:填充无效,无法删除? 下一篇:C# 将私有/公共 RSA 密钥从 RSACryptoServiceProvider 导出

                  相关文章

                  最新文章

                • <small id='IyZzY'></small><noframes id='IyZzY'>

                  <tfoot id='IyZzY'></tfoot>

                    <bdo id='IyZzY'></bdo><ul id='IyZzY'></ul>

                    <i id='IyZzY'><tr id='IyZzY'><dt id='IyZzY'><q id='IyZzY'><span id='IyZzY'><b id='IyZzY'><form id='IyZzY'><ins id='IyZzY'></ins><ul id='IyZzY'></ul><sub id='IyZzY'></sub></form><legend id='IyZzY'></legend><bdo id='IyZzY'><pre id='IyZzY'><center id='IyZzY'></center></pre></bdo></b><th id='IyZzY'></th></span></q></dt></tr></i><div id='IyZzY'><tfoot id='IyZzY'></tfoot><dl id='IyZzY'><fieldset id='IyZzY'></fieldset></dl></div>
                    <legend id='IyZzY'><style id='IyZzY'><dir id='IyZzY'><q id='IyZzY'></q></dir></style></legend>