1. AES
  2. 压缩解压
  3. 压缩加密解密解压

对称加密:

就是采用这种加密方法的双方使用方式用同样的密钥进行加密和解密。密钥是控制加密及解密过程的指令。算法是一组规则,规定如何进行加密和解密。
 
因此加密的安全性不仅取决于加密算法本身,密钥管理的安全性更是重要。因为加密和解密都使用同一个密钥,如何把密钥安全地传递到解密者手上就成了必须要解决的问题。
 
 
由此可见密钥传递也是比较重要的一环,一般都是通过对密钥二次加密的方式,进行密钥的传输
 
加密实现代码:

 
        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、压缩解压以及压缩加密解密解压综合实战的更多相关文章

  1. tar命令加密压缩/解密解压

    在tar解压文件时发生下面错误信息 gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not rec ...

  2. tar 加密压缩和解密解压

    加密压缩 tar -czvf - file | openssl des3 -salt -k password -out /path/to/file.tar.gz 解密解压 openssl des3 - ...

  3. 对称加密之AES加密详解

    最近有人问我AES对称加密是啥,我回答了个大概,发现自己不能清晰的讲出来,特此记录,以供学习 一.对称加密 对称加密是最快速.最简单的一种加密方式,加密(encryption)与解密(decrypti ...

  4. Asp.Net Core 2.0 项目实战(7)MD5加密、AES&DES对称加解密

    本文目录 1. 摘要 2. MD5加密封装 3. AES的加密.解密 4. DES加密/解密 5. 总结 1.  摘要 C#中常用的一些加密和解密方案,如:md5加密.RSA加密与解密和DES加密等, ...

  5. Mac上zip,rar,tar文件命令解压和压缩

    经常遇到在windowns上的压缩文件,在mac上解压出现问题,特意总结了下在Terminal里常用命令的方式解压和压缩文件 1.zip压缩文件 zip命令的参数很多,可以利用"zip -- ...

  6. 【VC++技术杂谈008】使用zlib解压zip压缩文件

    最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...

  7. linux中tar之解压和压缩常用

    我们知道在windows中解压和压缩有两个非常强大的工具winRar和国产的好压工具,在linux中也有一款强大的解压和压缩工具.那就是大名鼎鼎的tar.我们首先看看tar命令的使用格式 语法:tar ...

  8. tar 解压常用压缩文件格式命令大全

    常用压缩文件格式就那么几种,解压命令总结在此:   1 2 3 4 5 6 7 8 tar xzf filename.tar.gz tar xjf filename.tar.bz2 tar xzf f ...

  9. Linux:文件解压与压缩

    文件打包与压缩 常见压缩文件格式: |文件后缀名 |说明| |.zip |zip程序打包压缩的文件| |.rar |rar程序压缩的文件| |.7z |7zip程序压缩的文件| |.tar |tar程 ...

随机推荐

  1. Django笔记-helloworld

    网上的Django资料太乱了,我想写一下自己的学习过程(只记大体过程,有时间就完善).(用eclipse+PyDev工具开发的) 1.项目结构 2.关键代码:(注意缩进,可能贴上来缩进格式等有变化,我 ...

  2. vim:用vim修改文件编码为utf-8

    命令是 :set fileencoding=utf-8如果用vim打开文件时里面有乱码,可能用上面的命令修改文件后无法保存.可以用其他软件打开文件,然后把内容拷贝到vim里再保存就行了.

  3. CSV文件的读取 导出

    CSV文件是 逗号分隔值文件 内容基本是这么存的... 姓名,年龄,性别/r/n王有才,,男/r/n王二妞,,女 一行值内用逗号 , 分开  行与行之间用  /r/n  分隔 说白了,他其实是一个字符 ...

  4. Java并发编程核心方法与框架-Executors的使用

    合理利用线程池能够带来三个好处 降低资源消耗.通过重复利用已创建的线程降低线程创建和销毁造成的消耗. 提高响应速度.当任务到达时,任务可以不需要等到线程创建就能立即执行. 提高线程的可管理性.线程是稀 ...

  5. 用Redis实现分布式锁 与 实现任务队列(转)

    这一次总结和分享用Redis实现分布式锁 与 实现任务队列 这两大强大的功能.先扯点个人观点,之前我看了一篇博文说博客园的文章大部分都是分享代码,博文里强调说分享思路比分享代码更重要(貌似大概是这个意 ...

  6. Yii2 数据操作Query Builder(转)

    Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...

  7. MySQL主从数据库同步延迟问题解决(转)

    最近在做MySQL主从数据库同步测试,发现了一些问题,其中主从同步延迟问题是其中之一,下面内容是从网上找到的一些讲解,记录下来以便自己学习: MySQL的主从同步是一个很成熟的架构,优点为:①在从服务 ...

  8. 使用cachemanager做缓存(Session的缓存)

    1.我在这里直接用 cachemanager.redis 往redis里面存储缓存数据2.步骤 1)下载CacheManager.Redis(包含了CacheManager.Core) 下载Stack ...

  9. Oracle卸载

    用Oracle自带的卸载程序不能从根本上卸载Oracle,从而为下次的安装留下隐患,所以要想完全卸载Oracle就必须要直接将注册表清除. 步骤如下: 1. 开始->设置->控制面板-&g ...

  10. jquery遍历

    http://www.cnblogs.com/tylerdonet/archive/2013/04/05/3000618.html jQuery 遍历函数 jQuery 遍历函数包括了用于筛选.查找和 ...