C # folder Encryption

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.