DES encryption and decryption MD5 encryption help class, des encryption and decryption
Public class TrialHelper {// default key vector private static byte [] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; /// <summary> // DES encrypted string /// </summary> /// <param name = "encryptString"> string to be encrypted </param> // /<param name = "encryptKey"> encryption key, the value must be 8-bit </param> /// <returns>. The encrypted string is returned successfully, and the source string </returns> public static string EncryptDES (string encryptString, string encryptKey = "") {try {if (string. isNullOrEmpty (encryptKey) | encryptKey. length <8) {encryptKey = "winform01";} byte [] rgbKey = Encoding. UTF8.GetBytes (encryptKey. substring (0, 8); byte [] rgbIV = Keys; byte [] inputByteArray = Encoding. UTF8.GetBytes (encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider (); MemoryStream mStream = new MemoryStream (); CryptoStream cStream = new CryptoStream (mStream, dCSP. createEncryptor (rgbKey, rgbIV), CryptoStreamMode. write); cStream. write (inputByteArray, 0, inputByteArray. length); cStream. flushFinalBlock (); return Convert. toBase64String (mStream. toArray ();} catch {return encryptString ;}} /// <summary> // DES decryption string /// </summary> /// <param name = "decryptString"> string to be decrypted </param> // /<param name = "decryptKey"> decrypt the key, the value must be 8 bits. It must be the same as the encryption key. </param> /// <returns> the decrypted string is returned, returned source string failed </returns> public static string DecryptDES (string decryptString, string decryptKey = "") {try {if (string. isNullOrEmpty (decryptKey) | decryptKey. length <8) {decryptKey = "winform01";} byte [] rgbKey = Encoding. UTF8.GetBytes (decryptKey. substring (0, 8); byte [] rgbIV = Keys; byte [] inputByteArray = Convert. fromBase64String (decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider (); MemoryStream mStream = new MemoryStream (); CryptoStream cStream = new CryptoStream (mStream, DCSP. createDecryptor (rgbKey, rgbIV), CryptoStreamMode. write); cStream. write (inputByteArray, 0, inputByteArray. length); cStream. flushFinalBlock (); return Encoding. UTF8.GetString (mStream. toArray ();} catch {return decryptString ;}} /// <summary> /// MD5 Data Encryption /// </summary> /// <param name = "sDataIn"> encryption field </param> /// <returns> encrypted string </returns> public static string GetMD5 (string sDataIn) {System. security. cryptography. MD5CryptoServiceProvider md5 = new System. security. cryptography. MD5CryptoServiceProvider (); byte [] bytValue, bytHash; bytValue = System. text. encoding. UTF8.GetBytes (sDataIn); bytHash = md5.ComputeHash (bytValue); md5.Clear (); string sTemp = ""; for (int I = 0; I <bytHash. length; I ++) {sTemp + = bytHash [I]. toString ("x "). padLeft (2, '0');} return sTemp ;}}
Call:
// Obtain the logon information loginRecord. name = tbName. text. trim (); loginRecord. MD5Pwd = TrialHelper. getMD5 (tbPwd. password); // save it to the database. The MD5 encryption method is loginRecord. pwd = TrialHelper. encryptDES (tbPwd. password); // remember the Password des encryption method and save it to your local device.