首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >加密/解密后生成的无效文件

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

Stack Overflow用户
提问于 2014-08-21 14:41:50
回答 1查看 81关注 0票数 0

我已经制作了一个密码应用程序,它一直运行良好,直到我开始分块进行加密/解密。这是我的加密/解密代码:

代码语言:javascript
运行
复制
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文件,例如:“你好世界!应用程序工作!”我会得到一些笨拙的中文文字与符号,我从来没有见过。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-21 16:13:28

你留下了一些代码不完整。这里的解密代码是一个问题:

代码语言:javascript
运行
复制
byte[] buffer = new byte[8];
fsInput.Read(buffer, 0, 8);
long fileLength = BitConverter.ToInt64(buffer, 0);

这将读取8个从未被加密过的字节,然后将所读取的所有内容转换为8个字节。注释掉这三行,您的代码就能工作了。

在解密循环之后,您还丢失了一个cryptoStream.FlushFinalBlock()。由于错误的8字节偏移量,您将得到一个CryptographicException“要解密的数据长度无效”。(您遗漏了8个字节。)但是,在注释掉这些行之后,FlushFinalBlock()将成功。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25429275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档