// 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 "));}