AES上传文件加密下载文件解密(完整,附助手实体类)

首先可以自定义实体类,秘钥是最重要的,不要透露给任何人,来看助手类

    public class AesHelper
    {
        //
        // 摘要:
        //     默认密钥
        private const string PublicKey = "88888jghe8888uh8";

        //
        // 摘要:
        //     默认向量
        private const string Iv = "abudkdghij8888p8";

        //
        // 摘要:
        //     AES加密 (使用系统内置密钥)
        //
        // 参数:
        //   str:
        //     需要加密字符串
        //
        // 返回结果:
        //     加密后字符串
        public static string Encrypt(string str)
        {
            return Encrypt(str, "88888jghe8888uh8");
        }

        //
        // 摘要:
        //     AES解密 (使用系统内置密钥)
        //
        // 参数:
        //   str:
        //     需要解密字符串
        //
        // 返回结果:
        //     解密后字符串
        public static string Decrypt(string str)
        {
            return Decrypt(str, "88888jghe8888uh8");
        }

        //
        // 摘要:
        //     AES加密
        //
        // 参数:
        //   str:
        //     需要加密的字符串
        //
        //   key:
        //     32位密钥(如果小于32位则自动补0)
        //
        // 返回结果:
        //     加密后的字符串
        public static string Encrypt(string str, string key)
        {
            key = Key32Handler(key);
            return Encrypt(str, key, IvHandler("abudkdghij8888p8"));
        }

        //
        // 摘要:
        //     AES解密
        //
        // 参数:
        //   str:
        //     需要解密的字符串
        //
        //   key:
        //     32位密钥(如果小于32位则自动补0)
        //
        // 返回结果:
        //     解密后的字符串
        public static string Decrypt(string str, string key)
        {
            key = Key32Handler(key);
            return Decrypt(str, key, IvHandler("abudkdghij8888p8"));
        }

        //
        // 摘要:
        //     AES解密
        //
        // 参数:
        //   str:
        //     需要解密的字符串
        //
        //   key:
        //     16位密钥(如果小于16位则自动补0)
        //
        //   iv:
        //     16位偏向量(如果小于16位则自动补0)
        //
        // 返回结果:
        //     解密后的字符串
        public static string Decrypt16(string str, string key, string iv)
        {
            key = Key16Handler(key);
            return Decrypt(str, key, IvHandler(iv));
        }

        //
        // 摘要:
        //     AES加密
        //
        // 参数:
        //   str:
        //     需要加密的字符串
        //
        //   key:
        //     16位密钥(如果小于16位则自动补0)
        //
        //   iv:
        //     16位偏向量(如果小于16位则自动补0)
        //
        // 返回结果:
        //     加密后的字符串
        public static string Encrypt16(string str, string key, string iv)
        {
            key = Key16Handler(key);
            return Encrypt(str, key, IvHandler(iv));
        }

        //
        // 摘要:
        //     AES解密
        //
        // 参数:
        //   str:
        //     需要解密的字符串
        //
        //   key:
        //     32位密钥(如果小于32位则自动补0)
        //
        //   iv:
        //     16位偏向量(如果小于16位则自动补0)
        //
        // 返回结果:
        //     解密后的字符串
        public static string Decrypt32(string str, string key, string iv)
        {
            key = Key32Handler(key);
            return Decrypt(str, key, IvHandler(iv));
        }

        //
        // 摘要:
        //     AES加密
        //
        // 参数:
        //   str:
        //     需要加密的字符串
        //
        //   key:
        //     32位密钥(如果小于32位则自动补0)
        //
        //   iv:
        //     16位偏向量(如果小于16位则自动补0)
        //
        // 返回结果:
        //     加密后的字符串
        public static string Encrypt32(string str, string key, string iv)
        {
            key = Key32Handler(key);
            return Encrypt(str, key, IvHandler(iv));
        }

        //
        // 摘要:
        //     AES解密
        //
        // 参数:
        //   str:
        //     需要解密的字符串
        //
        //   key:
        //     16/32位密钥
        //
        //   iv:
        //     16位偏向量(如果小于16位则自动补0)
        //
        // 返回结果:
        //     解密后的字符串
        public static string Decrypt(string str, string key, string iv)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(key);
            byte[] array = Convert.FromBase64String(str);
            byte[] bytes2 = new RijndaelManaged
            {
                Key = bytes,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7,
                IV = Encoding.UTF8.GetBytes(IvHandler(iv))
            }.CreateDecryptor().TransformFinalBlock(array, 0, array.Length);
            return Encoding.UTF8.GetString(bytes2);
        }

        //
        // 摘要:
        //     AES加密
        //
        // 参数:
        //   str:
        //     需要加密的字符串
        //
        //   key:
        //     16/32位密钥
        //
        //   iv:
        //     16位偏向量(如果小于16位则自动补0)
        //
        // 返回结果:
        //     加密后的字符串
        public static string Encrypt(string str, string key, string iv)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(key);
            byte[] bytes2 = Encoding.UTF8.GetBytes(str);
            byte[] array = new RijndaelManaged
            {
                Key = bytes,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7,
                IV = Encoding.UTF8.GetBytes(IvHandler(iv))
            }.CreateEncryptor().TransformFinalBlock(bytes2, 0, bytes2.Length);
            return Convert.ToBase64String(array, 0, array.Length);
        }

        private static string Key32Handler(string key)
        {
            if (key.Length > 32)
            {
                throw new Exception("key(" + key + ")长度超过32位");
            }

            if (key.Length < 32)
            {
                key = key.PadRight(32, '0');
            }

            return key;
        }

        private static string Key16Handler(string key)
        {
            if (key.Length > 16)
            {
                throw new Exception("key(" + key + ")长度超过16位");
            }

            if (key.Length < 16)
            {
                key = key.PadRight(16, '0');
            }

            return key;
        }

        private static string IvHandler(string iv)
        {
            if (iv.Length > 16)
            {
                throw new Exception("iv(" + iv + ")长度超过16位");
            }

            if (iv.Length < 16)
            {
                iv = iv.PadRight(16, '0');
            }

            return iv;
        }

        //
        // 摘要:
        //     AES加密 (使用系统内置密钥)
        //
        // 参数:
        //   bs:
        //     字节数组
        //
        // 返回结果:
        //     加密后字符串
        public static byte[] Encrypt(byte[] bs)
        {
            return Encrypt32(bs, "88888jghe8888uh8");
        }

        //
        // 摘要:
        //     AES解密 (使用系统内置密钥)
        //
        // 参数:
        //   bs:
        //     字节数组
        //
        // 返回结果:
        //     解密后字符串
        public static byte[] Decrypt(byte[] bs)
        {
            return Decrypt32(bs, "88888jghe8888uh8");
        }

        //
        // 摘要:
        //     字节数组加密
        //
        // 参数:
        //   bs:
        //     字节数组
        //
        //   key:
        //     32位密钥(如果小于32位则自动补0)
        public static byte[] Encrypt32(byte[] bs, string key)
        {
            key = Key32Handler(key);
            return Encrypt(bs, key, "abudkdghij8888p8");
        }

        //
        // 摘要:
        //     字节数组解密
        //
        // 参数:
        //   bs:
        //     加密后的字节数组
        //
        //   key:
        //     32位密钥(如果小于32位则自动补0)
        public static byte[] Decrypt32(byte[] bs, string key)
        {
            key = Key32Handler(key);
            return Decrypt(bs, key, "abudkdghij8888p8");
        }

        //
        // 摘要:
        //     字节数组加密
        //
        // 参数:
        //   bs:
        //     字节数组
        //
        //   key:
        //     16位密钥(如果小于16位则自动补0)
        public static byte[] Encrypt16(byte[] bs, string key)
        {
            key = Key16Handler(key);
            return Encrypt(bs, key, "abudkdghij8888p8");
        }

        //
        // 摘要:
        //     字节数组解密
        //
        // 参数:
        //   bs:
        //     加密后的字节数组
        //
        //   key:
        //     16位密钥(如果小于16位则自动补0)
        public static byte[] Decrypt16(byte[] bs, string key)
        {
            key = Key16Handler(key);
            return Decrypt(bs, key, "abudkdghij8888p8");
        }

        //
        // 摘要:
        //     字节数组解密
        //
        // 参数:
        //   bs:
        //     加密后的字节数组
        //
        //   key:
        //     16位密钥(如果小于16位则自动补0)
        //
        //   iv:
        //     16位偏向量(如果小于16位则自动补0)
        public static byte[] Decrypt16(byte[] bs, string key, string iv)
        {
            key = Key16Handler(key);
            return Decrypt(bs, key, iv);
        }

        //
        // 摘要:
        //     字节数组加密
        //
        // 参数:
        //   bs:
        //     字节数组
        //
        //   key:
        //     16位密钥(如果小于16位则自动补0)
        //
        //   iv:
        //     16位偏向量(如果小于16位则自动补0)
        public static byte[] Encrypt16(byte[] bs, string key, string iv)
        {
            key = Key16Handler(key);
            return Encrypt(bs, key, iv);
        }

        //
        // 摘要:
        //     字节数组解密
        //
        // 参数:
        //   bs:
        //     加密后的字节数组
        //
        //   key:
        //     32位密钥(如果小于16位则自动补0)
        //
        //   iv:
        //     16位偏向量(如果小于16位则自动补0)
        public static byte[] Decrypt32(byte[] bs, string key, string iv)
        {
            key = Key32Handler(key);
            return Decrypt(bs, key, iv);
        }

        //
        // 摘要:
        //     字节数组加密
        //
        // 参数:
        //   bs:
        //     字节数组
        //
        //   key:
        //     32位密钥(如果小于32位则自动补0)
        //
        //   iv:
        //     16位偏向量(如果小于16位则自动补0)
        public static byte[] Encrypt32(byte[] bs, string key, string iv)
        {
            key = Key32Handler(key);
            return Encrypt(bs, key, iv);
        }

        //
        // 摘要:
        //     字节数组解密
        //
        // 参数:
        //   bs:
        //     加密后的字节数组
        //
        //   key:
        //     16/32位密钥
        //
        //   iv:
        //     16位偏向量(如果小于16位则自动补0)
        public static byte[] Decrypt(byte[] bs, string key, string iv)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(key);
            return new RijndaelManaged
            {
                Key = bytes,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7,
                IV = Encoding.UTF8.GetBytes(IvHandler(iv))
            }.CreateDecryptor().TransformFinalBlock(bs, 0, bs.Length);
        }

        //
        // 摘要:
        //     字节数组加密
        //
        // 参数:
        //   bs:
        //     字节数组
        //
        //   key:
        //     16/32位密钥
        //
        //   iv:
        //     16位偏向量(如果小于16位则自动补0)
        public static byte[] Encrypt(byte[] bs, string key, string iv)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(key);
            return new RijndaelManaged
            {
                Key = bytes,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7,
                IV = Encoding.UTF8.GetBytes(IvHandler(iv))
            }.CreateEncryptor().TransformFinalBlock(bs, 0, bs.Length);
        }
    }

