• .net aes加密视频等文件


    公司学习平台在app端下载下来的视频需要加密

    随查找资料参考一些写法

    写了aes的加密方法

    记录防止忘记

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Class1
        {
            private const string SKey = "_?73^?dVT3st5har3";
            private const string SaltKey = "!2S@LT&KT3st5har3EY";
            private const int Iterations = 1042; // Recommendation is >= 1000
            static byte[] GetBytes(string str)
            {
                var bytes = new byte[str.Length * sizeof(char)];
                Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
                return bytes;
            }
            static string GenerateKey()
            {
                // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
                DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
    
                // Use the Automatically generated key for Encryption. 
                return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
    
            }
            public static void EncryptFile(string srcFilename, string destFilename)
            {
                var aes = new AesManaged();
                aes.BlockSize = 128;
                aes.KeySize = 256;
                var salt = GetBytes(SaltKey);
                var key = new Rfc2898DeriveBytes(SKey, salt, Iterations);
                aes.Key = key.GetBytes(aes.KeySize / 8);
                aes.IV = key.GetBytes(aes.BlockSize / 8);
                aes.Mode = CipherMode.CBC;
                ICryptoTransform transform = aes.CreateEncryptor(aes.Key, aes.IV);
                using (var dest = new FileStream(destFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))
                {
                    using (var cryptoStream = new CryptoStream(dest, transform, CryptoStreamMode.Write))
                    {
                        using (var source = new FileStream(srcFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            source.CopyTo(cryptoStream);
                        }
                    }
                }
            }
            public static void DecryptFile(string srcFilename, string destFilename)
            {
                var aes = new AesManaged();
                aes.BlockSize = 128;
                aes.KeySize = 256;
                var salt = GetBytes(SaltKey);
                var key = new Rfc2898DeriveBytes(SKey, salt, Iterations);
                aes.Key = key.GetBytes(aes.KeySize / 8);
                aes.IV = key.GetBytes(aes.BlockSize / 8);
                aes.Mode = CipherMode.CBC;
                ICryptoTransform transform = aes.CreateDecryptor(aes.Key, aes.IV);
    
                using (var dest = new FileStream(destFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))
                {
                    using (var cryptoStream = new CryptoStream(dest, transform, CryptoStreamMode.Write))
                    {
                        try
                        {
                            using (var source = new FileStream(srcFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
                            {
                                source.CopyTo(cryptoStream);
                            }
                        }
                        catch (CryptographicException exception)
                        {
                            throw new ApplicationException("Decryption failed.", exception);
                        }
                    }
                }
            }
        }
    }

    运行

        static void Main(string[] args)
            {
               // Class1.EncryptFile(@"C:kc_Page_2.mp4", @"C:kc_Page_3.mp4");
                //Class1.DecryptFile(@"C:kc_Page_3.mp4", @"C:kc_Page_4.mp4");
                Class1.EncryptFile(@"C:1.docx", @"C:2.docx");
                Class1.DecryptFile(@"C:2.docx", @"C:3.docx");
        
            }
  • 相关阅读:
    ACM的探索之Keen On Evrything But Triangle(我觉得可以很接近啦!!)
    ACM的探索之Just Skip The Problem
    ACM的探索之Everything Is Generated In Equal Probability(这真的是很有趣的话语丫!)
    心情散记
    识别有效的IP地址和掩码并进行分类统计
    网络流复习计划
    CF1076D Edge Deletion
    bzoj4563 HAOI2016放旗子
    BZOJ2152聪聪可可
    BZOJ3224普通平衡树
  • 原文地址:https://www.cnblogs.com/zhang888/p/4178501.html
Copyright © 2020-2023  润新知