DES encryption and decryption MD5 encryption help class, des encryption and decryption

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.