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

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

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

    1. “坏数据"加密异常

      时间:2023-06-02

      <small id='8rmC3'></small><noframes id='8rmC3'>

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

        <legend id='8rmC3'><style id='8rmC3'><dir id='8rmC3'><q id='8rmC3'></q></dir></style></legend>
          <tbody id='8rmC3'></tbody>
        <tfoot id='8rmC3'></tfoot>

                <bdo id='8rmC3'></bdo><ul id='8rmC3'></ul>
                本文介绍了“坏数据"加密异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                首先,我仅出于学术目的编写以下代码.我之所以这么说是因为我没有把它放在生产环境中,因此我绕过"了一些我需要做的开销,我只需要能够使用加密/解密字符串下面的代码.有几次我能够做到这一点,但由于某种原因,我开始收到CryptographicException Bad Data"并且不确定是什么导致了问题.

                First, I have only written the code below for academic purposes. The reason I say this is because I am not putting this in a production environment, and therefor am "bypassing" some of the overhead that I would need to do if I was, I simply need to be able to encrypt/decrypt a string using the code below. I was able to do it a few time, but for some reason, I started receiving "CryptographicException Bad Data" and am not sure what might be causing the problem.

                   private string RSAEncrypt(string value)
                    {
                        byte[] encryptedData = Encoding.Unicode.GetBytes(value);
                
                        CspParameters cspParams = new CspParameters();
                        cspParams.KeyContainerName = _rsaContainerName;
                        using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048,cspParams))
                        {
                            encryptedData = RSA.Encrypt(encryptedData, false);
                            return Convert.ToBase64String(encryptedData);
                
                        }
                
                    }
                
                
                
                    private string RSADecrypt(string value)
                    {
                
                        byte[] encryptedData = Encoding.Unicode.GetBytes(value);
                
                        CspParameters cspParams = new CspParameters();
                        cspParams.KeyContainerName = _rsaContainerName;
                        using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048,cspParams))
                        { 
                            encryptedData = RSA.Decrypt(encryptedData,false);
                            return Convert.ToBase64String(encryptedData);
                
                        }
                    }
                

                它只是在 RSADecrypt 调用上抛出这个异常.

                It is only throwing this exception on the RSADecrypt call.

                有什么想法吗?我在某处读到它可能与传递给 RSA.Decrypt 的 encryptedData 的预期大小有关.

                Any ideas? I read somewhere it might have to do with the expected size of encryptedData that is passed into RSA.Decrypt.

                谢谢}

                推荐答案

                • 使用字符串编码(即Encoding.Unicode)来回转换明文.

                  使用 Base-64 来回转换加密数据(即 Convert.[To/From]Base64String);

                  Convert the encrypted data back and forth using Base-64 (i.e. Convert.[To/From]Base64String);

                  像这样:

                  private string RSAEncrypt(string value)
                  {
                      byte[] plaintext = Encoding.Unicode.GetBytes(value);
                  
                      CspParameters cspParams = new CspParameters();
                      cspParams.KeyContainerName = _rsaContainerName;
                      using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048,cspParams))
                      {
                          byte[] encryptedData = RSA.Encrypt(plaintext, false);
                          return Convert.ToBase64String(encryptedData);
                      }
                  }
                  
                  private string RSADecrypt(string value)
                  {
                      byte[] encryptedData = Convert.FromBase64String(value);
                  
                      CspParameters cspParams = new CspParameters();
                      cspParams.KeyContainerName = _rsaContainerName;
                      using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048,cspParams))
                      { 
                          byte[] decryptedData = RSA.Decrypt(encryptedData,false);
                          return Encoding.Unicode.GetString(decryptedData);
                      }
                  }
                  

                  这篇关于“坏数据"加密异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:RSACryptoServiceProvider(.NET 的 RSA)可以使用 SHA256 进行 下一篇:为什么RSAParameters Modulus不等于P和Q的乘积?

                相关文章

                最新文章

                  1. <small id='g32js'></small><noframes id='g32js'>

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

                  2. <tfoot id='g32js'></tfoot>
                      <bdo id='g32js'></bdo><ul id='g32js'></ul>