加密字符串时出现密码异常

Cryptographicexception at encrypting string

目前,我正在从事一个涉及加密的项目。因此,我有一个 class,它在 .NET 4.0 中运行良好,但现在我更改它以便它在 .NET 2.0 中运行,它几乎无法加密任何字符串...我不知道为什么,但它每次都失败并抛出 CryptographicException。据我所知,解密工作正常,其他一切也很好。 无论如何,这是代码:

public class Encryption
{
    static readonly string PasswordHash = "P@@Sw0rd";
    static readonly string SaltKey = "S@LT&KEY";
    static readonly string VIKey = "@1B2c3D4e5F6g7H8";
    public static string Encrypt(string plainText, string passwordHash)
    {
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
        byte[] keyBytes = new Rfc2898DeriveBytes(passwordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
        RijndaelManaged symmetricKey = new RijndaelManaged();
        symmetricKey.Mode = CipherMode.CBC;
        symmetricKey.Padding = PaddingMode.None;
        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
        byte[] cipherTextBytes;
        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
            {
                cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                cryptoStream.FlushFinalBlock();
                cipherTextBytes = memoryStream.ToArray();
                cryptoStream.Close();
            }
            memoryStream.Close();
        }
        return Convert.ToBase64String(cipherTextBytes);
    }
    public static string Decrypt(string encryptedText, string passwordHash)
    {
        byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
        byte[] keyBytes = new Rfc2898DeriveBytes(passwordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
        RijndaelManaged symmetricKey = new RijndaelManaged(); 
        symmetricKey.Mode = CipherMode.CBC;
        symmetricKey.Padding = PaddingMode.None;
        ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
        MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
        CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
        byte[] plainTextBytes = new byte[cipherTextBytes.Length];
        int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
        memoryStream.Close();
        cryptoStream.Close();
        return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("[=10=]".ToCharArray());
    }
}

错误信息是(翻译自德语):

CryptographicException: Length of the data to encrypt is invalid. at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) at System.Security.Cryptography.CryptoStream.FlushFinalBlock() at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)

Encryption.Encrypt("Client connected!", "elit3Nase") does not work

"Client connected!" 的长度不足以完全填满一个块(16 字节)并且您没有使用填充:

symmetricKey.Padding = PaddingMode.None;

更改为 symmetricKey.Padding = PaddingMode.PKCS7; 以确保应用填充。