使用 SymmetricAlgorithm 和 CryptoStream 解密字节数组

Decrypting byte array with SymmetricAlgorithm and CryptoStream

我的加解密方式:

private static SymmetricAlgorithm GetAlgorithm(string password)
{
    using (Rijndael algorithm = Rijndael.Create())
    {
        using (Rfc2898DeriveBytes rdb = new Rfc2898DeriveBytes(password, new byte[]
        {
            0x53, 0x6f, 0x64, 0x69, 0x75, 0x6d, 0x20, 0x43, 0x68, 0x6c, 0x6f, 0x72, 0x69, 0x64, 0x65
        }))
        {
            algorithm.Padding = PaddingMode.ISO10126;
            algorithm.Key = rdb.GetBytes(32);
            algorithm.IV = rdb.GetBytes(16);
        }
        return algorithm;
    }
}

public static byte[] EncryptBytes(byte[] clearBytes, string password)
{
    ICryptoTransform encryptor;
    using (SymmetricAlgorithm algorithm = GetAlgorithm(password))
        encryptor = algorithm.CreateEncryptor();
    using (MemoryStream ms = new MemoryStream())
    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
    {
        cs.Write(clearBytes, 0, clearBytes.Length);
        cs.FlushFinalBlock();
        return ms.ToArray();
    }
}

public static byte[] DecryptBytes(byte[] cipherBytes, string password)
{
    ICryptoTransform decryptor;
    using (SymmetricAlgorithm algorithm = GetAlgorithm(password))
        decryptor = algorithm.CreateDecryptor();
    using (MemoryStream ms = new MemoryStream())
    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
    {
        cs.Write(cipherBytes, 0, cipherBytes.Length); //here is the exception thrown
        cs.FlushFinalBlock();
        return ms.ToArray();
    }
}

我如何调用这些方法:

byte[] prev = File.ReadAllBytes(path + sourcefile);
byte[] enc = Encryption.EncryptBytes(prev, password);
byte[] dec = Encryption.DecryptBytes(enc, password);

File.WriteAllBytes(path + targetfile, dec);

当我尝试解密字节数组时出现以下异常:

System.Security.Cryptography.CryptographicException
Additional information: padding is invalid and cannot be removed

我已经阅读了一些可能的解决方案,但其中 none 解决了我的问题。 Key和IV(InitialisationVector)是一样的,我加解密的时候肯定不是这个原因。

更正的方法:

错误:您正在 GetAlgorithm() 中处理 Rijndael algorithm。这是错误的:GetAlgorithm() 的调用者必须处理 algorithm(正如你所做的那样)

private static SymmetricAlgorithm GetAlgorithm(string password)
{
    Rijndael algorithm = Rijndael.Create();

    using (Rfc2898DeriveBytes rdb = new Rfc2898DeriveBytes(password, new byte[]
    {
        0x53, 0x6f, 0x64, 0x69, 0x75, 0x6d, 0x20, 0x43, 0x68, 0x6c, 0x6f, 0x72, 0x69, 0x64, 0x65
    }))
    {
        algorithm.Padding = PaddingMode.ISO10126;
        algorithm.Key = rdb.GetBytes(32);
        algorithm.IV = rdb.GetBytes(16);
    }

    return algorithm;
}

此处的小警告:您没有处理 ICryptoTransform

public static byte[] EncryptBytes(byte[] clearBytes, string password)
{
    using (SymmetricAlgorithm algorithm = GetAlgorithm(password))
    using (ICryptoTransform encryptor = algorithm.CreateEncryptor())
    using (MemoryStream ms = new MemoryStream())
    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
    {
        cs.Write(clearBytes, 0, clearBytes.Length);
        cs.FlushFinalBlock();
        return ms.ToArray();
    }
}

public static byte[] DecryptBytes(byte[] cipherBytes, string password)
{
    using (SymmetricAlgorithm algorithm = GetAlgorithm(password))
    using (ICryptoTransform decryptor = algorithm.CreateDecryptor())
    using (MemoryStream ms = new MemoryStream())
    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
    {
        cs.Write(cipherBytes, 0, cipherBytes.Length); //here is the exception thrown
        cs.FlushFinalBlock();
        return ms.ToArray();
    }
}