A symmetric encryption and decryption method C # tool class,
Using System; using System. collections. generic; using System. text; using System. IO; using System. security. cryptography; namespace CMD. EDI {public class EncryptHandler {// <summary> // encrypted string /// </summary> public static string Encrypt (string password, string cleartext) {string password2 = "Ahbool"; string cipher; char [] key = new char [8]; if (password. length> 8) {password = password. remove (8);} password. copyTo (0, key, 0, password. length); char [] iv = new char [8]; if (password2.Length> 8) {password2 = password2.Remove (8);} password2.CopyTo (0, iv, 0, password2.Length ); if (cleartext = null) {return string. empty;} Your ricalgorithm serviceProvider = new DESCryptoServiceProvider (); serviceProvider. key = Encoding. ASCII. getBytes (key); serviceProvider. IV = Encoding. ASCII. getBytes (iv); MemoryStream memoryStream = new MemoryStream (); CryptoStream cryptoStream = new CryptoStream (memoryStream, serviceProvider. createEncryptor (), CryptoStreamMode. write); StreamWriter streamWriter = new StreamWriter (cryptoStream); streamWriter. write (cleartext); streamWriter. dispose (); cryptoStream. dispose (); byte [] signData = memoryStream. toArray (); memoryStream. dispose (); serviceProvider. clear (); cipher = Convert. toBase64String (signData); return cipher;} // <summary> // Decrypt the string /// </summary> public static string Decrypt (string password, string ciphertext) {string password2 = "Ahbool"; string cipher = string. empty; try {char [] key = new char [8]; if (password. length> 8) {password = password. remove (8);} password. copyTo (0, key, 0, password. length); char [] iv = new char [8]; if (password2.Length> 8) {password2 = password2.Remove (8);} password2.CopyTo (0, iv, 0, password2.Length ); if (ciphertext = null) {return cipher;} specify ricalgorithm serviceProvider = new DESCryptoServiceProvider (); serviceProvider. key = Encoding. ASCII. getBytes (key); serviceProvider. IV = Encoding. ASCII. getBytes (iv); byte [] contentArray = Convert. fromBase64String (ciphertext); MemoryStream memoryStream = new MemoryStream (contentArray); CryptoStream cryptoStream = new CryptoStream (memoryStream, serviceProvider. createDecryptor (), CryptoStreamMode. read); StreamReader streamReader = new StreamReader (cryptoStream); cipher = streamReader. readToEnd (); streamReader. dispose (); cryptoStream. dispose (); memoryStream. dispose (); serviceProvider. clear ();} catch (Exception ex) {throw new SystemException ("key error, data packet decryption failed. ") ;}return cipher ;}}}