加密字符串时的Cryptographicexception

时间:2015-02-14 20:29:07

标签: c# encryption

目前,我正在开展一个涉及加密的项目。因此,我有一个类,在.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("\0".ToCharArray());
    }
}

错误消息(由德语翻译):

  

CryptographicException:要加密的数据长度无效。 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)

1 个答案:

答案 0 :(得分:1)

  

Encryption.Encrypt("Client connected!", "elit3Nase")不起作用

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

symmetricKey.Padding = PaddingMode.None;

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