C # Use des to encrypt and decrypt data

Source: Internet
Author: User

C #'s Des encryption and decryption support both. NET and. Net CF 2.0 platforms, so that a encapsulated class can be used on both PC and WM mobile phones. The DES in C # has more IV implementations than the des algorithms in other languages. In fact, the IV can be set to an arbitrary 8-bit byte. Note that DESC should be added. mode = ciphermode. ECB is compatible with DES encryption algorithms in other languages.


Paste the Code directly. The following code is common. Do not laugh!

 

Using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. security. cryptography; <br/> using system. globalization; <br/> using system. io; <br/> class cls_cryptography <br/> {<br/> /// <summary> <br/> // set class to 8 characters <br/> // /</Summary> <br/> Private Static byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef }; <br/> /// <summary> <br/> // encrypted string <br/> /// </Summary> <br/> // <returns> returns the ciphertext </returns> <br/> Public static string encryptstring (string key, string Str) <br/> {<br/> byte [] DATA = encoding. default. getbytes (STR); <br/> byte [] result = encryptdata (Key, data); <br/> If (result! = NULL) <br/> return convert. tobase64string (result, 0, result. length); <br/> else <br/> return ""; <br/>}< br/> /// <summary> <br/> // encrypt binary data <br/> /// </Summary> <br/> /// <returns> returns the binary ciphertext </returns> <br/> Public static byte [] encryptdata (string key, byte [] data) <br/>{< br/> byte [] bkey = encoding. default. getbytes (key. substring (0, 8); <br/> byte [] BIV = IV; <br/> try <br/>{< br/> descryptose Rviceprovider DESC = new descryptoserviceprovider (); <br/> DESC. mode = ciphermode. ECB; // compatible with DES encryption algorithms in other languages <br/> DESC. padding = paddingmode. zeros; // auto-fill 0 <br/> memorystream mstream = new memorystream (); <br/> cryptostream cstream = new cryptostream (mstream, DESC. createencryptor (bkey, BIV), cryptostreammode. write); <br/> cstream. write (data, 0, Data. length); <br/> cstream. flushfinalblock (); <br/> return Mstream. toarray (); <br/>}< br/> catch <br/> {<br/> return NULL; <br/>}< br/> // <summary> <br/> // decrypt the ciphertext to a plaintext. <br/> // </Summary> <br/> // <returns> Returns plaintext </returns> <br/> Public static string decryptstring (string key, string decryptstr) <br/>{< br/> byte [] DATA = convert. frombase64string (decryptstr); <br/> byte [] result = decryptdata (Key, data); <br/> If (result! = NULL) <br/> return encoding. default. getstring (result, 0, result. length); <br/> else <br/> return ""; <br/>}</P> <p> /// <summary> <br/> // decrypt the binary ciphertext into plain text binary. <br/> // </Summary> <br/> // <returns> returns the plaintext binary value </returns> <br/> Public static byte [] decryptdata (string key, byte [] data) <br/>{< br/> try <br/>{< br/> byte [] bkey = encoding. default. getbytes (key. substring (0, 8); <br/> byte [] BIV = IV; <br/> descryptoserviceprovider DESC = new descryptoserviceprovider (); <br/> DESC. mode = ciphermode. ECB; // compatible with DES encryption algorithms in other languages <br/> DESC. padding = paddingmode. zeros; // auto-fill 0 <br/> memorystream mstream = new memorystream (); <br/> cryptostream cstream = new cryptostream (mstream, DESC. createdecryptor (bkey, BIV), cryptostreammode. write); <br/> cstream. write (data, 0, Data. length); <br/> cstream. flushfinalblock (); <br/> return mstream. toarray (); <br/>}< br/> catch <br/> {<br/> return NULL; <br/>}< br/>}

 

As for how to call... To put it simply

 

String STR = cls_cryptography.encryptstring ("12345678", "12345678"); <br/> MessageBox. show (STR); <br/> STR = cls_cryptography.decryptstring ("12345678", STR); <br/> MessageBox. show (STR );

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.