对称加密之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程 ...
随机推荐
- C-函数与内存剖析
功能,封装,调用 形参:定义函数时函数名后面中的参数,形式参数 实参:调用函数时传入的具体数据 return作用:1退出函数 2返回一个具体数值给调用者 返回值: 如果不明确声明返回值类型,默认就是i ...
- redis缓存分页数据ID
1.用户通过分类.属性进来分页时 如果没有缓存,就读数据库前10页的数据Id,转为json,根据cate_分类1+cate_分类2+cate_分类3+arr_属性1+arr_属性2+arr_属性3作为 ...
- Jetty多Connector
有时候想要启动两个端口,或者通过一个Jetty server提供多个不同服务,比如说使用8080来指定默认访问端口,使用8433指定https访问端口等等,此时就可以通过创建多个Connector来解 ...
- Javascript 中我很想说说的 this
this是每一个想要深入学习Javascript的人必过的一关,我为this看过很多书查过很多资料,虽然对this有了一定的了解并且也经常使用this,但是如果有人问我 this是什么呀? 我依旧不 ...
- HTML5游戏设计与开发 小白7-9月的动态
好久没有更新博客了,最近在努力修炼提升逼格,当然了还有个恶心的毕业论文... 当然啦...在写这个论文的时候也就是为了提升下自身的技术,毕竟我的公司也不是游戏公司,SO 我决定开发个手机游戏.然后考虑 ...
- 关于IE7 默认有边框的解决方案
这个问题出现在IE7中,因为body有默认的border.这个原因是由于声明引起的. 加了这个头就可以了 * {border:0;} 以上的 CSS 在 XHTML 下是无效果的,将 DOCTYPE ...
- Todd's Matlab讲义第5讲:二分法和找根
二分法和if ... else ... end 语句 先回顾一下二分法.要求方程\(f(x)=0\)的根.假设\(c = f(a) < 0\)和\(d = f(b) > 0\),如果\(f ...
- padding/margin/border 理解
- OS X Framework Library not loaded: 'Image not found'的解决办法
参考:OS X Framework Library not loaded: 'Image not found' 1.首先将相应的framework手动复制到/System/Library/Framew ...
- Swift2.1 语法指南——错误处理
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...