Encryption and decryption Methods

Source: Internet
Author: User

The encryption and decryption code is as follows:

1 /// <summary> 2 /// encrypted string: returns the encrypted string 3 // </Summary> 4 public static string encrypt (string value, string key) based on the input string and key) 5 {6 try 7 {8 Key + = "[email protected] # $ % ^ & * ()"; 9 byte [] bytecrypt = {0x10, 0x29, 38, 0x47, 0x56}; 10 byte [] bytekey = system. text. encoding. utf8.getbytes (key. substring (0, 10); 11 descryptoserviceprovider des = new descryptoserviceprovider (); 12 byte [] inputbytearray = system. text. encoding. utf8.getbytes (value); 13 memorystream MS = new memorystream (); 14 cryptostream cs = new cryptostream (MS, Des. createencryptor (bytekey, bytecrypt), cryptostreammode. write); 15 CS. write (inputbytearray, 0, inputbytearray. length); 16 CS. flushfinalblock (); 17 return convert. tobase64string (Ms. toarray (); 18} 19 catch (exception ex) 20 {21 throw ex; 22} 23} 24 25 // <summary> 26 // encrypted string: the encrypted string 27 // </Summary> 28 public static string encrypt (string value) 29 {30 return encrypt (value, String. empty); 31} 32 33 // <summary> 34 // decrypt the string: return the decrypted string 35 // </Summary> 36 public static string decrypt (string value, string key) based on the input string and key) 37 {38 try39 {40 key + = "[email protected] # $ % ^ & * ()"; 41 byte [] bytecrypt = {0x10, 0x29, 38, 0x47, 0x56}; 42 byte [] bytekey = system. text. encoding. utf8.getbytes (key. substring (0, 10); 43 descryptoserviceprovider des = new descryptoserviceprovider (); 44 byte [] inputbytearray = convert. frombase64string (value); 45 memorystream MS = new memorystream (); 46 cryptostream cs = new cryptostream (MS, Des. createdecryptor (bytekey, bytecrypt), cryptostreammode. write); 47 CS. write (inputbytearray, 0, inputbytearray. length); 48 CS. flushfinalblock (); 49 system. text. encoding encoding = system. text. encoding. utf8; 50 return encoding. getstring (Ms. toarray (); 51} 52 catch (exception ex) 53 {54 throw ex; 55} 56} 57 58 // <summary> 59 // decrypt the string: return the decrypted string 60 // </Summary> 61 Public static string decrypt (string value) 62 {63 return decrypt (value, String. empty); 64}
View code

 

Encryption and decryption Methods

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.