using System;using System.Collections.Generic;using System.Text;using System.Security.Cryptography;using System.IO;namespace Net.Template.Common{/// <summary>/// The encryption and decryption of images/// </summary>public class DEncrypt4ImageHelper{public DEncrypt4ImageHelper() { }#region Encryption methods Image encryption/// <summary>/// Image encryption/// </summary>/// <param name="filePath"> The source file </param>/// <param name="savePath"> Save as the file name </param>/// <param name="keyStr"> The key </param>public static void EncryptFile(string filePath, string savePath, string keyStr){// through des encryptionDESCryptoServiceProvider des = new DESCryptoServiceProvider();// Open the file through the streamFileStream fs = File.OpenRead(filePath);// Access to the file 2 Hexadecimal charactersbyte[] inputByteArray = new byte[fs.Length];// Read the stream filefs.Read(inputByteArray, 0, (int)fs.Length);// Close the streamfs.Close();// Get the encrypted string 2 Hexadecimal charactersbyte[] keyByteArray = Encoding.Default.GetBytes(keyStr);// Computes the hash value of the specified region for the specified byte groupSHA1 ha = new SHA1Managed();byte[] hb = ha.ComputeHash(keyByteArray);// Encryption key arraybyte[] sKey = new byte[8];// Encryption variablesbyte[] sIV = new byte[8];for (int i = 0; i < 8; i++)sKey[i] = hb[i];for (int i = 8; i < 16; i++)sIV[i - 8] = hb[i];// Get the encryption keydes.Key = sKey;// Sets the encryption initialization vectordes.IV = sIV;MemoryStream ms = new MemoryStream();CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);cs.Write(inputByteArray, 0, inputByteArray.Length);cs.FlushFinalBlock();fs = File.OpenWrite(savePath);foreach (byte b in ms.ToArray()){fs.WriteByte(b);}fs.Close();cs.Close();ms.Close();}#endregion#region Decryption method Image decryption/// <summary>/// Image decryption/// </summary>/// <param name="filePath"> The source file </param>/// <param name="savePath"> Save the file </param>/// <param name="keyStr"> The key </param>public static void DecryptFile(string filePath, string savePath, string keyStr){// through des decryptionDESCryptoServiceProvider des = new DESCryptoServiceProvider();// The file is read through the streamFileStream fs = File.OpenRead(filePath);// Access to the file 2 Hexadecimal charactersbyte[] inputByteArray = new byte[fs.Length];// Read stream filefs.Read(inputByteArray, 0, (int)fs.Length);// Close the streamfs.Close();// Key arraybyte[] keyByteArray = Encoding.Default.GetBytes(keyStr);// Define hash variablesSHA1 ha = new SHA1Managed();// Computes the hash value of the specified region for the specified byte groupbyte[] hb = ha.ComputeHash(keyByteArray);// Encryption key arraybyte[] sKey = new byte[8];// Encryption variablesbyte[] sIV = new byte[8];for (int i = 0; i < 8; i++)sKey[i] = hb[i];for (int i = 8; i < 16; i++)sIV[i - 8] = hb[i];// Get the encryption keydes.Key = sKey;// Encryption variablesdes.IV = sIV;MemoryStream ms = new MemoryStream();CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);cs.Write(inputByteArray, 0, inputByteArray.Length);cs.FlushFinalBlock();fs = File.OpenWrite(savePath);foreach (byte b in ms.ToArray()){fs.WriteByte(b);}fs.Close();cs.Close();ms.Close();}#endregion}}