对称加密之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程 ...
随机推荐
- array_flip() array_merge() array+array的使用总结
array_flip(array); //传递一个数组参数,对该数组的键.值进行翻转 例如: $a = array( 'a', 'b', 'c' ); print_r(array_flip($a)); ...
- PHP数组处理函数的使用array_reduce(二)
关于PHP数组操作函数更为细致的用法大家还可以参考PHP在线参考手册:http://php.net/manual/zh/index.php array_reduce — 用回调函数迭代地将数组简化为单 ...
- Java面试笔记
1.&和&& if(str != null& !str.equals("")){ System.out.println("ok" ...
- java获取指定路径下的指定文件/java.io.File.listFiles(FilenameFilter filter)
java.io.File.listFiles(FilenameFilter filter) 返回抽象路径名数组,表示在目录中此抽象路径名表示,满足指定过滤器的文件和目录. 声明 以下是java.io. ...
- eclipse的html代码辅助失效解决办法
Eclipse IDE : .xhtml code assist is not working for JSF tag By mkyong | September 6, 2010 | Viewed : ...
- R语言画图实例-参考R语言实战
dose <- c(, , , ,) drugA <- c(, , , , ) drugB <- c(, , , , ) # 数据准备 opar <- par(no.reado ...
- oracle中时间运算
Oracle两个函数相减,默认得到的是天数,按日期格式,精准到响应的精度,如用sysdate(2015/12/7 10:17:52),时间差精确到秒. 在此基础上,oracle两个时间相减默认天数*2 ...
- MVC中你必须知道的13个扩展点
MVC中你必须知道的13个扩展点 pasting 转:http://www.cnblogs.com/kirinboy/archive/2009/06/01/13-asp-net-mvc-extensi ...
- Jquery控制滚动显示欢迎字幕v2
Jquery控制滚动显示欢迎字幕v2: 之前做的那个比较适合测试环境,但要套入到网站中,有两个按钮在那摆着,还是不太好看.后面对代码进行了修改,如下: 参考代码: <html> <h ...
- firefox浏览器不能使用window.close的解决方案
javascript中window.close()函数用来关闭窗体,而且IE.google.firefox浏览均支持,但由于firefox浏览器dom.allow_scripts_to_close_w ...