AesCryptoServiceProvider的默认填充是什么

本文关键字:是什么 填充 默认 AesCryptoServiceProvider | 更新日期: 2023-09-27 18:28:07

我在尝试使用 AesCryptoServiceProvider 解密字符串时收到错误Padding is invalid and cannot be removed。根据这个问题,我需要在加密和解密算法上指定相同的填充。我遇到的问题是已经有数据在没有显式填充的情况下被加密。在这种情况下使用的默认填充是什么,以便我可以显式设置它?

下面是正在使用的代码:

public static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
  // Check arguments. 
  if (plainText == null || plainText.Length <= 0)
    throw new ArgumentNullException("plainText");
  if (Key == null || Key.Length <= 0)
    throw new ArgumentNullException("Key");
  if (IV == null || IV.Length <= 0)
    throw new ArgumentNullException("Key");
  byte[] encrypted;
  // Create an AesCryptoServiceProvider object 
  // with the specified key and IV. 
  using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
  {
    aesAlg.Key = Key;
    aesAlg.IV = IV;
    // Create a decrytor to perform the stream transform.
    ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
    // Create the streams used for encryption. 
    using (MemoryStream msEncrypt = new MemoryStream())
    {
      using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
      {
        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
        {
          //Write all data to the stream.
          swEncrypt.Write(plainText);
        }
        encrypted = msEncrypt.ToArray();
      }
    }
  }
  // Return the encrypted bytes from the memory stream. 
  return encrypted;
}
public static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
{
  // Check arguments. 
  if (cipherText == null || cipherText.Length <= 0)
    throw new ArgumentNullException("cipherText");
  if (Key == null || Key.Length <= 0)
    throw new ArgumentNullException("Key");
  if (IV == null || IV.Length <= 0)
    throw new ArgumentNullException("Key");
  string plaintext = null;
  using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
  {
    aesAlg.Key = Key;
    aesAlg.IV = IV;
    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
    using (MemoryStream msDecrypt = new MemoryStream(cipherText))
    {
      using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
      {
        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
        {
          plaintext = srDecrypt.ReadToEnd();
        }
      }
    }
  }
  return plaintext;
}

KeyIV正在使用给定passwordsalt Rfc2898DeriveBytes进行检索。波纹管是代码:

public static void GetKeyAndIVFromPasswordAndSalt(string password, byte[] salt, SymmetricAlgorithm symmetricAlgorithm, ref byte[] key, ref byte[] iv)
{
  Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, salt);
  key = rfc2898DeriveBytes.GetBytes(symmetricAlgorithm.KeySize / 8);
  iv = rfc2898DeriveBytes.GetBytes(symmetricAlgorithm.BlockSize / 8);
}

AesCryptoServiceProvider的默认填充是什么

可以在 MSDN 上的"对称算法属性"页中找到对称算法的默认模式。在这种情况下,PaddingMode.PKCS7是使用的默认填充模式。

但是,请注意,此特定加密异常也可能在不同情况下引发。如果您的密钥和/或 IV 与用于加密数据时使用的密钥和/或 IV 不完全相同,则将引发此异常。您链接的问题中的第二个答案讨论了这个问题。