C # DES encryption and decryption,

Source: Internet
Author: User
Tags pkcs7

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 ());}}}

  

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.