You can encrypt the contents of the file or encrypt the folder itself, and this article encrypts the folder.
One, specify or generate a key
1) The specified key
/// <summary> /// //</summary>publicstaticstring"? \a?? (? ";
2) You can also generate a key
/// <summary>///generate a 64-bit key/// </summary>/// <returns>string</returns> Public Static stringGenerateKey () {//creates an instance of a symmetric algorithm. Auto-generated keys and IV. DESCryptoServiceProvider Descrypto =(DESCryptoServiceProvider) descryptoserviceprovider.create (); //encrypted using an auto-generated key. returnASCIIEncoding.ASCII.GetString (Descrypto.key);}
Second, call the ZeroMemory function to remove the key from memory
<summary> //// Call this function to remove the key from memory using //</summary>[DllImport (" KERNEL32. DLL""rtlzeromemory")]publicstatic extern BOOL int Length);
Third, encrypt files
/// <summary> ///Encrypting Files/// </summary> /// <param name= "sInputFilename" >the full path of the file to be encrypted</param> /// <param name= "sOutputFileName" >The full path of the encrypted file</param> Public Static voidEncryptFile (stringsInputFilename,stringsOutputFileName) {FileStream fsinput=NewFileStream (sInputFilename, FileMode.Open, FileAccess.Read); FileStream fsencrypted=NewFileStream (sOutputFileName, FileMode.Create, FileAccess.Write); DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); Des. Key=ASCIIEncoding.ASCII.GetBytes (sSecretKey); Des.iv=ASCIIEncoding.ASCII.GetBytes (sSecretKey); ICryptoTransform desencrypt=DES. CreateEncryptor (); CryptoStream CryptoStream=NewCryptoStream (fsencrypted, desencrypt, CryptoStreamMode.Write); byte[] bytearrayinput =New byte[fsInput.Length]; fsInput.Read (Bytearrayinput,0, bytearrayinput. Length); CryptoStream. Write (Bytearrayinput,0, bytearrayinput. Length); CryptoStream. Flush (); Fsinput.flush (); Fsencrypted.flush (); CryptoStream. Close (); Fsinput.close (); Fsencrypted.close (); }
Iv. Decryption of files
/// <summary> ///Decrypt Files/// </summary> /// <param name= "sInputFilename" >The full path of the file to be decrypted</param> /// <param name= "sOutputFileName" >The full path of the decrypted file</param> Public Static voidDecryptFile (stringsInputFilename,stringsOutputFileName) {DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); Des. Key=ASCIIEncoding.ASCII.GetBytes (sSecretKey); Des.iv=ASCIIEncoding.ASCII.GetBytes (sSecretKey); FileStream Fsread=NewFileStream (sInputFilename, FileMode.Open, FileAccess.Read); ICryptoTransform Desdecrypt=DES. CreateDecryptor (); CryptoStream CRYPTOSTREAMDECR=NewCryptoStream (fsread, Desdecrypt, CryptoStreamMode.Read); StreamWriter fsdecrypted=NewStreamWriter (sOutputFileName); Fsdecrypted.write (NewStreamReader (CRYPTOSTREAMDECR). ReadToEnd ()); Fsdecrypted.flush (); Fsdecrypted.close (); }
Five, complete code
/// <summary> ///file Encryption/// </summary> Public classFilesecrethelper {/// <summary> ///Key , this password can be arbitrarily specified/// </summary> Public Static stringsSecretKey ="? \a?? (?"; /// <summary> ///Call this function to remove the key from memory using/// </summary>[DllImport ("KERNEL32. DLL", EntryPoint ="RtlZeroMemory")] Public Static extern BOOLZeroMemory (IntPtr Destination,intLength); /// <summary> ///generate a 64-bit key/// </summary> /// <returns>string</returns> Public Static stringGenerateKey () {//creates an instance of a symmetric algorithm. Auto-generated keys and IV. DESCryptoServiceProvider Descrypto =(DESCryptoServiceProvider) descryptoserviceprovider.create (); //encrypted using an auto-generated key. returnASCIIEncoding.ASCII.GetString (Descrypto.key); } /// <summary> ///Encrypting Files/// </summary> /// <param name= "sInputFilename" >the full path of the file to be encrypted</param> /// <param name= "sOutputFileName" >The full path of the encrypted file</param> Public Static voidEncryptFile (stringsInputFilename,stringsOutputFileName) {FileStream fsinput=NewFileStream (sInputFilename, FileMode.Open, FileAccess.Read); FileStream fsencrypted=NewFileStream (sOutputFileName, FileMode.Create, FileAccess.Write); DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); Des. Key=ASCIIEncoding.ASCII.GetBytes (sSecretKey); Des.iv=ASCIIEncoding.ASCII.GetBytes (sSecretKey); ICryptoTransform desencrypt=DES. CreateEncryptor (); CryptoStream CryptoStream=NewCryptoStream (fsencrypted, desencrypt, CryptoStreamMode.Write); byte[] bytearrayinput =New byte[fsInput.Length]; fsInput.Read (Bytearrayinput,0, bytearrayinput. Length); CryptoStream. Write (Bytearrayinput,0, bytearrayinput. Length); CryptoStream. Flush (); Fsinput.flush (); Fsencrypted.flush (); CryptoStream. Close (); Fsinput.close (); Fsencrypted.close (); } /// <summary> ///Decrypt Files/// </summary> /// <param name= "sInputFilename" >The full path of the file to be decrypted</param> /// <param name= "sOutputFileName" >The full path of the decrypted file</param> Public Static voidDecryptFile (stringsInputFilename,stringsOutputFileName) {DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); Des. Key=ASCIIEncoding.ASCII.GetBytes (sSecretKey); Des.iv=ASCIIEncoding.ASCII.GetBytes (sSecretKey); FileStream Fsread=NewFileStream (sInputFilename, FileMode.Open, FileAccess.Read); ICryptoTransform Desdecrypt=DES. CreateDecryptor (); CryptoStream CRYPTOSTREAMDECR=NewCryptoStream (fsread, Desdecrypt, CryptoStreamMode.Read); StreamWriter fsdecrypted=NewStreamWriter (sOutputFileName); Fsdecrypted.write (NewStreamReader (CRYPTOSTREAMDECR). ReadToEnd ()); Fsdecrypted.flush (); Fsdecrypted.close (); } }
C # folder Encryption