1 using System; 2 using System. collections. generic; 3 using System. text; 4 using System. IO; 5 using System. runtime. serialization; 6 using System. security. cryptography; 7 8 namespace Sky. decrypt 9 {10 /// <summary> 11 // Decrypt 12 /// </summary> 13 public class Decryption14 {15 public Decryption () 16 {17} 18 19 /// <summary> 20 // get the file content-string 21 /// </summary> 22 /// <param name = "path"> file path </param> 23 // <re Turns> file content </returns> 24 public string GetString (string path) 25 {26 return this. deserializeFile (path ); 27} 28 29 // <summary> 30 // deserialization file 31 // </summary> 32 // <param name = "path"> file path </param> 33 // <returns> file content </returns> 34 private string DeserializeFile (string path) 35 {36 string str = ""; 37 38 if (! File. Exists (path) 39 {40 throw new Exception ("File is not exist! "); 41} 42 43 IFormatter binaryFormatter = new System. runtime. serialization. formatters. binary. binaryFormatter (); 44 using (FileStream fileStream = new FileStream (path, FileMode. open, FileAccess. read) 45 {46 str = (string) binaryFormatter. deserialize (fileStream); 47 fileStream. close (); 48} 49 50 return str; 51} 52 53 public string DecryptString (string data, string key) 54 {55 string str = string. empty; 56 57 if (string. isNullOrEmpty (data) 58 {59 throw new Exception ("data is empty"); 60} 61 62 MemoryStream MS = new MemoryStream (); 63 byte [] myKey = Encoding. UTF8.GetBytes (key); 64 byte [] myIV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 65 66 DES myProvider = new DESCryptoServiceProvider (); 67 CryptoStream cs = new CryptoStream (MS, myProvider. createDecryptor (myKey, myIV), CryptoStreamMode. write); 68 69 try70 {71 byte [] bs = Convert. fromBase64String (data); 72 cs. write (bs, 0, bs. length); 73 cs. flushFinalBlock (); 74 str = Encoding. UTF8.GetString (ms. toArray (); 75} 76 finally77 {78 cs. close (); 79 ms. close (); 80} 81 return str; 82} 83} 84}
Encryption:
Using System; using System. collections. generic; using System. text; using System. runtime. serialization; using System. IO; using System. security. cryptography; namespace Sky. encrypt {// <summary> /// Encryption /// </summary> public class Encryption {/// <summary> /// generate the Certificate file /// </ summary> /// <param name = "data"> Registration Information </param> /// <param name = "fileName"> Certificate file path </param> /// <param name = "key"> </param> public void Gener AteFile (string data, string fileName, string key) {string str = this. encryptString (data, key); this. serializeFile (str, fileName );} /// <summary> /// serialize the object /// </summary> /// <param name = "data"> data string </param> /// <param name = "path"> file path </param> private void SerializeFile (string data, string path) {IFormatter binaryFormatter = new System. runtime. serialization. formatters. binary. binaryFormatter (); if (d Ata! = Null) {using (FileStream fileStream = new FileStream (path, FileMode. create, FileAccess. write) {binaryFormatter. serialize (fileStream, data); fileStream. close () ;}} public string EncryptString (string data, string key) {string str = string. empty; if (string. isNullOrEmpty (data) {return str;} MemoryStream MS = new MemoryStream (); byte [] myKey = Encoding. UTF8.GetBytes (key); byte [] myIV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; DES myProvider = new DESCryptoServiceProvider (); CryptoStream cs = new CryptoStream (MS, myProvider. createEncryptor (myKey, myIV), CryptoStreamMode. write); try {byte [] bs = Encoding. UTF8.GetBytes (data); cs. write (bs, 0, bs. length); cs. flushFinalBlock (); str = Convert. toBase64String (ms. toArray ();} finally {cs. close (); ms. close () ;}return str ;}}}
Call the encrypted file:
Encryption encry = new Encryption ();
String xmldata = File. ReadAllText ("File Path 1 ");
String data = encry. EncryptString (xmldata, "abcdefgh"); // abcdefgh key, password
File. WriteAllText ("Save to File 2", data );
Decryption
Decryption decrypt = new Decryption ();
String strData = File. ReadAllText ("Save to File 2 ");
String newData = decrypt. DecryptString (strData, "abcdefgh"); // abcdefgh encrypts the key