C # implement 3Des encryption and decryption

Source: Internet
Author: User

3Des implements three DES encryption algorithms for each data block, which is a safer form of DES. 3DES is safer than the original DES. It's all clear. The following is the source code for encryption and decryption. ECB mode. 1 public class _ 3 DESEncrypt 2 {3 4 public static string Encrypt3DES (string a_strString, string a_strKey) 5 {6 TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider (); 7 DES. key = ASCIIEncoding. ASCII. getBytes (System. web. security. formsAuthentication. hashPasswordForStoringInConfigFile (a_strKey, "md5 "). substring (0, 24); 8 DES. mode = CipherMode. ECB; 9 ICryptoTransform DESEncrypt = DES. createEncryptor (); 10 byte [] Buffer = ASCIIEncoding. ASCII. getBytes (a_strString); 11 return Convert. toBase64String (DESEncrypt. transformFinalBlock (Buffer, 0, Buffer. length); 12} 13 14 public static string Decrypt3DES (string a_strString, string a_strKey) 15 {16 TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider (); 17 DES. key = ASCIIEncoding. ASCII. getBytes (System. web. security. form SAuthentication. hashPasswordForStoringInConfigFile (a_strKey, "md5 "). substring (0, 24); 18 DES. mode = CipherMode. ECB; 19 DES. padding = System. security. cryptography. paddingMode. PKCS7; 20 ICryptoTransform DESDecrypt = DES. createDecryptor (); 21 string result = ""; 22 try23 {24 byte [] Buffer = Convert. fromBase64String (a_strString); 25 26 result = ASCIIEncoding. ASCII. getString (DESDecrypt. transformFinalBlock (B Uffer, 0, Buffer. length); 27 28 // MemoryStream msDecrypt = new MemoryStream (Buffer); 29 // CryptoStream csDecrypt = new CryptoStream (msDecrypt, 30 // DES. createDecryptor (DES. key, DES. IV), 31 // CryptoStreamMode. read); 32 33 // Create buffer to hold the decrypted data.34 // byte [] fromEncrypt = new byte [Buffer. length]; 35 36 // Read the decrypted data out of the crypto stream37 // and place it The temporary buffer.38 // csDecrypt. read (fromEncrypt, 0, fromEncrypt. length); 39 // result = System. text. encoding. default. getString (fromEncrypt); 40} 41 catch (Exception e) 42 {43} 44 return result; in 45 46} 47}, encryption and decryption are implemented based on DES. The difference is that the Key value of 3Des is 24 bits, and DES is 8 bits.

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.