黑客24小时在线接单网站

电脑高手24在线咨询,黑客24小时在线接单网站,黑客接单平台,黑客网站找人,黑客在线qq接单

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

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

因此,加密的安全性不仅取决于加密算法本身,还取决于密钥管理的安全性。由于加密和解密使用相同的密钥,因此如何安全地将密钥传输给解密者已成为一个必须解决的问题。

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

由此可见,密钥传输也是一个更重要的环节,通常通过二次加密密钥传输密钥

加密实现代码:

  • publicstaticbyte[]encryptStringToBytes_AES(byte[]fileContentBytes,byte[]Key,byte[]IV)
  • {
  • //Checkarguments.
  • if(fileContentBytes==null||fileContentBytes.Length<=0)
  • thrownewArgumentNullException("plainText");
  • if(Key==null||Key.Length<=0)
  • thrownewArgumentNullException("Key");
  • if(IV==null||IV.Length<=0)
  • thrownewArgumentNullException("IV");
  • MemoryStreammsEncrypt=null;
  • AesCryptoServiceProvideraesAlg=null;
  • try
  • {
  • aesAlg=newAesCryptoServiceProvider();
  •    
  • aesAlg.Padding=PaddingMode.PKCS7;
  • aesAlg.Key=Key;
  • aesAlg.IV=IV;
  •    
  • ICryptoTransformencryptor=aesAlg.CreateEncryptor(aesAlg.Key,aesAlg.IV);
  •    
  • msEncrypt=newMemoryStream();
  • using(CryptoStreamcsEncrypt=newCryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write))
  • {
  • csEncrypt.Write(fileContentBytes,0,fileContentBytes.Length);
  • csEncrypt.FlushFinalBlock();
  • }
  • }
  • catch(Exceptionex)
  • {
  •    
  • }
  • finally
  • {
  • if(aesAlg!=null)
  • aesAlg.Clear();
  • }
  • returnmsEncrypt.ToArray();
  • }
  • 实现解密代码:

  • publicstaticbyte[]decryptBytes(byte[]cipherText,byte[]Key,byte[]IV)
  • {
  • if(cipherText==null||cipherText.Length<=0)
  • thrownewArgumentNullException("cipherText");
  • if(Key==null||Key.Length<=0)
  • thrownewArgumentNullException("Key");
  • if(IV==null||IV.Length<=0)
  • thrownewArgumentNullException("IV");
  • AesCryptoServiceProvideraesAlg=null;
  • byte[]buffer=null;
  • try
  • {
  • using(aesAlg=newAesCryptoServiceProvider())
  • {
  • aesAlg.Padding=PaddingMode.PKCS7;
  • aesAlg.Key=Key;
  • aesAlg.IV=IV;
  • ICryptoTransformdecryptor=aesAlg.CreateDecryptor(aesAlg.Key,aesAlg.IV);
  •    
  • using(MemoryStreammsDecrypt=newMemoryStream(cipherText))
  • {
  • CryptoStreamcsDecrypt=newCryptoStream(msDecrypt,decryptor,CryptoStreamMode.Read);
  • byte[]tempbuffer=newbyte[cipherText.Length];
  • inttotalBytesRead=csDecrypt.Read(tempbuffer,0,tempbuffer.Length);
  • buffer=tempbuffer.Take(totalBytesRead).ToArray();
  • }
  • }
  • }
  • catch(Exceptionex)
  • {
  •    
  • }
  • finally
  • {
  • if(aesAlg!=null)
  • aesAlg.Clear();
  • }
  • returnbuffer;
  • }
  • 客户加密解密文本文件实战:

  • ///<summary>
  • ////加密解密
  • ///</summary>
  • privatestaticvoid_EncryptAndDecrypt()
  • {
  • ASCIIEncodingasciiEnc=newASCIIEncoding();
  • byte[]initVectorBytes=asciiEnc.GetBytes("@1B2c3D4e5F6g7H8");
  •    
  • //RandomlygenerateorBookkey-keyK2-Keytoencryptxmlcontent
  • stringkeyK2=Generator.RandomString(10);
  • //Generatethe128bitstringusingMD5forkeyK2
  • MD5hashProvider=MD5.Create();
  • byte[]md5EncryptedKeyK2=hashProvider.ComputeHash(asciiEnc.GetBytes(keyK2));
  •    
  • stringfilename="NewTextDocument.txt";
  • stringfilepath=Environment.CurrentDirectory "\\" filename;
  •    
  • byte[]Content=Encryption.encryptStringToBytes_AES(File.ReadAllBytes(filepath),md5EncryptedKeyK2,initVectorBytes);
  • stringencryptfilepath=Environment.CurrentDirectory "\\encrypt" filename;
  • File.WriteAllBytes(encryptfilepath,Content);
  •    
  • byte[]decryptContent=Encryption.decryptBytes(File.ReadAllBytes(encryptfilepath),md5EncryptedKeyK2,initVectorBytes);
  • stringdecryptfilepath=Environment.CurrentDirectory "\\decrypt" filename;
  • File.WriteAllBytes(decryptfilepath,decryptContent);
  •    
  • }
  • 压缩解压:

  • stringfilename="NewTextDocument.txt";
  • stringfilepath=Environment.CurrentDirectory "\\"    filename;
  • stringzipfilepath=Environment.CurrentDirectory "\\NewTextDocument.zip";
  • using(ZipFilecontentZip=newZipFile())
  • {
  • //压缩
  • contentZip.AlternateEncoding=Encoding.GetEncoding("iso-8859-1");
  • contentZip.AlternateEncodingUsage=ZipOption.Always;
  • ZipEntrycontentFile=contentZip.AddEntry(filename,File.ReadAllBytes(filepath));
  • contentZip.Save(zipfilepath);
  •    
  •    
  • //解压
  • contentZip.ExtractAll(Environment.CurrentDirectory);
  • }
  • 压缩加密解密解压:

  • stringfilename="NewTextDocument.zip";
  •    
  • stringfilepath=Environment.CurrentDirectory "\\"    filename;
  • stringzipfilepath=Environment.CurrentDirectory "\\"    filename;
  •    
  • ZipFilecontentZip=newZipFile();
  •    
  • contentZip.AlternateEncoding=Encoding.GetEncoding("iso-8859-1");
  • contentZip.AlternateEncodingUsage=ZipOption.Always;
  • varbytecontent=File.ReadAllBytes(Environment.CurrentDirectory "\\NewTextDocument.txt");
  • ZipEntrycontentFile=contentZip.AddEntry("NewTextDocument.txt",bytecontent);
  • contentZip.Save(zipfilepath);
  •    
  • ASCIIEncodingasciiEnc=newASCIIEncoding();
  • byte[]initVectorBytes=asciiEnc.GetBytes("@1B2c3D4e5F6g7H8");
  •    
  • //RandomlygenerateorBookkey-keyK2-Keytoencryptxmlcontent
  • stringkeyK2=Generator.RandomString(10);
  • //Generatethe128bitstringusingMD5forkeyK2
  • MD5hashProvider=MD5.Create();
  • byte[]md5EncryptedKeyK2=hashProvider.ComputeHash(asciiEnc.GetBytes(keyK2));
  •    
  • byte[]Content=Encryption.encryptStringToBytes_AES(File.ReadAllBytes(filepath),md5EncryptedKeyK2,initVectorBytes);
  • stringencryptfilepath=Environment.CurrentDirectory "\\encrypt"    filename;
  • File.WriteAllBytes(encryptfilepath,Content);
  •    
  • byte[]decryptContent=Encryption.decryptBytes(File.ReadAllBytes(encryptfilepath),md5EncryptedKeyK2,initVectorBytes);
  • stringdecryptfilepath=Environment.CurrentDirectory "\\decrypt"    filename;
  • File.WriteAllBytes(decryptfilepath,decryptContent);
  •    
  • contentZip.ExtractAll(Environment.CurrentDirectory "\\unzip\\decrypt");
  • stringkey=Convert.ToBase64String(md5EncryptedKeyK2);
  • stringiv=Convert.ToBase64String(initVectorBytes);
  • Console.WriteLine(key);
  • Console.WriteLine(iv);
  •    
  • byte[]decryptContent1=Encryption.decryptBytes(File.ReadAllBytes(encryptfilepath),Convert.FromBase64String(key),Convert.FromBase64String(iv));
  • stringdecryptfilepath1=Environment.CurrentDirectory "\\decrypt1"    filename;
  •    
  • contentZip.ExtractAll(Environment.CurrentDirectory "\\unzip\\decrypt1");
  •    
  • File.WriteAllBytes(decryptfilepath1,decryptContent1);
  •    
    • 评论列表:
    •  惑心近箐
       发布于 2022-05-29 18:06:17  回复该评论
    • d5EncryptedKeyK2,initVectorBytes); stringencryptfilepath=Environment.CurrentDirectory "\\encryp

    发表评论:

    Powered By

    Copyright Your WebSite.Some Rights Reserved.