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

 
对称加密之AES及压缩加密解密解压综合实战
2016-09-15 13:29:07 /故事大全

对称加密:就是采用这种加密方法的双方使用方式用同样的密钥进行加密和解密。密钥是控制加密及解密过程的指令。算法是一组规则,规定如何进行加密和解密。

因此加密的安全性不仅取决于加密算法本身,密钥管理的安全性更是重要。因为加密和解密都使用同一个密钥,如何把密钥安全地传递到解密者手上就成了必须要解决的问题。

由此可见密钥传递也是比较重要的一环,一般都是通过对密钥二次加密的方式,进行密钥的传输

加密实现代码:

  1. publicstaticbyte[]encryptStringToBytes_AES(byte[]fileContentBytes,byte[]Key,byte[]IV)
  2. {
  3. //Checkarguments.
  4. if(fileContentBytes==null||fileContentBytes.Length<=0)
  5. thrownewArgumentNullException("plainText");
  6. if(Key==null||Key.Length<=0)
  7. thrownewArgumentNullException("Key");
  8. if(IV==null||IV.Length<=0)
  9. thrownewArgumentNullException("IV");
  10. MemoryStreammsEncrypt=null;
  11. AesCryptoServiceProvideraesAlg=null;
  12. try
  13. {
  14. aesAlg=newAesCryptoServiceProvider();
  15. aesAlg.Padding=PaddingMode.PKCS7;
  16. aesAlg.Key=Key;
  17. aesAlg.IV=IV;
  18. ICryptoTransformencryptor=aesAlg.CreateEncryptor(aesAlg.Key,aesAlg.IV);
  19. msEncrypt=newMemoryStream();
  20. using(CryptoStreamcsEncrypt=newCryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write))
  21. {
  22. csEncrypt.Write(fileContentBytes,0,fileContentBytes.Length);
  23. csEncrypt.FlushFinalBlock();
  24. }
  25. }
  26. catch(Exceptionex)
  27. {
  28. }
  29. finally
  30. {
  31. if(aesAlg!=null)
  32. aesAlg.Clear();
  33. }
  34. returnmsEncrypt.ToArray();
  35. }

解密代码实现:

  1. publicstaticbyte[]decryptBytes(byte[]cipherText,byte[]Key,byte[]IV)
  2. {
  3. if(cipherText==null||cipherText.Length<=0)
  4. thrownewArgumentNullException("cipherText");
  5. if(Key==null||Key.Length<=0)
  6. thrownewArgumentNullException("Key");
  7. if(IV==null||IV.Length<=0)
  8. thrownewArgumentNullException("IV");
  9. AesCryptoServiceProvideraesAlg=null;
  10. byte[]buffer=null;
  11. try
  12. {
  13. using(aesAlg=newAesCryptoServiceProvider())
  14. {
  15. aesAlg.Padding=PaddingMode.PKCS7;
  16. aesAlg.Key=Key;
  17. aesAlg.IV=IV;
  18. ICryptoTransformdecryptor=aesAlg.CreateDecryptor(aesAlg.Key,aesAlg.IV);
  19. using(MemoryStreammsDecrypt=newMemoryStream(cipherText))
  20. {
  21. CryptoStreamcsDecrypt=newCryptoStream(msDecrypt,decryptor,CryptoStreamMode.Read);
  22. byte[]tempbuffer=newbyte[cipherText.Length];
  23. inttotalBytesRead=csDecrypt.Read(tempbuffer,0,tempbuffer.Length);
  24. buffer=tempbuffer.Take(totalBytesRead).ToArray();
  25. }
  26. }
  27. }
  28. catch(Exceptionex)
  29. {
  30. }
  31. finally
  32. {
  33. if(aesAlg!=null)
  34. aesAlg.Clear();
  35. }
  36. returnbuffer;
  37. }

客户端加密解密文本文件实战:

  1. ///<summary>
  2. ///加密解密
  3. ///</summary>
  4. privatestaticvoid_EncryptAndDecrypt()
  5. {
  6. ASCIIEncodingasciiEnc=newASCIIEncoding();
  7. byte[]initVectorBytes=asciiEnc.GetBytes("@1B2c3D4e5F6g7H8");
  8. //RandomlygenerateorBookkey-keyK2-Keytoencryptxmlcontent
  9. stringkeyK2=Generator.RandomString(10);
  10. //Generatethe128bitstringusingMD5forkeyK2
  11. MD5hashProvider=MD5.Create();
  12. byte[]md5EncryptedKeyK2=hashProvider.ComputeHash(asciiEnc.GetBytes(keyK2));
  13. stringfilename="NewTextDocument.txt";
  14. stringfilepath=Environment.CurrentDirectory+""+filename;
  15. byte[]Content=Encryption.encryptStringToBytes_AES(File.ReadAllBytes(filepath),md5EncryptedKeyK2,initVectorBytes);
  16. stringencryptfilepath=Environment.CurrentDirectory+"encrypt"+filename;
  17. File.WriteAllBytes(encryptfilepath,Content);
  18. byte[]decryptContent=Encryption.decryptBytes(File.ReadAllBytes(encryptfilepath),md5EncryptedKeyK2,initVectorBytes);
  19. stringdecryptfilepath=Environment.CurrentDirectory+"decrypt"+filename;
  20. File.WriteAllBytes(decryptfilepath,decryptContent);
  21. }

