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 ();}}}