C # DES encryption and decryption,
Using System; using System. collections. generic; using System. IO; using System. linq; using System. security. cryptography; using System. text; namespace ConsoleApp1 {public class EncryptUtil {// <summary> // MD5 and password string /// </summary> /// <param name = "sInputString"> Message Body </param> // <returns> MD5 Signature character </returns> public static string MD5Encrypt (string sInputString) {System. security. cryptography. MD5 md5 = System. Security. cryptography. MD5.Create (); string encoded = BitConverter. toString (md5.ComputeHash (Encoding. UTF8.GetBytes (sInputString ))). replace ("-", ""); return encoded ;} /// <summary> // DES encryption // </summary> /// <param name = "sInputString"> encrypted message body </param> // /<param name = "sKey"> 24-bit key </param> // <returns> decrypted message body </returns> public static string DESEncryptBase64 (string sInputString, string sKey) {if (s Tring. IsNullOrEmpty (sInputString) | string. IsNullOrEmpty (sKey) | sKey. Length! = 24) {return string. empty;} DESCryptoServiceProvider des = new DESCryptoServiceProvider (); des. mode = CipherMode. ECB; des. padding = PaddingMode. PKCS7; string key = sKey. substring (0, 12); string iv = sKey. remove (0, 12); byte [] byKey = Convert. fromBase64String (key); byte [] byIV = Convert. fromBase64String (iv); byte [] inputByteArray = Encoding. UTF8.GetBytes (sInputString); MemoryStream MS = new Memor YStream (); CryptoStream cs = new CryptoStream (MS, des. createEncryptor (byKey, byIV), CryptoStreamMode. write); cs. write (inputByteArray, 0, inputByteArray. length); cs. flushFinalBlock (); return Convert. toBase64String (ms. toArray ());} /// <summary> // DES decryption // </summary> /// <param name = "sInputString"> message body to be encrypted </param> /// <param name = "sKey"> 24-bit key </param> // <returns> the encrypted message body </returns> public static st Ring DESDecryptBase64 (string sInputString, string sKey) {if (string. IsNullOrEmpty (sInputString) | string. IsNullOrEmpty (sKey) | sKey. Length! = 24) {return string. empty;} DESCryptoServiceProvider des = new DESCryptoServiceProvider (); des. mode = CipherMode. ECB; des. padding = PaddingMode. PKCS7; string key = sKey. substring (0, 12); string iv = sKey. remove (0, 12); byte [] byKey = Convert. fromBase64String (key); byte [] byIV = Convert. fromBase64String (iv); byte [] inputByteArray = Convert. fromBase64String (sInputString); MemoryStream MS = new MemoryStream (); CryptoStream cs = new CryptoStream (MS, des. createDecryptor (byKey, byIV), CryptoStreamMode. write); cs. write (inputByteArray, 0, inputByteArray. length); cs. flushFinalBlock (); Encoding encoding = new UTF8Encoding (); return encoding. getString (ms. toArray ());}}}