对称加密之AES及压缩加密解密解压综合实践
发布时间:2022-05-11 09:40:16 所属栏目:安全 来源:互联网
导读:对称加密:就是采用这种加密方法的双方使用方式用同样的密钥进行加密和解密。密钥是控制加密及解密过程的指令。算法是一组规则,规定如何进行加密和解密。 由此可见密钥传递也是比较重要的一环,一般都是通过对密钥二次加密的方式,进行密钥的传输 加密实现
对称加密:就是采用这种加密方法的双方使用方式用同样的密钥进行加密和解密。密钥是控制加密及解密过程的指令。算法是一组规则,规定如何进行加密和解密。 由此可见密钥传递也是比较重要的一环,一般都是通过对密钥二次加密的方式,进行密钥的传输 加密实现代码: 复制 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(); 解密代码实现: 复制 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(); 复制 string filename = "NewTextDocument.txt"; string filepath = Environment.CurrentDirectory + "" + filename; string zipfilepath = Environment.CurrentDirectory + "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); (编辑:东莞站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