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

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

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

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

        使用 RSA 公鑰解密使用 RSA 私鑰加密的字符串

        Using an RSA Public Key to decrypt a string that was encrypted using RSA Private Key(使用 RSA 公鑰解密使用 RSA 私鑰加密的字符串)

          <legend id='d1bHw'><style id='d1bHw'><dir id='d1bHw'><q id='d1bHw'></q></dir></style></legend>

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

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

                  <bdo id='d1bHw'></bdo><ul id='d1bHw'></ul>
                • <tfoot id='d1bHw'></tfoot>
                    <tbody id='d1bHw'></tbody>
                  本文介紹了使用 RSA 公鑰解密使用 RSA 私鑰加密的字符串的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我知道我可能得到的主要答案是你為什么要這么做?!

                  I know the main answer I am likely to get is why the hell would you want to do that?!

                  不幸的是,盡管我提出了抗議,但我還是不得不這樣做,盡管我知道這毫無意義.

                  Unfortunately despite my protests I have to do it, even though I know it makes little sense.

                  我有用 .Net 編寫的函數,可以使用私鑰解密,使用公鑰加密.我還進行了 RSA 簽名和驗證,并且對我認為這一切是如何工作的有一個合理的理解.

                  I have functions written in .Net to decrypt using a private key, encrypt using a public key. I also RSA sign and verify and have a reasonable understanding of how this all work I think.

                  我現在收到一個使用私鑰進行 RSA 加密的值,我應該通過使用公鑰解密來導出可用值.

                  I am now being sent a value that is RSA encrypted using a private key which I am supposed to derive a usable value by decrypting using the public key.

                  我似乎無法弄清楚如何做到這一點.我是個白癡嗎?這是正常的做法嗎?

                  I can't seem to figure out how to do this. Am I being an idiot? Is this a normal thing to do?

                  向我發送值的人告訴我,這在 PHP 中沒有問題.我不知道也沒有使用過PHP.我找不到使用我知道的任何主要語言(即 C++、Java、C#)的庫.我正在使用的服務器使用 .Net.

                  I am told by the person sending me the value that this is no problem in PHP. I don't know and haven't used PHP yet. I can't find a library to do it in any of the main languages I know i.e. C++, Java, C#. The server I am working on uses .Net.

                  我希望有人可以幫助我.

                  I am hoping someone might be able help me.

                  如果除了乞求他們改變他們正在做的事情之外,還有某種合理的解決方案,那就太好了.

                  It would be great if there is some kind of reasonable solution besides begging them to change what they are doing.

                  這是我的方法(從 Iridium 指出的我以前的壞方法更新)但是當我嘗試解密該值時,我得到一個異常

                  This is my method (updated from my previous bad one as pointed out by Iridium) but when I try to decrypt the value I get an exception

                  解碼 OAEP 填充時出錯."

                  "Error occurred while decoding OAEP padding."

                  如果我使用 rsa.Decrypt(bytes, false) 我會得到一個錯誤的密鑰異常.

                  If I use rsa.Decrypt(bytes, false) I get a bad key exception.

                  public static string DecryptUsingPublic(string dataEncrypted, string publicKey)
                      {
                          if (dataEncrypted == null) throw new ArgumentNullException("dataEncrypted");
                          if (publicKey == null) throw new ArgumentNullException("publicKey");
                          try
                          {
                              RSAParameters _publicKey = LoadRsaPublicKey(publicKey, false);
                              RSACryptoServiceProvider rsa = InitRSAProvider(_publicKey);
                  
                              byte[] bytes = Convert.FromBase64String(dataEncrypted);
                              byte[] decryptedBytes = rsa.Decrypt(bytes, true);
                  
                              ArrayList arrayList = new ArrayList();
                              arrayList.AddRange(decryptedBytes);
                  
                             return Encoding.UTF8.GetString(decryptedBytes);
                          }
                          catch
                          {
                              return null;
                          }
                      }
                  
                      private static RSAParameters LoadRsaPublicKey(String publicKeyFilePath, Boolean isFile)
                      {
                          RSAParameters RSAKeyInfo = new RSAParameters();
                          byte[] pubkey = ReadFileKey(publicKeyFilePath, "PUBLIC KEY", isFile);
                          byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
                          byte[] seq = new byte[15];
                          // ---------  Set up stream to read the asn.1 encoded SubjectPublicKeyInfo blob  ------
                          MemoryStream mem = new MemoryStream(pubkey);
                          BinaryReader binr = new BinaryReader(mem);    //wrap Memory Stream with BinaryReader for easy reading
                          byte bt = 0;
                          ushort twobytes = 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 RSAKeyInfo;
                  
                              seq = binr.ReadBytes(15);       //read the Sequence OID
                              if (!CompareBytearrays(seq, SeqOID))    //make sure Sequence for OID is correct
                                  return RSAKeyInfo;
                  
                              twobytes = binr.ReadUInt16();
                              if (twobytes == 0x8103) //data read as little endian order (actual data order for Bit String is 03 81)
                                  binr.ReadByte();    //advance 1 byte
                              else if (twobytes == 0x8203)
                                  binr.ReadInt16();   //advance 2 bytes
                              else
                                  return RSAKeyInfo;
                  
                              bt = binr.ReadByte();
                              if (bt != 0x00)     //expect null byte next
                                  return RSAKeyInfo;
                  
                              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 RSAKeyInfo;
                  
                              twobytes = binr.ReadUInt16();
                              byte lowbyte = 0x00;
                              byte highbyte = 0x00;
                  
                              if (twobytes == 0x8102) //data read as little endian order (actual data order for Integer is 02 81)
                                  lowbyte = binr.ReadByte();  // read next bytes which is bytes in modulus
                              else if (twobytes == 0x8202)
                              {
                                  highbyte = binr.ReadByte(); //advance 2 bytes
                                  lowbyte = binr.ReadByte();
                              }
                              else
                                  return RSAKeyInfo;
                              byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };   //reverse byte order since asn.1 key uses big endian order
                              int modsize = BitConverter.ToInt32(modint, 0);
                  
                              byte firstbyte = binr.ReadByte();
                              binr.BaseStream.Seek(-1, SeekOrigin.Current);
                  
                              if (firstbyte == 0x00)
                              {   //if first byte (highest order) of modulus is zero, don't include it
                                  binr.ReadByte();    //skip this null byte
                                  modsize -= 1;   //reduce modulus buffer size by 1
                              }
                  
                              byte[] modulus = binr.ReadBytes(modsize);   //read the modulus bytes
                  
                              if (binr.ReadByte() != 0x02)            //expect an Integer for the exponent data
                                  return RSAKeyInfo;
                              int expbytes = (int)binr.ReadByte();        // should only need one byte for actual exponent data (for all useful values)
                              byte[] exponent = binr.ReadBytes(expbytes);
                  
                  
                              RSAKeyInfo.Modulus = modulus;
                              RSAKeyInfo.Exponent = exponent;
                  
                              return RSAKeyInfo;
                          }
                          catch (Exception)
                          {
                              return RSAKeyInfo;
                          }
                  
                          finally { binr.Close(); }
                          //return RSAparams;
                  
                      }
                  
                   private static RSACryptoServiceProvider InitRSAProvider(RSAParameters rsaParam)
                      {
                          //
                          // Initailize the CSP
                          //   Supresses creation of a new key
                          //
                          CspParameters csp = new CspParameters();
                          //csp.KeyContainerName = "RSA Test (OK to Delete)";
                  
                          const int PROV_RSA_FULL = 1;
                          csp.ProviderType = PROV_RSA_FULL;
                  
                          const int AT_KEYEXCHANGE = 1;
                          // const int AT_SIGNATURE = 2;
                          csp.KeyNumber = AT_KEYEXCHANGE;
                          //
                          // Initialize the Provider
                          //
                          RSACryptoServiceProvider rsa =
                            new RSACryptoServiceProvider(csp);
                          rsa.PersistKeyInCsp = false;
                  
                          //
                          // The moment of truth...
                          //
                          rsa.ImportParameters(rsaParam);
                          return rsa;
                      }
                  
                      private static int GetIntegerSize(BinaryReader binr)
                      {
                          byte bt = 0;
                          byte lowbyte = 0x00;
                          byte highbyte = 0x00;
                          int count = 0;
                          bt = binr.ReadByte();
                          if (bt != 0x02)     //expect integer
                              return 0;
                          bt = binr.ReadByte();
                  
                          if (bt == 0x81)
                              count = binr.ReadByte();    // data size in next byte
                          else
                              if (bt == 0x82)
                              {
                                  highbyte = binr.ReadByte(); // data size in next 2 bytes
                                  lowbyte = binr.ReadByte();
                                  byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
                                  count = BitConverter.ToInt32(modint, 0);
                              }
                              else
                              {
                                  count = bt;     // we already have the data size
                              }
                  
                          while (binr.ReadByte() == 0x00)
                          {   //remove high order zeros in data
                              count -= 1;
                          }
                          binr.BaseStream.Seek(-1, SeekOrigin.Current);       //last ReadByte wasn't a removed zero, so back up a byte
                          return count;
                      }
                  
                      private static bool CompareBytearrays(byte[] a, byte[] b)
                      {
                          if (a.Length != b.Length)
                              return false;
                          int i = 0;
                          foreach (byte c in a)
                          {
                              if (c != b[i])
                                  return false;
                              i++;
                          }
                          return true;
                      }
                  

                  InitRSAProvider 和 LoadRsaPublicKey 上面的兩種方法已從教程中刪除,以允許 PEM 密鑰作為字符串用于 .Net.

                  The two methods above InitRSAProvider and LoadRsaPublicKey were gotten out of tutorials to allow PEM keys as Strings to be used with .Net.

                  推薦答案

                  查看了一些關于 RSA 加密模式的信息,看起來 PKCS#1 v1.5(你正在使用,因為你是調用 Decrypt(..., false))

                  Having looked at some of the information on RSA encryption modes, it would appear that PKCS#1 v1.5 (which you're using, because you're calling Decrypt(..., false))

                  "...可以對長度為 最多 k - 11 個八位字節的消息進行操作(k 是 RSA 模數的八位字節長度)"

                  "...can operate on messages of length up to k - 11 octets (k is the octet length of the RSA modulus)"

                  (RFC 3447,強調我的).

                  (RFC 3447, emphasis mine).

                  根據指示您的密鑰為 128 字節的錯誤消息,這意味著您無法使用 PKCS#1 v1.5 對超過 128 - 11 = 的消息執行 RSA (en|de)cryption117 字節.

                  Based on the error message, which indicates that your key is 128 bytes, that means that you can't perform RSA (en|de)cryption using PKCS#1 v1.5 on a message with more than 128 - 11 = 117 bytes.

                  您不應直接使用 RSA 加密您的消息,而應使用對稱算法加密消息正文,并僅使用 RSA 加密對稱加密密鑰.只有當您的消息相當短(即您的密鑰大小低于 117 字節)時,您才應考慮直接使用 RSA 加密消息.

                  Instead of encrypting your message directly using RSA, you should be using a symmetric algorithm to encrypt the body of the message, and encrypt only the symmetric encryption key using RSA. Only if your message is reasonably short (i.e. below 117 bytes for your key size) should you consider encrypting the message directly with RSA.

                  我添加了以下內容,假設您的輸入是 Base64 編碼的,正如您在下面的評論中指出的那樣:

                  I have added the following, assuming that your input is Base64 encoded as you indicate in your comment below:

                  public string DecryptUsingPublic(string dataEncryptedBase64, string publicKey)
                      {
                          if (dataEncryptedBase64 == null) throw new ArgumentNullException("dataEncryptedBase64");
                          if (publicKey == null) throw new ArgumentNullException("publicKey");
                          try
                          {
                              RSAParameters _publicKey = LoadRsaPublicKey(publicKey, false);
                              RSACryptoServiceProvider rsa = InitRSAProvider(_publicKey);
                  
                              byte[] bytes = Convert.FromBase64String(dataEncryptedBase64);
                              byte[] decryptedBytes = rsa.Decrypt(bytes, false);
                  
                              // I assume here that the decrypted data is intended to be a
                              // human-readable string, and that it was UTF8 encoded.
                              return Encoding.UTF8.GetString(decryptedBytes);
                          }
                          catch
                          {
                              return null;
                          }
                      }
                  

                  這篇關于使用 RSA 公鑰解密使用 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='kRCne'></tbody>

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

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

                          1. 主站蜘蛛池模板: 久久久久久亚洲 | 欧美 日韩 国产 成人 在线 | 波多野吉衣在线播放 | 成人一区二区视频 | 久久伊人精品 | 国产高清一区二区三区 | 亚洲aⅴ | 91精品国产一区二区 | 久久黄色网 | 激情六月丁香婷婷 | 天天干天天插天天 | 日日摸夜夜添夜夜添特色大片 | 免费午夜视频 | 日韩欧美操 | 黑人巨大精品 | 99久久久久久99国产精品免 | 亚洲成人三级 | 亚洲一区在线免费观看 | 欧美一区二区三区一在线观看 | 国产一级淫片a直接免费看 免费a网站 | a在线视频| 日本色高清 | 伊人免费网 | 亚洲天堂999 | 欧美日韩国产一区二区三区 | 国产黄色网址在线观看 | 中文字幕在线一区二区三区 | 成人在线视频看看 | 亚洲国产二区 | 国产一区二区av | 精品无码三级在线观看视频 | 欧美日韩精品免费观看 | 欧美中文字幕一区二区三区 | 日本天堂一区 | 99热首页| 国产乱码高清区二区三区在线 | 91影院在线观看 | 国产亚洲区 | 高清欧美性猛交xxxx黑人猛交 | 免费一区| 黄色一级电影免费观看 |