【问题标题】:Encrypt and decrypt a file with aes in c#?在c#中用aes加密和解密文件?
【发布时间】:2016-11-29 23:47:55
【问题描述】:

我想知道在 C# 中是否有使用 AES 加密和解密文件的代码?我看过一些关于使用 aes 在 c# 中加密和解密文本的代码,但是 在c#中加密和解密文件..没有完整的代码可以很好地理解它..如果有人可以帮助我吗?

【问题讨论】:

标签: c# encryption aes


【解决方案1】:

一般来说,您不想加密文件。也就是说,你不想写一个文件,然后加密它。数据可能位于存储设备的不同扇区中,并且很可能可以恢复。 (当然,如果您尝试编写勒索软件,无论如何都写得不好)。相反,您要做的是在内容进入磁盘之前对其进行加密。

你要求什么

public static void EncryptFile(string filePath, byte[] key)
{
    string tempFileName = Path.GetTempFileName();

    using (SymmetricAlgorithm cipher = Aes.Create())
    using (FileStream fileStream = File.OpenRead(filePath))
    using (FileStream tempFile = File.Create(tempFileName))
    {
        cipher.Key = key;
        // aes.IV will be automatically populated with a secure random value
        byte[] iv = cipher.IV;

        // Write a marker header so we can identify how to read this file in the future
        tempFile.WriteByte(69);
        tempFile.WriteByte(74);
        tempFile.WriteByte(66);
        tempFile.WriteByte(65);
        tempFile.WriteByte(69);
        tempFile.WriteByte(83);

        tempFile.Write(iv, 0, iv.Length);

        using (var cryptoStream =
            new CryptoStream(tempFile, cipher.CreateEncryptor(), CryptoStreamMode.Write))
        {
            fileStream.CopyTo(cryptoStream);
        }
    }

    File.Delete(filePath);
    File.Move(tempFileName, filePath);
}

public static void DecryptFile(string filePath, byte[] key)
{
    string tempFileName = Path.GetTempFileName();

    using (SymmetricAlgorithm cipher = Aes.Create())
    using (FileStream fileStream = File.OpenRead(filePath))
    using (FileStream tempFile = File.Create(tempFileName))
    {
        cipher.Key = key;
        byte[] iv = new byte[cipher.BlockSize / 8];
        byte[] headerBytes = new byte[6];
        int remain = headerBytes.Length;

        while (remain != 0)
        {
            int read = fileStream.Read(headerBytes, headerBytes.Length - remain, remain);

            if (read == 0)
            {
                throw new EndOfStreamException();
            }

            remain -= read;
        }

        if (headerBytes[0] != 69 ||
            headerBytes[1] != 74 ||
            headerBytes[2] != 66 ||
            headerBytes[3] != 65 ||
            headerBytes[4] != 69 ||
            headerBytes[5] != 83)
        {
            throw new InvalidOperationException();
        }

        remain = iv.Length;

        while (remain != 0)
        {
            int read = fileStream.Read(iv, iv.Length - remain, remain);

            if (read == 0)
            {
                throw new EndOfStreamException();
            }

            remain -= read;
        }

        cipher.IV = iv;

        using (var cryptoStream =
            new CryptoStream(tempFile, cipher.CreateDecryptor(), CryptoStreamMode.Write))
        {
            fileStream.CopyTo(cryptoStream);
        }
    }

    File.Delete(filePath);
    File.Move(tempFileName, filePath);
}

你真正想要的

不要通过 FileStream 写入原始文件,而是打开文件,写入标题和 IV,创建 CryptoStream,然后对所有内容使用 CryptoStream。没有理由让未加密的表单存在磁盘上。

【讨论】:

    猜你喜欢
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    相关资源
    最近更新 更多