File encryption write to document

Source: Internet
Author: User

    /// <summary>    ///file Encryption/// </summary>     Public classFileencryptdecrypt {/// <summary>        ///Encrypted characters/// </summary>         Private Static stringKey ="KANGJINW"; /// <summary>        ///encrypting a key to a file/// </summary>        /// <param name= "sInputFilename" >encryption Key</param>        /// <param name= "sOutputFileName" >Key file path</param>        /// <param name= "SKey" ></param>        Private  Static voidEncryptFile (stringsInputFilename,stringsOutputFileName,stringSKey) {FileStream fsencrypted=NewFileStream (Soutputfilename,filemode.create, FileAccess.Write); DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); Des. Key=ASCIIEncoding.ASCII.GetBytes (SKey); Des.iv=ASCIIEncoding.ASCII.GetBytes (SKey); ICryptoTransform desencrypt=DES.            CreateEncryptor (); CryptoStream CryptoStream=NewCryptoStream (fsencrypted, desencrypt, CryptoStreamMode.Write); byte[] Fsinput =System.Text.Encoding.Default.GetBytes (sInputFilename); CryptoStream. Write (Fsinput,0, fsinput.length); CryptoStream.            Close ();        Fsencrypted.close (); }        /// <summary>        ///Open key File/// </summary>        /// <param name= "sInputFilename" >Key Path</param>        /// <param name= "SKey" ></param>        Static stringDecryptFile (stringsInputFilename,stringSKey) {DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); Des. Key=ASCIIEncoding.ASCII.GetBytes (SKey); Des.iv=ASCIIEncoding.ASCII.GetBytes (SKey); FileStream Fsread=NewFileStream (sInputFilename, FileMode.Open, FileAccess.Read); ICryptoTransform Desdecrypt=DES.            CreateDecryptor (); CryptoStream CRYPTOSTREAMDECR=NewCryptoStream (fsread, Desdecrypt, CryptoStreamMode.Read); StreamReader Read=NewStreamReader (CRYPTOSTREAMDECR, System.Text.Encoding.Default); stringReft =read.            ReadToEnd (); Fsread.            Flush (); Fsread.            Close (); returnReft; }        /// <summary>        ///Encrypting Files/// </summary>        /// <param name= "filename" >File storage Path</param>        /// <param name= "Soce" >Encrypt content</param>         Public Static voidEncrypt (stringFileNamestringSoce) {            Try{encryptfile (soce, filename, key); }            Catch(Exception ex) {Throwex; }        }        /// <summary>        ///Decrypt Files/// </summary>        /// <param name= "filename" >Open File path</param>        /// <returns>returns the contents of an encrypted file</returns>         Public Static stringDecrypt (stringfilename) {            Try            {                returndecryptfile (filename, key); }            Catch(Exception ex) {Throwex; }        }    }

File encryption write to document

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.