国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

對稱加密之AES、壓縮解壓以及壓縮加密解密解壓綜合實戰(zhàn)

2019-11-17 02:29:41
字體:
來源:轉載
供稿:網(wǎng)友

對稱加密之AES、壓縮解壓以及壓縮加密解密解壓綜合實戰(zhàn)

  1. AES
  2. 壓縮解壓
  3. 壓縮加密解密解壓

對稱加密:

就是采用這種加密方法的雙方使用方式用同樣的密鑰進行加密和解密。密鑰是控制加密及解密過程的指令。算法是一組規(guī)則,規(guī)定如何進行加密和解密。因此加密的安全性不僅取決于加密算法本身,密鑰管理的安全性更是重要。因為加密和解密都使用同一個密鑰,如何把密鑰安全地傳遞到解密者手上就成了必須要解決的問題。由此可見密鑰傳遞也是比較重要的一環(huán),一般都是通過對密鑰二次加密的方式,進行密鑰的傳輸加密實現(xiàn)代碼:
        public static byte[] encryptStringToBytes_AES(byte[] fileContentBytes, byte[] Key, byte[] IV)        {            // Check arguments.            if (fileContentBytes == null || fileContentBytes.Length <= 0)                throw new ArgumentNullException("plainText");            if (Key == null || Key.Length <= 0)                throw new ArgumentNullException("Key");            if (IV == null || IV.Length <= 0)                throw new ArgumentNullException("IV");            MemoryStream msEncrypt = null;            AesCryptoServicePRovider aesAlg = null;            try            {                aesAlg = new AesCryptoServiceProvider();                aesAlg.Padding = PaddingMode.PKCS7;                aesAlg.Key = Key;                aesAlg.IV = IV;                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);                msEncrypt = new MemoryStream();                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))                {                    csEncrypt.Write(fileContentBytes, 0, fileContentBytes.Length);                    csEncrypt.FlushFinalBlock();                }            }            catch (Exception ex)            {            }            finally            {                if (aesAlg != null)                    aesAlg.Clear();            }            return msEncrypt.ToArray();        }

解密代碼實現(xiàn):


        public static byte[] decryptBytes(byte[] cipherText, byte[] Key, byte[] IV)        {            if (cipherText == null || cipherText.Length <= 0)                throw new ArgumentNullException("cipherText");            if (Key == null || Key.Length <= 0)                throw new ArgumentNullException("Key");            if (IV == null || IV.Length <= 0)                throw new ArgumentNullException("IV");            AesCryptoServiceProvider aesAlg = null;            byte[] buffer = null;            try            {                using (aesAlg = new AesCryptoServiceProvider())                {                    aesAlg.Padding = PaddingMode.PKCS7;                    aesAlg.Key = Key;                    aesAlg.IV = IV;                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);                    using (MemoryStream msDecrypt = new MemoryStream(cipherText))                    {                        CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);                        byte[] tempbuffer = new byte[cipherText.Length];                        int totalBytesRead = csDecrypt.Read(tempbuffer, 0, tempbuffer.Length);                        buffer = tempbuffer.Take(totalBytesRead).ToArray();                    }                }            }            catch (Exception ex)            {            }            finally            {                if (aesAlg != null)                    aesAlg.Clear();            }            return buffer;        }

客戶端加密解密文本文件實戰(zhàn):


        /// <summary>        /// 加密解密        /// </summary>        private static void _EncryptAndDecrypt()        {            ASCIIEncoding asciiEnc = new ASCIIEncoding();            byte[] initVectorBytes = asciiEnc.GetBytes("@1B2c3D4e5F6g7H8");            //Randomly generate or Book key - key K2 - Key to encrypt xml content            string keyK2 = Generator.RandomString(10);            //Generate the 128 bit string using md5 for key K2            MD5 hashProvider = MD5.Create();            byte[] md5EncryptedKeyK2 = hashProvider.ComputeHash(asciiEnc.GetBytes(keyK2));            string filename = "NewTextDocument.txt";            string filepath = Environment.CurrentDirectory + "http://" + filename;            byte[] Content = Encryption.encryptStringToBytes_AES(File.ReadAllBytes(filepath), md5EncryptedKeyK2, initVectorBytes);            string encryptfilepath = Environment.CurrentDirectory + "http://encrypt" + filename;            File.WriteAllBytes(encryptfilepath, Content);            byte[] decryptContent = Encryption.decryptBytes(File.ReadAllBytes(encryptfilepath), md5EncryptedKeyK2, initVectorBytes);            string decryptfilepath = Environment.CurrentDirectory + "http://decrypt" + filename;            File.WriteAllBytes(decryptfilepath, decryptContent);        }

