对称加密之AES、压缩解压以及压缩加密解密解压综合实战
对称加密:

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();
}
解密代码实现:
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;
}
客户端加密解密文本文件实战:
/// <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 + "\\" + filename; byte[] Content = Encryption.encryptStringToBytes_AES(File.ReadAllBytes(filepath), md5EncryptedKeyK2, initVectorBytes);
string encryptfilepath = Environment.CurrentDirectory + "\\encrypt" + filename;
File.WriteAllBytes(encryptfilepath, Content); byte[] decryptContent = Encryption.decryptBytes(File.ReadAllBytes(encryptfilepath), md5EncryptedKeyK2, initVectorBytes);
string decryptfilepath = Environment.CurrentDirectory + "\\decrypt" + filename;
File.WriteAllBytes(decryptfilepath, decryptContent); }
压缩解压:
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);
}
压缩加密解密解压:
string filename = "NewTextDocument.zip";
string filepath = Environment.CurrentDirectory + "\\" + filename;
string zipfilepath = Environment.CurrentDirectory + "\\" + filename;
ZipFile contentZip = new ZipFile();
contentZip.AlternateEncoding = Encoding.GetEncoding("iso-8859-1");
contentZip.AlternateEncodingUsage = ZipOption.Always;
var bytecontent = File.ReadAllBytes(Environment.CurrentDirectory + "\\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 + "\\encrypt" + filename;
File.WriteAllBytes(encryptfilepath, Content);
byte[] decryptContent = Encryption.decryptBytes(File.ReadAllBytes(encryptfilepath), md5EncryptedKeyK2, initVectorBytes);
string decryptfilepath = Environment.CurrentDirectory + "\\decrypt" + filename;
File.WriteAllBytes(decryptfilepath, decryptContent);
contentZip.ExtractAll(Environment.CurrentDirectory + "\\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 + "\\decrypt1" + filename;
contentZip.ExtractAll(Environment.CurrentDirectory + "\\unzip\\decrypt1");
File.WriteAllBytes(decryptfilepath1, decryptContent1);
参考文章:NET对称加密体系
对称加密之AES、压缩解压以及压缩加密解密解压综合实战的更多相关文章
- tar命令加密压缩/解密解压
在tar解压文件时发生下面错误信息 gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not rec ...
- tar 加密压缩和解密解压
加密压缩 tar -czvf - file | openssl des3 -salt -k password -out /path/to/file.tar.gz 解密解压 openssl des3 - ...
- 对称加密之AES加密详解
最近有人问我AES对称加密是啥,我回答了个大概,发现自己不能清晰的讲出来,特此记录,以供学习 一.对称加密 对称加密是最快速.最简单的一种加密方式,加密(encryption)与解密(decrypti ...
- Asp.Net Core 2.0 项目实战(7)MD5加密、AES&DES对称加解密
本文目录 1. 摘要 2. MD5加密封装 3. AES的加密.解密 4. DES加密/解密 5. 总结 1. 摘要 C#中常用的一些加密和解密方案,如:md5加密.RSA加密与解密和DES加密等, ...
- Mac上zip,rar,tar文件命令解压和压缩
经常遇到在windowns上的压缩文件,在mac上解压出现问题,特意总结了下在Terminal里常用命令的方式解压和压缩文件 1.zip压缩文件 zip命令的参数很多,可以利用"zip -- ...
- 【VC++技术杂谈008】使用zlib解压zip压缩文件
最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...
- linux中tar之解压和压缩常用
我们知道在windows中解压和压缩有两个非常强大的工具winRar和国产的好压工具,在linux中也有一款强大的解压和压缩工具.那就是大名鼎鼎的tar.我们首先看看tar命令的使用格式 语法:tar ...
- tar 解压常用压缩文件格式命令大全
常用压缩文件格式就那么几种,解压命令总结在此: 1 2 3 4 5 6 7 8 tar xzf filename.tar.gz tar xjf filename.tar.bz2 tar xzf f ...
- Linux:文件解压与压缩
文件打包与压缩 常见压缩文件格式: |文件后缀名 |说明| |.zip |zip程序打包压缩的文件| |.rar |rar程序压缩的文件| |.7z |7zip程序压缩的文件| |.tar |tar程 ...
随机推荐
- linux sort,uniq,cut,wc,tr命令详解
sort是在Linux里非常常用的一个命令,对指定文件进行排序.去除重复的行 sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出.如果 File 参数指定多个文件,那么 sor ...
- sqlserver权限体系(下)
简介 在上一篇文章中,我对主体的概念做了全面的阐述.本篇文章接着讲述主体所作用的安全对象以及所对应的权限. 理解安全对象(Securable) 安全对象,是SQL Server 数据库引擎授权系统控制 ...
- 修改hosts
- 浅谈JavaScript中的Ajax
引言 作为一名WEB开发者,我想Ajax技术是一定需要掌握的.你也许平时没有使用JavaScript真正的写过Ajax.但是你一定使用过JQuery里面的相关函数来进行异步调用.今天我们就来介绍下原生 ...
- 用apache-cxf生成webservice客户端的时候报错Parameter: shead already exists for method
版本apache-cxf-3.1.0 命令如下:wsdl2java -p com.wz.interfaces -d ./src -client ./ws/xxx.wsdl 报错如下: WSDLToJa ...
- php:Header
转自鸟哥的博客: http://www.laruence.com/2007/12/16/308.html PHP header()the function declaration: void head ...
- What is the most common software of data mining? (整理中)
What is the most common software of data mining? 1 Orange? 2 Weka? 3 Apache mahout? 4 Rapidminer? 5 ...
- jQuery源码-dom操作之jQuery.fn.text
写在前面 jQuery.fn.text在jQuery是个使用频率比较高的接口,它的作用无非是设置/获取dom节点的内容文本,下文会通过几个简单的例子来说明.text()接口的使用,以及最后会对源码进行 ...
- 2015年12月10日 spring初级知识讲解(三)Spring消息之activeMQ消息队列
基础 JMS消息 一.下载ActiveMQ并安装 地址:http://activemq.apache.org/ 最新版本:5.13.0 下载完后解压缩到本地硬盘中,解压目录中activemq-core ...
- SQL--表分区
use Test --.创建数据库文件组>>alter database <数据库名> add filegroup <文件组名> ALTER DATABASE TE ...