C# 压缩文件并加密(AES)以及解压缩

public static void Main()
{
    string originalFilePath = "D:\\文档\\工作\\新建文件夹.7z"; // 要加密压缩的文件
    string encryptedCompressedFilePath = "D:\\文档\\工作\\test222"; // 加密压缩后的文件
    string decryptedDecompressedFilePath = "D:\\文档\\工作\\test222.7z"; // 解密解压缩后的文件

    string password = "123456"; // 加密密码
    string salt = "123456"; // 加密盐值

    // 加密压缩文件
    EncryptAndCompressFile(originalFilePath, encryptedCompressedFilePath, password, salt);
    Console.WriteLine("文件已加密并压缩。");

    // 解密解压缩文件
    DecryptAndDecompressFile(encryptedCompressedFilePath, decryptedDecompressedFilePath, password, salt);
    Console.WriteLine("文件已解密并解压缩。");
}

static void EncryptAndCompressFile(string inputFilePath, string outputFilePath, string password, string salt)
{
    using (Aes aesAlg = Aes.Create())
    {
        // 生成密钥和IV
        Rfc2898DeriveBytes keyDerivation = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt));
        aesAlg.Key = keyDerivation.GetBytes(aesAlg.KeySize / 8);
        //aesAlg.GenerateIV(); // 使用随机生成的IV
        aesAlg.IV = keyDerivation.GetBytes(aesAlg.BlockSize / 8);

        // 创建加密器
        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

        // 打开输入文件
        using (FileStream inputFileStream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read))
        {
            // 创建输出文件
            using (FileStream outputFileStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
            {
                // 写入IV到输出文件
                outputFileStream.Write(aesAlg.IV, 0, aesAlg.IV.Length);

                // 使用CryptoStream加密
                using (CryptoStream cryptoStream = new CryptoStream(outputFileStream, encryptor, CryptoStreamMode.Write))
                {
                    // 使用GZip压缩
                    using (GZipStream compressionStream = new GZipStream(cryptoStream, CompressionMode.Compress))
                    {
                        inputFileStream.CopyTo(compressionStream);
                    }
                }
            }
        }
    }
}
static void DecryptAndDecompressFile(string inputFilePath, string outputFilePath, string password, string salt)
{
    using (Aes aesAlg = Aes.Create())
    {
        // 生成密钥
        Rfc2898DeriveBytes keyDerivation = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt));
        aesAlg.Key = keyDerivation.GetBytes(aesAlg.KeySize / 8);

        // 打开加密压缩的文件
        using (FileStream inputFileStream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read))
        {
            // 读取IV
            byte[] iv = new byte[aesAlg.BlockSize / 8];
            inputFileStream.Read(iv, 0, iv.Length);
            aesAlg.IV = iv;

            // 创建解密器
            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

            // 使用CryptoStream解密
            using (CryptoStream cryptoStream = new CryptoStream(inputFileStream, decryptor, CryptoStreamMode.Read))
            {
                // 使用GZip解压缩
                using (GZipStream decompressionStream = new GZipStream(cryptoStream, CompressionMode.Decompress))
                {
                    // 创建输出文件
                    using (FileStream outputFileStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
                    {
                        decompressionStream.CopyTo(outputFileStream);
                    }
                }
            }
        }
    }
}

你可能感兴趣的:(c#,AES)