壓縮解壓:


            string filename = "NewTextDocument.txt";            string filepath = Environment.CurrentDirectory + "http://" + filename;            string zipfilepath = Environment.CurrentDirectory + "http://NewTextDocument.zip";            using (ZipFile contentZip = new ZipFile())            {                //壓縮                contentZip.AlternateEncoding = Encoding.GetEncoding("iso-8859-1");                contentZip.AlternateEncodingUsage = ZipOption.Always;                ZipEntry contentFile = contentZip.AddEntry(filename, File.ReadAllBytes(filepath));                contentZip.Save(zipfilepath);                //解壓                contentZip.ExtractAll(Environment.CurrentDirectory);            }

壓縮加密解密解壓:


 string filename = "NewTextDocument.zip";            string filepath = Environment.CurrentDirectory + "http://" + filename;            string zipfilepath = Environment.CurrentDirectory + "http://" + filename;            ZipFile contentZip = new ZipFile();            contentZip.AlternateEncoding = Encoding.GetEncoding("iso-8859-1");            contentZip.AlternateEncodingUsage = ZipOption.Always;            var bytecontent = File.ReadAllBytes(Environment.CurrentDirectory + "http://NewTextDocument.txt");            ZipEntry contentFile = contentZip.AddEntry("NewTextDocument.txt", bytecontent);            contentZip.Save(zipfilepath);            ASCIIEncoding asciiEnc = new ASCIIEncoding();            byte[] initVectorBytes = asciiEnc.GetBytes("@1B2c3D4e5F6g7H8");            //Randomly generate or Book key - key K2 - Key to encrypt xml content            string keyK2 = Generator.RandomString(10);            //Generate the 128 bit string using MD5 for key K2            MD5 hashProvider = MD5.Create();            byte[] md5EncryptedKeyK2 = hashProvider.ComputeHash(asciiEnc.GetBytes(keyK2));            byte[] Content = Encryption.encryptStringToBytes_AES(File.ReadAllBytes(filepath), md5EncryptedKeyK2, initVectorBytes);            string encryptfilepath = Environment.CurrentDirectory + "http://encrypt" + filename;            File.WriteAllBytes(encryptfilepath, Content);            byte[] decryptContent = Encryption.decryptBytes(File.ReadAllBytes(encryptfilepath), md5EncryptedKeyK2, initVectorBytes);            string decryptfilepath = Environment.CurrentDirectory + "http://decrypt" + filename;            File.WriteAllBytes(decryptfilepath, decryptContent);            contentZip.ExtractAll(Environment.CurrentDirectory + "http://unzip//decrypt");            string key = Convert.ToBase64String(md5EncryptedKeyK2);            string iv = Convert.ToBase64String(initVectorBytes);            Console.WriteLine(key);            Console.WriteLine(iv);            byte[] decryptContent1 = Encryption.decryptBytes(File.ReadAllBytes(encryptfilepath), Convert.FromBase64String(key), Convert.FromBase64String(iv));            string decryptfilepath1 = Environment.CurrentDirectory + "http://decrypt1" + filename;            contentZip.ExtractAll(Environment.CurrentDirectory + "http://unzip//decrypt1");            File.WriteAllBytes(decryptfilepath1, decryptContent1);

 

參考文章:NET對稱加密體系

下載地址


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 会同县| 抚宁县| 安多县| 蒙山县| 辽阳县| 皋兰县| 达拉特旗| 万载县| 新沂市| 陆良县| 泰宁县| 当涂县| 偏关县| 余姚市| 筠连县| 喜德县| 来凤县| 韶山市| 库尔勒市| 峨边| 西安市| 綦江县| 洞口县| 榆树市| 商都县| 青州市| 高平市| 老河口市| 琼海市| 浑源县| 博兴县| 白河县| 扶余县| 泾源县| 禄劝| 文山县| 昌图县| 阳新县| 华亭县| 内黄县| 衡南县|