对称加密之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程 ...
随机推荐
- CKEditor的使用方法
CKEditor的使用方法 2014-03-31 09:44 8649人阅读 评论(1) 收藏 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. ckeditor 的官方网站是 http:/ ...
- Effective Objective-C 2.0 — 第9条:以“类族模式”隐藏实现细节
第9条:以“类族模式”隐藏实现细节 类族模式可以把实现细节隐藏在一套简单的公共接口后面 系统框架中经常使用类族 从类族的公共抽象基类中继承子类时要当心,若有开发文档,则应首先阅读 “类族”(class ...
- JaxWsProxyFactoryBean 与 JaxWsDynamicClientFactory
1. JaxWsProxyFactoryBean 简介:调用方式采用了和RMI类似的机制,即客户端直接调用服务器端提供的服务接口(interface),CXF通过运行时代理生成远程服务的代理对象 ...
- Xcode常用技巧(2)-使Xcode在创建类时自动添加前缀
在Xcode5之前的版本中,Xcode在新建项目时,会要求为一个类指定一个前缀,这样方便我们区分相同名字的类.而从Xcode6开始,由于Swift增加了命名空间的关系,Xcode在新建项目时,不会再要 ...
- Linux服务器管理: 系统的进程管理pstree命令
pstree命令是查看进程树或者结构的命令 [root@localhost~]#pstree [选项] 需要注意的是不能将 -p和-u同时使用 如果同时使用前者生效后者无效但并不报错 选项: -p: ...
- HDU4930 Fighting the Landlords 模拟
Fighting the Landlords Fighting the Landlords Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- Unity API
关于 int Mathf.PingPong(t, length); 原理,相当于 #include <iostream> #include <vector> int test( ...
- javascript简单的认识下return语句+2015的总结+2016的展望
好久没更新博客了...自从有了mac之后世界变得简单了...日常么,除了研究代码,看别人的代码,写自己的代码.就那样.... 吐槽点:window配个nodejs的环境花了九头牛两只老虎的力气,mac ...
- HttpClient连接池的连接保持、超时和失效机制
HTTP是一种无连接的事务协议,底层使用的还是TCP,连接池复用的就是TCP连接,目的就是在一个TCP连接上进行多次的HTTP请求从而提高性能.每次HTTP请求结束的时候,HttpClient会判断连 ...
- css 设计总结
一.背景图片的拉伸: backgroud-size 说明: http://www.w3school.com.cn/cssref/pr_background-size.asp 效果: http:// ...