实际调用时 我们需要 读--加密--写    读--解密--写--删

          //--------------------------------加密--------------------------------
            //上传   
            byte[] Newbuffur = AuthGetFileData(@"E:待上传文件交接事宜.docx");
            Bytes2File(Newbuffur, @"F:加密后文件", "交接事宜.docx");


            ////下载
            byte[] UpLoadbuffur = LoadGetFileData(@"F:加密后文件Enclosure交接事宜.docx");
            Bytes2File(UpLoadbuffur, @"F:加密解密后下载", "交接事宜.docx");

调用

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="fileUrl">文件路径文件名称</param>
        /// <returns>byte[]</returns>

        protected byte[] AuthGetFileData(string fileUrl)
        {
            using (FileStream fs = new FileStream(fileUrl, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {

                byte[] buffur = new byte[fs.Length];
                //从当前读写指针的位置往后读取字节的数据到字节数组中
                //buffur = File2Bytes(fileUrl);
                fs.Read(buffur, 0, buffur.Length);
                //读写指针从开头往后移动字节
                fs.Seek(0, SeekOrigin.Begin);
                byte[] NewByte = AesHelper.Encrypt(buffur);
                return NewByte;
            }
        }




        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="fileUrl">文件路径文件名称</param>
        /// <returns>byte[]</returns>

        protected byte[] LoadGetFileData(string fileUrl)
        {
            using (FileStream fs = new FileStream(fileUrl, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                byte[] buffur = new byte[fs.Length];
                //buffur = File2Bytes(fileUrl);
                //从当前读写指针的位置往后读取字节的数据到字节数组中
                fs.Read(buffur, 0, buffur.Length);
                //读写指针从开头往后移动字节
                fs.Seek(0, SeekOrigin.Begin);

                byte[] NewByte = AesHelper.Decrypt(buffur);
                return NewByte;
            }
        }



        /// <summary>
        /// 将byte数组转换为文件并保存到指定地址
        /// </summary>
        /// <param name="buff">byte数组</param>
        /// <param name="savepath">保存地址</param>
        public static void Bytes2File(byte[] buff, string savepath, string fileName)
        {
            try
            {
                if (!System.IO.File.Exists(savepath  + fileName))
                {
                    System.IO.File.Create(savepath + fileName).Close(); 
                }
                //创建Process命令
                var cmd = new Process();
                using (FileStream fs = new FileStream(savepath  + fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(buff, 0, buff.Length);

                    bw.Close();
                    fs.Close();

                    fs.Dispose();
                    bw.Dispose();
                } 
            }
            catch (Exception e)
            {
                string kk = e.Message;
            }

        }
原文地址:https://www.cnblogs.com/ning-xiaowo/p/13594698.html