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

由此可见密钥传递也是比较重要的一环,一般都是通过对密钥二次加密的方式,进行密钥的传输
加密实现代码:
- 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+"unzipdecrypt");
- 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+"unzipdecrypt1");
- File.WriteAllBytes(decryptfilepath1,decryptContent1);
所属专题: