//des default key vectorPrivate Static byte[] Keys = {0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF };/// <summary>///des encrypted String/// </summary>/// <param name= "encryptstring" >string to encrypt</param>/// <param name= "Encryptkey" >encryption key, required for 8-bit</param>/// <returns>encryption successfully returns the encrypted string, failed to return the source string</returns> Public Static stringEncryptdes (stringEncryptString,stringEncryptkey) { Try { byte[] RgbKey = Encoding.UTF8.GetBytes (encryptkey.substring (0,8)); byte[] Rgbiv =Keys; byte[] Inputbytearray =Encoding.UTF8.GetBytes (encryptstring); DESCryptoServiceProvider DCSP=NewDESCryptoServiceProvider (); MemoryStream Mstream=NewMemoryStream (); CryptoStream Cstream=NewCryptoStream (Mstream, Dcsp.createencryptor (RgbKey, Rgbiv), cryptostreammode.write); Cstream.write (Inputbytearray,0, inputbytearray.length); Cstream.flushfinalblock (); returnconvert.tobase64string (Mstream.toarray ()); } Catch { returnencryptstring; }}/// <summary>///des decryption String/// </summary>/// <param name= "decryptstring" >string to decrypt</param>/// <param name= "Decryptkey" >Decryption key, required to be 8-bit, same as encryption key</param>/// <returns>decryption succeeded in returning the decrypted string, failed to return the source string</returns> Public Static stringDecryptdes (stringDecryptstring,stringDecryptkey) { Try { byte[] RgbKey =Encoding.UTF8.GetBytes (Decryptkey); byte[] Rgbiv =Keys; byte[] Inputbytearray =convert.frombase64string (decryptstring); DESCryptoServiceProvider DCSP=NewDESCryptoServiceProvider (); MemoryStream Mstream=NewMemoryStream (); CryptoStream Cstream=NewCryptoStream (Mstream, DCSP. CreateDecryptor (RgbKey, Rgbiv), cryptostreammode.write); Cstream.write (Inputbytearray,0, inputbytearray.length); Cstream.flushfinalblock (); returnEncoding.UTF8.GetString (Mstream.toarray ()); } Catch { returndecryptstring; }}
A simple C # encryption and decryption class