压缩解压:

  1. stringfilename="NewTextDocument.txt";
  2. stringfilepath=Environment.CurrentDirectory+""+filename;
  3. stringzipfilepath=Environment.CurrentDirectory+"NewTextDocument.zip";
  4. using(ZipFilecontentZip=newZipFile())
  5. {
  6. //压缩
  7. contentZip.AlternateEncoding=Encoding.GetEncoding("iso-8859-1");
  8. contentZip.AlternateEncodingUsage=ZipOption.Always;
  9. ZipEntrycontentFile=contentZip.AddEntry(filename,File.ReadAllBytes(filepath));
  10. contentZip.Save(zipfilepath);
  11. //解压
  12. contentZip.ExtractAll(Environment.CurrentDirectory);
  13. }

压缩加密解密解压:

  1. stringfilename="NewTextDocument.zip";
  2. stringfilepath=Environment.CurrentDirectory+""+filename;
  3. stringzipfilepath=Environment.CurrentDirectory+""+filename;
  4. ZipFilecontentZip=newZipFile();
  5. contentZip.AlternateEncoding=Encoding.GetEncoding("iso-8859-1");
  6. contentZip.AlternateEncodingUsage=ZipOption.Always;
  7. varbytecontent=File.ReadAllBytes(Environment.CurrentDirectory+"NewTextDocument.txt");
  8. ZipEntrycontentFile=contentZip.AddEntry("NewTextDocument.txt",bytecontent);
  9. contentZip.Save(zipfilepath);
  10. ASCIIEncodingasciiEnc=newASCIIEncoding();
  11. byte[]initVectorBytes=asciiEnc.GetBytes("@1B2c3D4e5F6g7H8");
  12. //RandomlygenerateorBookkey-keyK2-Keytoencryptxmlcontent
  13. stringkeyK2=Generator.RandomString(10);
  14. //Generatethe128bitstringusingMD5forkeyK2
  15. MD5hashProvider=MD5.Create();
  16. byte[]md5EncryptedKeyK2=hashProvider.ComputeHash(asciiEnc.GetBytes(keyK2));
  17. byte[]Content=Encryption.encryptStringToBytes_AES(File.ReadAllBytes(filepath),md5EncryptedKeyK2,initVectorBytes);
  18. stringencryptfilepath=Environment.CurrentDirectory+"encrypt"+filename;
  19. File.WriteAllBytes(encryptfilepath,Content);
  20. byte[]decryptContent=Encryption.decryptBytes(File.ReadAllBytes(encryptfilepath),md5EncryptedKeyK2,initVectorBytes);
  21. stringdecryptfilepath=Environment.CurrentDirectory+"decrypt"+filename;
  22. File.WriteAllBytes(decryptfilepath,decryptContent);
  23. contentZip.ExtractAll(Environment.CurrentDirectory+"unzipdecrypt");
  24. stringkey=Convert.ToBase64String(md5EncryptedKeyK2);
  25. stringiv=Convert.ToBase64String(initVectorBytes);
  26. Console.WriteLine(key);
  27. Console.WriteLine(iv);
  28. byte[]decryptContent1=Encryption.decryptBytes(File.ReadAllBytes(encryptfilepath),Convert.FromBase64String(key),Convert.FromBase64String(iv));
  29. stringdecryptfilepath1=Environment.CurrentDirectory+"decrypt1"+filename;
  30. contentZip.ExtractAll(Environment.CurrentDirectory+"unzipdecrypt1");
  31. File.WriteAllBytes(decryptfilepath1,decryptContent1);

所属专题:
如果您觉得本文或图片不错,请把它分享给您的朋友吧!

 
搜索
 
 
广告
 
 
广告
 
故事大全
 
版权所有- © 2012-2015 · 故事大全 SITEMAP站点地图 Power by flip clock online手机看故事 站点地图 Power by flip clock online