• <legend id='lLu6N'><style id='lLu6N'><dir id='lLu6N'><q id='lLu6N'></q></dir></style></legend>
  • <small id='lLu6N'></small><noframes id='lLu6N'>

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

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

        <tfoot id='lLu6N'></tfoot>

        指定的填充模式对此算法无效 - c# - System.Securit

        时间:2023-06-02

        1. <tfoot id='q2Kzy'></tfoot>
            • <bdo id='q2Kzy'></bdo><ul id='q2Kzy'></ul>

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

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

                  本文介绍了指定的填充模式对此算法无效 - c# - System.Security.Cryptography的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  对 c# 非常陌生,目前在解密长密码时遇到问题,错误为

                  pretty new to c# and currently having a problem decrypting long passwords with an error of

                  指定的密钥不是该算法的有效大小

                  Specified key is not a valid size for this algorithm

                  我知道这与不支持加密密码位长度有关,但不确定如何采用建议的方法来允许这些更长的密码.

                  I know this has something to do with the encrypted password bits length not being supported but unsure how to go about suggested ways to allow for these longer passwords.

                  这是我的加密和解密

                  cipherKey":0123456789abcdef",cipherVector":有点酷"

                  "cipherKey": "0123456789abcdef", "cipherVector": "somereallycooliv"

                  using System;
                  using System.Security.Cryptography;
                  using System.IO;
                  using System.Text;
                  
                  namespace DataApi
                  {
                  public class Encryption
                  {
                      private readonly IConfigurationService _configService;
                  
                  
                      private const string _vector = "cipherVector";
                      private const string _key = "cipherKey";
                  
                      public Encryption(IConfigurationService configService)
                      {
                          _configService = configService;
                      }
                      public string EncryptString(string text)
                      {
                          if(string.IsNullOrEmpty(text))
                          {
                              return "";
                          }
                          try
                          {
                  
                        var key = Encoding.UTF8.GetBytes(_configService.Get(_key));
                          byte[] IV = Encoding.ASCII.GetBytes(_configService.Get(_vector));
                  
                          using (var aesAlg = Aes.Create())
                          {
                              using (var encryptor = aesAlg.CreateEncryptor(key, IV))
                              {
                                  using (var msEncrypt = new MemoryStream())
                                  {
                                      using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                                      using (var swEncrypt = new StreamWriter(csEncrypt))
                                      {
                                          swEncrypt.Write(text);
                                      }
                  
                                      var decryptedContent = msEncrypt.ToArray();
                  
                                      var result = new byte[IV.Length + decryptedContent.Length];
                  
                                      Buffer.BlockCopy(IV, 0, result, 0, IV.Length);
                                      Buffer.BlockCopy(decryptedContent, 0, result, IV.Length, decryptedContent.Length);
                  
                                      return Convert.ToBase64String(result);
                                  }
                              }
                          }
                          }
                          catch(Exception e) {
                               Loggifer.Error("Unable to encrypt string: "+text , e );
                  
                              throw e;
                          }
                      }
                  
                      public string DecryptString(string cipherText)
                      {
                          if(string.IsNullOrEmpty(cipherText))
                          {
                              return "";
                          }
                          try
                          {
                              var fullCipher = Convert.FromBase64String(cipherText);
                  
                              byte[] IV = Encoding.ASCII.GetBytes(_configService.Get(_vector));
                              var cipher = new byte[16];
                  
                              Buffer.BlockCopy(fullCipher, 0, IV, 0, IV.Length);
                              Buffer.BlockCopy(fullCipher, IV.Length, cipher, 0, IV.Length);
                              var key = Encoding.UTF8.GetBytes(_configService.Get(_key));
                  
                              using (var aesAlg = Aes.Create())
                              {
                                  using (var decryptor = aesAlg.CreateDecryptor(key, IV))
                                  {
                                      string result;
                                      using (var msDecrypt = new MemoryStream(cipher))
                                      {
                                          using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                                          {
                                              using (var srDecrypt = new StreamReader(csDecrypt))
                                              {
                                                  result = srDecrypt.ReadToEnd();
                                              }
                                          }
                                      }
                  
                                      return result;
                                  }
                              }
                          }
                          catch (Exception e)
                          {
                              Loggifer.Error("Unable to decrypt string: "+cipherText , e );
                              throw e;
                          }
                      }
                  
                  }
                  }
                  

                  推荐答案

                  函数public string DecryptString(string cipherText)

                   var cipher = new byte[fullCipher.Length - IV.Length];
                  

                   Buffer.BlockCopy(fullCipher, IV.Length, cipher, 0, fullCipher.Length - IV.Length);
                  

                  这篇关于指定的填充模式对此算法无效 - c# - System.Security.Cryptography的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:导入 RSA CngKey 并存储在 MicrosoftSoftwareKeyStoragePro 下一篇:如何在 C# 中将字符串转换为字节 []

                  相关文章

                  最新文章

                  <tfoot id='VQSQZ'></tfoot>

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

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