A simple C # cryptographic decryption class

Source: Internet
Author: User

//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

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.