C # DES encryption and decryption string

Source: Internet
Author: User
// Namespace using system; using system. security. cryptography; using system. io; using system. text;/** // <summary> // DES algorithm description: // des is the abbreviation of Data Encryption Standard. It is an encryption algorithm developed by IBM. // The U.S. National Bureau of Standards announced on 1977 that it serves as a Data Encryption Standard for non-confidential departments; /// it is a grouping encryption algorithm that encrypts data in 64-bit groups. /// Des is also a symmetric algorithm: The same algorithm is used for encryption and decryption. /// Its key length is 56 bits (because each 8th bits are used for parity check), // The key can be any 56 bits and can be changed at any time. /// </Summary> // 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 {byte [] Key = encoding. utf8.getbytes (encryptkey. substring (0, 8); byte [] IV = keys; byte [] inputbytearray = encoding. utf8.getbytes (encryptstring); descryptoserviceprovider dcsp = new descryptoserviceprovider (); memorystream mstream = new memorystream (); cryptostream cstream = new cryptostream (mstream, dcsp. createencryptor (Key, IV), 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 descryptdes (string descryptstring, string descryptkey) {try {byte [] Key = encoding. utf8.getbytes (descryptkey. substring (0, 8); byte [] IV = keys; byte [] inputbytearray = convert. frombase64string (descryptstring); descryptoserviceprovider dcsp = new descryptoserviceprovider (); memorystream mstream = new memorystream (); cryptostream cstream = new cryptostream (mstream, dcsp. createdecryptor (Key, IV), cryptostreammode. write); cstream. write (inputbytearray, 0, inputbytearray. length); cstream. flushfinalblock (); Return encoding. utf8.getstring (mstream. toarray ();} catch {return descryptstring ;}}

Instance

Protected void Page_Load (object sender, EventArgs e) {Response. Write ("encrypted character: hello world !, Key: hongkaihua@126.com "); string str = EncryptDES (" hello world! "," Hongkaihua@126.com "); Response. write ("DES encryption:" + str); Response. write ("DES decryption:" + DescryptDES (str, "hongkaihua@126.com "));}

 

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.