Using system;using system.collections.generic;using system.text;using system.io;using System.Security.Cryptography; Namespace CMD. edi{public class Encrypthandler {//<summary>///encryption string///</summary> PU Blic 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; } symmetricalgorithm 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 string///</summary> public static string Decrypt (String PASSW Ord, 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; } symmetricalgorithm 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, packet decryption failed."); } return cipher; } }}
A method of symmetric encryption and decryption C # tool class