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

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

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

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

对称加密之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();  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

解密代码实现:

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;  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

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

/// <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);  
   

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

压缩解压:

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);  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

压缩加密解密解压:

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); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

 

责任编辑:蓝雨泪 来源: 博客园
相关推荐

2022-10-21 07:33:12

2020-05-27 10:10:56

对称加密Hash算法数字签名

2023-09-04 14:00:28

加密密钥私钥

2019-09-11 08:37:16

2019-08-02 10:43:57

2024-12-31 08:00:00

SpringBoot开发加密

2023-11-22 16:08:48

2023-08-01 07:24:05

2024-08-26 08:34:47

AES加密算法

2014-07-07 10:04:32

2019-09-23 12:16:02

通信安全加密哈希

2024-04-29 07:50:52

C#AES加密

2023-03-06 08:49:02

加密和解密SpringBoot

2019-12-11 16:56:37

HTTPS对称加密Java

2009-08-13 18:12:11

C#数据加密

2009-08-04 11:08:33

ASP.NET数据加密

2024-07-09 10:13:15

2022-06-05 23:30:25

AES加密算法

2010-07-22 16:33:08

2011-08-01 14:14:36

加密技术
点赞
收藏

51CTO技术栈公众号