C # DES encryption class, 16-bit encryption .,

Source: Internet
Author: User

C # DES encryption class, 16-bit encryption .,

This Encryption Class is self-written because it is not the same as the DES encryption written in java. It is finally the same as Java encryption, and solves the different problems after encryption.

You can directly call the encryption and decryption methods in it.

Using System; using System. collections. generic; using System. linq; using System. web; using System. text; using System. security. cryptography; using System. IO; namespace EallNum. helper {public class FI_DesTools {private FI_DesTools () {} private static string key = "×××× "; /// <summary> /// Key for symmetric encryption and decryption /// </summary> public static string key {get {return key;} set {Key = value ;}} /// <summary> // DES encryption /// </summary> /// <param name = "encryptString"> </param> /// <returns> </returns> public static string DesEncrypt (string strEncryptString) {StringBuilder strRetValue = new StringBuilder (); try {byte [] keyBytes = Encoding. UTF8.GetBytes (key. substring (0, 8); byte [] keyIV = keyBytes; byte [] inputByteArray = Encoding. UTF8.GetBytes (strEncryptString); DESCryptoServiceProvider provider = new DESCryptoServiceProvider (); provider. mode = CipherMode. ECB; // compatible with Des encryption algorithm provider in other languages. padding = PaddingMode. zeros; // auto-fill 0 MemoryStream mStream = new MemoryStream (); CryptoStream cStream = new CryptoStream (mStream, provider. createEncryptor (keyBytes, keyIV), CryptoStreamMode. write); cStream. write (inputByteArray, 0, inputByteArray. length); cStream. flushFinalBlock (); // base64 encoding is not used // return Convert. toBase64String (mStream. toArray (); // It is organized into a hexadecimal string foreach (byte B in mStream. toArray () {strRetValue. appendFormat ("{0: X2}", B) ;}} catch (Exception e) {Console. writeLine (e);} return strRetValue. toString ();} /// <summary> /// DES decryption /// </summary> /// <param name = "decryptString"> </param> /// <returns> </returns> public static string DesDecrypt (string strDecryptString) {string strRetValue = ""; try {byte [] keyBytes = Encoding. UTF8.GetBytes (key. substring (0, 8); byte [] keyIV = keyBytes; // base64 decoding is not used. // byte [] inputByteArray = Convert. fromBase64String (decryptString); // hexadecimal conversion to byte [] inputByteArray = new byte [strDecryptString. length/2]; for (int x = 0; x <strDecryptString. length/2; x ++) {int I = (Convert. toInt32 (strDecryptString. substring (x * 2, 2), 16); inputByteArray [x] = (byte) I;} DESCryptoServiceProvider provider = new DESCryptoServiceProvider (); provider. mode = CipherMode. ECB; // compatible with Des encryption algorithm provider in other languages. padding = PaddingMode. zeros; // auto-fill 0 MemoryStream mStream = new MemoryStream (); CryptoStream cStream = new CryptoStream (mStream, provider. createDecryptor (keyBytes, keyIV), CryptoStreamMode. write); cStream. write (inputByteArray, 0, inputByteArray. length); cStream. flushFinalBlock (); // remove the ending null character // strRetValue = Encoding. UTF8.GetString (mStream. toArray (); strRetValue = Encoding. UTF8.GetString (mStream. toArray ()). trimEnd ('\ 0');} catch (Exception e) {Console. writeLine (e) ;}return strRetValue ;}}}

 

Reference page: http://qingqingquege.cnblogs.com/p/5933752.html

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.