How to encrypt and decrypt files

Source: Internet
Author: User
Using system; using system. io; using system. security; using system. security. cryptography; using system. runtime. interopservices; using system. text; namespace csencryptdecrypt {class class1 {// call this function to remove the key from memory after use for security [system. runtime. interopservices. dllimport ("kernel32.dll", entrypoint = "rtlzeromemory")] public static extern bool zeromemory (intptr destination, int length); // function to generate a 64 bits key. static string generatekey () {// create an instance of symetric algorithm. key and IV is generated automatically. descryptoserviceprovider descrypto = (descryptoserviceprovider) descryptoserviceprovider. create (); // use the automatically generated key for encryption. return asciiencoding. ASCII. getstring (descrypto. key);} static void encryptfile (string sinputfilename, string soutputfilename, string skey) {filestream fsinput = new filestream (sinputfilename, filemode. open, fileaccess. read); filestream fsencrypted = new filestream (soutputfilename, filemode. create, fileaccess. write); descryptoserviceprovider des = new descryptoserviceprovider (); des. key = asciiencoding. ASCII. getbytes (skey); des. IV = asciiencoding. ASCII. getbytes (skey); icryptotransform desencrypt = des. createencryptor (); cryptostream = new cryptostream (fsencrypted, desencrypt, cryptostreammode. write); byte [] bytearrayinput = new byte [fsinput. length]; fsinput. read (bytearrayinput, 0, bytearrayinput. length); cryptostream. write (bytearrayinput, 0, bytearrayinput. length); cryptostream. close (); fsinput. close (); fsencrypted. close ();} static void decryptfile (string sinputfilename, string soutputfilename, string skey) {descryptoserviceprovider des = new descryptoserviceprovider (); // a 64-bit key and IV is required for this provider. // set secret key for DES algorithm. des. key = asciiencoding. ASCII. getbytes (skey); // set initialization vector. des. IV = asciiencoding. ASCII. getbytes (skey); // create a file stream to read the encrypted file back. filestream fsread = new filestream (sinputfilename, filemode. open, fileaccess. read); // create a des decryptor from the des instance. icryptotransform desdecrypt = des. createdecryptor (); // create crypto stream set to read and do a // des Decryption Transform on incoming bytes. cryptostream cryptostreamdecr = new cryptostream (fsread, desdecrypt, cryptostreammode. read); // print the contents of the decrypted file. streamwriter fsdecrypted = new streamwriter (soutputfilename); fsdecrypted. write (New streamreader (cryptostreamdecr ). readtoend (); fsdecrypted. flush (); fsdecrypted. close ();} static void main () {// must be 64 bits, 8 bytes. // distribute this key to the user who will decrypt this file. string ssecretkey; // get the key for the file to encrypt. ssecretkey = generatekey (); // for additional security pin the key. gchandle gch = gchandle. alloc (ssecretkey, gchandletype. pinned); // encrypt the file. encryptfile (@ "C: \ mydata.txt", @ "C: \ encrypted.txt", ssecretkey); // decrypt the file. decryptfile (@ "C: \ encrypted.txt", @ "C: \ decrypted.txt", ssecretkey); // remove the key from memory. zeromemory (gch. addrofpinnedobject (), ssecretkey. length * 2); gch. free ();}}}

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.