C # DES encryption/Decryption string

Source: Internet
Author: User

 

 Using  System;  Using  System. text;  Using  System. Security. cryptography;  Using  System. IO;  //  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, which must be 8 bits  </Param>  ///   <Returns>  The encrypted string is returned successfully. If the encrypted string fails, the source string is returned.  </Returns>  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 ;}}  ///   <Summary>  ///  Des decryption string  ///   </Summary>  ///   <Param name = "decryptstring">  String to be decrypted  </Param>  ///   <Param name = "decryptkey">  Decryption key, which must be 8 bits, the same as the encryption key </Param>  ///   <Returns>  The decrypted string is returned successfully, and the source string fails to be returned.  </Returns>  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 ;}} 

 

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.