1

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

这是正在使用的代码:

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;
}

Key正在使用给定的 a和 aIV检索。下面是代码:Rfc2898DeriveBytespasswordsalt

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);
}
4

1 回答 1

3

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

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

于 2013-08-16T19:33:17.937 回答