Des encryption and decryption method

Source: Internet
Author: User
1. Encryption Method 1/*** // <summary>
2 // Encryption Method
3 /// </Summary>
4 /// <Param name = "data"> string to be encrypted </param>
5 // <Param name = "key_64"> key </param>
6 /// <Param name = "iv_64"> key </param>
7 /// <returns> encrypted string </returns>
8 Public String encode (string data, string key_64, string iv_64)
9 {
10 byte [] bykey = system. Text. asciiencoding. ASCII. getbytes (key_64 );
11 byte [] byiv = system. Text. asciiencoding. ASCII. getbytes (iv_64 );
12
13 descryptoserviceprovider cryptoprovider = new descryptoserviceprovider ();
14 int I = cryptoprovider. keysize;
15 memorystream MS = new memorystream ();
16 cryptostream CST = new cryptostream (MS, cryptoprovider. createencryptor (bykey, byiv), cryptostreammode. Write );
17
18 streamwriter Sw = new streamwriter (Cst );
19 Sw. Write (data );
20 Sw. Flush ();
21 Cst. flushfinalblock ();
22 Sw. Flush ();
23 return convert. tobase64string (Ms. getbuffer (), 0, (INT) ms. Length );
24
25}

 

2. Decryption Method

1/** // <summary>
2 // Decryption Method
3 /// </Summary>
4 /// <Param name = "data"> string to be decrypted </param>
5 // <Param name = "key_64"> key </param>
6 /// <Param name = "iv_64"> key </param>
7 // <returns> decrypted string </returns>
8 Public String decode (string data, string key_64, string iv_64)
9 {
10 byte [] bykey = system. Text. asciiencoding. ASCII. getbytes (key_64 );
11 byte [] byiv = system. Text. asciiencoding. ASCII. getbytes (iv_64 );
12
13 byte [] byenc;
14 try
15 {
16 byenc = convert. frombase64string (data );
17}
18 catch
19 {
20 return NULL;
21}
22
23 descryptoserviceprovider cryptoprovider = new descryptoserviceprovider ();
24 memorystream MS = new memorystream (byenc );
25 cryptostream CST = new cryptostream (MS, cryptoprovider. createdecryptor (bykey, byiv), cryptostreammode. Read );
26 streamreader sr = new streamreader (Cst );
27 return Sr. readtoend ();
28}

3. Note: The two keys must be the same during encryption and decryption, that is, the four keys must be the same, for example, 1 result = encode (result, "vavicapp", "vavicapp ");
2
3 connstring = decode (connstring, "vavicapp", "vavicapp ");

4. Reference 1 using system. Security;
2 using system. Security. cryptography;

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.