// Default key vector private static byte [] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; ////// DES encrypted string //////String to be encrypted///Encryption key, which must be 8 bits///
The encrypted string is returned successfully. If the encrypted string fails, the source string is returned.
Public static string EncryptDES (string encryptString, string encryptKey) {try {byte [] rgbKey = Encoding. UTF8.GetBytes (encryptKey. substring (0, 8); byte [] rgbIV = Keys; byte [] inputByteArray = Encoding. UTF8.GetBytes (encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider (); MemoryStream mStream = new MemoryStream (); CryptoStream cStream = new CryptoStream (mStream, dCSP. createEncryptor (rgbKey, rgbIV), CryptoStreamMode. write); cStream. write (inputByteArray, 0, inputByteArray. length); cStream. flushFinalBlock (); return Convert. toBase64String (mStream. toArray ();} catch {return encryptString ;}}////// DES decryption string //////String to be decrypted///Decryption key, which must be 8 bits, the same as the encryption key///
The decrypted string is returned successfully, and the source string fails to be returned.
Public static string DecryptDES (string decryptString, string decryptKey) {try {byte [] rgbKey = Encoding. UTF8.GetBytes (decryptKey); byte [] rgbIV = Keys; byte [] inputByteArray = Convert. fromBase64String (decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider (); MemoryStream mStream = new MemoryStream (); CryptoStream cStream = new CryptoStream (mStream, DCSP. createDecryptor (rgbKey, rgbIV), CryptoStreamMode. write); cStream. write (inputByteArray, 0, inputByteArray. length); cStream. flushFinalBlock (); return Encoding. UTF8.GetString (mStream. toArray ();} catch {return decryptString ;}}