加密/解密后生成的文件无效

时间:2014-08-21 14:41:51

标签: c# encryption cryptography byte aes

我已经创建了一个加密应用程序,它工作正常,直到我开始在块中进行加密/解密。这是我的加密/解密代码:

public static class AESCryptography
{
    private const int keysize = 256;
    public static void Encrypt(string Input, string passPhrase)
    {
        var chunkSize = 16;
        PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
        byte[] keyBytes = password.GetBytes(keysize / 8);
        FileStream fsOutput = File.OpenWrite(Input.Substring(0, Input.LastIndexOf('.')) + ".aent");
        FileStream fsInput = File.OpenRead(Input);
        RijndaelManaged symmetricKey = new RijndaelManaged();
        symmetricKey.GenerateIV();
        symmetricKey.Mode = CipherMode.CBC;
        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, symmetricKey.IV);
        CryptoStream cryptoStream = new CryptoStream(fsOutput, encryptor, CryptoStreamMode.Write);
        fsOutput.Write(symmetricKey.IV, 0, symmetricKey.IV.Length);
        for (long i = 0; i < fsInput.Length; i += chunkSize)
        {
            byte[] chunkData = new byte[chunkSize];
            int bytesRead = 0;
            while ((bytesRead = fsInput.Read(chunkData, 0, chunkSize)) > 0)
            {
                if (bytesRead != chunkSize)
                {
                    for (int x = bytesRead - 1; x < chunkSize; x++)
                    {
                        chunkData[x] = 0;
                    }
                }
                cryptoStream.Write(chunkData, 0, chunkSize);
            }
        }
        cryptoStream.FlushFinalBlock();
        cryptoStream.Dispose();
        fsOutput.Dispose();
        fsInput.Dispose();
        encryptor.Dispose();
        symmetricKey.Dispose();
        password.Dispose();
    }

    public static void Decrypt(string Input, string passPhrase)
    {
        var chunkSize = 16;
        PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
        byte[] keyBytes = password.GetBytes(keysize / 8);
        FileStream fsInput = File.OpenRead(Input);
        FileStream fsOutput = File.OpenWrite(Input.Substring(0, Input.LastIndexOf('.')) + ".txt");
        byte[] initVectorBytes = new byte[16];
        byte[] buffer = new byte[8];
        fsInput.Read(buffer, 0, 8);
        long fileLength = BitConverter.ToInt64(buffer, 0);
        fsInput.Read(initVectorBytes, 0, 16);
        RijndaelManaged symmetricKey = new RijndaelManaged();
        symmetricKey.Mode = CipherMode.CBC;
        ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
        CryptoStream cryptoStream = new CryptoStream(fsOutput, decryptor, CryptoStreamMode.Write);
        for (long i = 0; i < fsInput.Length; i += chunkSize)
        {
            byte[] chunkData = new byte[chunkSize];
            int bytesRead = 0;
            while ((bytesRead = fsInput.Read(chunkData, 0, chunkSize)) > 0)
            {
                cryptoStream.Write(chunkData, 0, bytesRead);
            }
        }
    }
}

如果我加密.txt文件,例如:&#34; hello world!该应用程序工作!!&#34;我会得到一些尴尬的中文文字和我从未见过的符号。

1 个答案:

答案 0 :(得分:1)

你留下了一些不完整的代码。 Decrypt中的此代码是一个问题:

byte[] buffer = new byte[8];
fsInput.Read(buffer, 0, 8);
long fileLength = BitConverter.ToInt64(buffer, 0);

这将读取从未由Encrypt写入的八个字节,将之后读取的所有内容移动八个字节。注释掉这三行,你的代码就可以了。

在Decrypt循环之后,你也错过了一个cryptoStream.FlushFinalBlock()。由于bug的8字节偏移量,您将收到CryptographicException“要解密的数据长度无效”。 (你错过了8个字节。)但在评论出这些行之后,FlushFinalBlock()将会成功。