久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    • <bdo id='fKKyQ'></bdo><ul id='fKKyQ'></ul>

        <small id='fKKyQ'></small><noframes id='fKKyQ'>

      1. <legend id='fKKyQ'><style id='fKKyQ'><dir id='fKKyQ'><q id='fKKyQ'></q></dir></style></legend>

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

      2. C# BouncyCastle - 使用公鑰/私鑰的 RSA 加密

        C# BouncyCastle - RSA Encryption with Public/Private keys(C# BouncyCastle - 使用公鑰/私鑰的 RSA 加密)
        <i id='gzx5n'><tr id='gzx5n'><dt id='gzx5n'><q id='gzx5n'><span id='gzx5n'><b id='gzx5n'><form id='gzx5n'><ins id='gzx5n'></ins><ul id='gzx5n'></ul><sub id='gzx5n'></sub></form><legend id='gzx5n'></legend><bdo id='gzx5n'><pre id='gzx5n'><center id='gzx5n'></center></pre></bdo></b><th id='gzx5n'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='gzx5n'><tfoot id='gzx5n'></tfoot><dl id='gzx5n'><fieldset id='gzx5n'></fieldset></dl></div>
          <tbody id='gzx5n'></tbody>
      3. <legend id='gzx5n'><style id='gzx5n'><dir id='gzx5n'><q id='gzx5n'></q></dir></style></legend>

        <small id='gzx5n'></small><noframes id='gzx5n'>

                <bdo id='gzx5n'></bdo><ul id='gzx5n'></ul>
              • <tfoot id='gzx5n'></tfoot>

                • 本文介紹了C# BouncyCastle - 使用公鑰/私鑰的 RSA 加密的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我需要在 C# 中加密數據才能將其傳遞給 Java.Java 代碼屬于第 3 方,但我已獲得相關來源,因此我決定由于 Java 使用 Bouncy Castle 庫,我將使用 C# 端口.

                  I need to encrypt data in C# in order to pass it to Java. The Java code belongs to a 3rd party but I have been given the relevant source, so I decided that as the Java uses the Bouncy Castle libs, I will use the C# port.

                  解密工作正常.但是,只有當我使用私鑰而不是公鑰進行加密時,解密才有效.使用公鑰時,解密失敗,unknown block type.

                  Decryption works fine. However, decryption works only when I use the encrypt using the private key, and not with the public key. When using the public key, decryption fails with unknown block type.

                  顯然 RsaEncryptWithPrivate 內部的加密在加密時使用公鑰,所以我不明白為什么兩種加密方法在功能上不一樣:

                  Obviously the encryption inside the RsaEncryptWithPrivate uses the public key when encrypting, so I do not get why the two encryption methods are not functionally identical:

                  using Org.BouncyCastle.Crypto;
                  using Org.BouncyCastle.Crypto.Encodings;
                  using Org.BouncyCastle.Crypto.Engines;
                  using Org.BouncyCastle.OpenSsl;
                  
                  public class EncryptionClass
                  {       
                      public string RsaEncryptWithPublic(string clearText
                          , string publicKey)
                      {
                          var bytesToEncrypt = Encoding.UTF8.GetBytes(clearText);
                  
                          var encryptEngine = new Pkcs1Encoding(new RsaEngine());
                  
                          using (var txtreader = new StringReader(publicKey))
                          {
                              var keyParameter = (AsymmetricKeyParameter)new PemReader(txtreader).ReadObject();
                  
                              encryptEngine.Init(true, keyParameter);
                          }
                  
                          var encrypted = Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));
                          return encrypted;
                  
                      }
                  
                      public string RsaEncryptWithPrivate(string clearText
                          , string privateKey)
                      {
                          var bytesToEncrypt = Encoding.UTF8.GetBytes(clearText);
                  
                          var encryptEngine = new Pkcs1Encoding(new RsaEngine());
                  
                          using (var txtreader = new StringReader(privateKey))
                          {
                              var keyPair = (AsymmetricCipherKeyPair)new PemReader(txtreader).ReadObject();
                  
                              encryptEngine.Init(true, keyPair.Public);
                          }
                  
                          var encrypted= Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));
                          return encrypted;
                      }
                  
                  
                      // Decryption:
                  
                      public string RsaDecrypt(string base64Input
                          , string privateKey)
                      {
                          var bytesToDecrypt = Convert.FromBase64String(base64Input);
                  
                          //get a stream from the string
                          AsymmetricCipherKeyPair keyPair;
                          var decryptEngine = new Pkcs1Encoding(new RsaEngine());
                  
                          using ( var txtreader = new StringReader(privateKey) )
                          {
                              keyPair = (AsymmetricCipherKeyPair) new PemReader(txtreader).ReadObject();
                  
                              decryptEngine.Init(false, keyPair.Private);
                          }
                  
                          var decrypted = Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length));
                          return decrypted;
                      }
                  }
                  
                  // In my test project   
                  
                      [Test()]
                      public void EncryptTest()
                      {
                          // Set up 
                          var input = "Perceived determine departure explained no forfeited";
                          var enc = new EncryptionClass();
                          var publicKey = "-----BEGIN PUBLIC KEY----- // SNIPPED // -----END PUBLIC KEY-----";
                          var privateKey = "-----BEGIN PRIVATE KEY----- // SNIPPED // -----END PRIVATE KEY-----";
                  
                          // Encrypt it
                          var encryptedWithPublic = enc.RsaEncryptWithPublic(input, publicKey); 
                  
                          var encryptedWithPrivate = enc.RsaEncryptWithPrivate(input, privateKey);
                  
                          // Decrypt
                          var outputWithPublic = payUEnc.RsaDecrypt(encryptedWithPrivate, privateKey); 
                          // Throws error: "unknown block type"
                  
                          var outputWithPrivate = payUEnc.RsaDecrypt(encryptedWithPrivate, _privateKey); 
                          // returns the correct decrypted text, "Perceived determine departure explained no forfeited"
                  
                          // Assertion
                          Assert.AreEqual(outputWithPrivate, input); // This is true
                      }
                  

                  順便說一句,Java 解密也出現了同樣的問題 - 僅使用公鑰加密時,它會失敗.

                  Incidentally the Java decryption exhibits the same issue - when encrypted with the public key only, it fails.

                  我對密碼學很陌生,所以我確定我在 RsaEncryptWithPublic 方法中做了一些非常簡單的錯誤.

                  I'm very new to cryptography, so I'm sure I'm doing something very simple wrong in the RsaEncryptWithPublic method.

                  我還添加了一個單元測試,證明公鑰等于從私鑰中提取的公鑰:

                  I've also added a unit test which proves that the public key is equal to the public key that is extracted from the private key:

                      [Test()]
                      public void EncryptCompareTest()
                      {
                          AsymmetricKeyParameter keyParameterFromPub;
                          AsymmetricKeyParameter keyParameterFromPriv;
                          AsymmetricCipherKeyPair keyPair;
                  
                          using (var txtreader = new StringReader(_publicKey))
                          {
                              keyParameterFromPub = (AsymmetricKeyParameter)new PemReader(txtreader).ReadObject();
                          }
                  
                          using (var txtreader = new StringReader(_privateKey))
                          {
                              keyPair = (AsymmetricCipherKeyPair)new PemReader(txtreader).ReadObject();
                              keyParameterFromPriv = keyPair.Public;
                          }
                  
                          Assert.AreEqual(keyParameterFromPub, keyParameterFromPriv); // returns true;
                  
                      } 
                  

                  推薦答案

                  我使用了不正確的公鑰.. 并且證明私鑰和公鑰匹配的測試使用了正確的公鑰.

                  I was using an incorrect Public Key.. and the test that proved the Private and Public keys matched was using the correct Public Key.

                  只要你把鑰匙弄對了,上面的代碼就可以完美地工作!

                  The above code works perfectly as is, as long as you get the keys right!

                  這篇關于C# BouncyCastle - 使用公鑰/私鑰的 RSA 加密的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  What are good algorithms for vehicle license plate detection?(車牌檢測有哪些好的算法?)
                  onClick event for Image in Unity(Unity中圖像的onClick事件)
                  Running Total C#(運行總 C#)
                  Deleting a directory when clicked on a hyperlink with JAvascript.ASP.NET C#(單擊帶有 JAvascript.ASP.NET C# 的超鏈接時刪除目錄)
                  asp.net listview highlight row on click(asp.net listview 在單擊時突出顯示行)
                  Calling A Button OnClick from a function(從函數調用按鈕 OnClick)
                    <tbody id='0cfH8'></tbody>
                • <tfoot id='0cfH8'></tfoot>
                  • <legend id='0cfH8'><style id='0cfH8'><dir id='0cfH8'><q id='0cfH8'></q></dir></style></legend>
                    <i id='0cfH8'><tr id='0cfH8'><dt id='0cfH8'><q id='0cfH8'><span id='0cfH8'><b id='0cfH8'><form id='0cfH8'><ins id='0cfH8'></ins><ul id='0cfH8'></ul><sub id='0cfH8'></sub></form><legend id='0cfH8'></legend><bdo id='0cfH8'><pre id='0cfH8'><center id='0cfH8'></center></pre></bdo></b><th id='0cfH8'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0cfH8'><tfoot id='0cfH8'></tfoot><dl id='0cfH8'><fieldset id='0cfH8'></fieldset></dl></div>
                      <bdo id='0cfH8'></bdo><ul id='0cfH8'></ul>

                          1. <small id='0cfH8'></small><noframes id='0cfH8'>

                            主站蜘蛛池模板: www.日本精品 | 天天干亚洲| 久久久久久久久久久久91 | 成人影| 欧美精品在欧美一区二区 | 久久精品国产99国产精品 | 久久福利电影 | 欧美精品一二三区 | 国产成人aⅴ | 欧美国产日韩在线观看 | 91精品国产一区二区三区香蕉 | 国产精品99久久久久久大便 | 毛片免费观看 | 国产一区二区三区亚洲 | 日本在线视频一区二区 | 日韩欧美中文在线 | 国产视频一区二区三区四区五区 | 91精品国产综合久久久久 | 久久亚洲精品久久国产一区二区 | 日韩一区二区免费视频 | 日韩网站在线观看 | 亚洲欧美日韩精品久久亚洲区 | 91精品国产综合久久久亚洲 | 欧美啪啪| 性在线| 一区二区三区欧美在线 | 国产精品免费在线 | 国产精品一区二区三区在线 | 在线播放国产一区二区三区 | 第一区在线观看免费国语入口 | 精品国产乱码 | 新91视频网 | 欧美大片一区 | 91色网站| 欧美日韩久久 | 黄色片在线看 | 国产人成在线观看 | 欧美区在线观看 | 亚洲人成在线观看 | 欧美日韩亚洲一区二区 | 欧美九九|