Encryption and decryption in. NET

Source: Internet
Author: User

To encrypt a string, perform the following steps:

1. Convert the source string into a byte array

2. Initialize an encryption algorithm class

3. Use this encryption algorithm class to generate an encryption entity (encryptor object) and implement the IcryptoTransform interface. He needs the key and IV value.

4. Use the encryption object to initialize a ciphertext data stream (CryptoStream object ). This data stream also needs to know which data you want to encrypt and the target data stream used to write the encrypted data.

5. Use this ciphertext data stream to generate encrypted data and write it to the target memory data stream created by the Source byte array created earlier.

6. Obtain the byte data stored in this data stream.

7. convert these bytes into a string.

 

The decryption mode is similar to encryption:

1. Convert the source string into a byte array.

2. Fill in the memory data stream value based on the byte array.

3. Initialize an encryption algorithm class.

4. Use the encryption algorithm class to generate a decryptor object and implement the ICrytoTransform interface. He needs the key and IV value.

5. Use the decryption object to initialize a ciphertext data stream (CryptoStream object ). This data stream also needs to know what data you want to decrypt and a source data stream from which encrypted data is read.

6. Use the ciphertext data stream to read the decrypted data (you can use StreamReader. ReadToEnd to obtain the string type result ).

 

Sample Code:

Namespace SecurityLib {// <summary> // StringEncryptor abstract description /// </summary> public static class StringEncryptor {public static string Encrypt (string sourceData) {byte [] key = new byte [] {1, 2, 3, 4, 5, 6, 7, 8}; byte [] iv = new byte [] {1, 2, 3, 4, 5, 6, 7, 8}; try {byte [] sourceDataBytes = System. text. ASCIIEncoding. ASCII. getBytes (sourceData); MemoryStream tempStream = new MemoryStream (); DESCryptoServiceProvider encryptor = new DESCryptoServiceProvider (); CryptoStream encryptionStream = new CryptoStream (tempStream, encryptor. createEncryptor (key, iv), CryptoStreamMode. write); encryptionStream. write (sourceDataBytes, 0, sourceDataBytes. length); encryptionStream. flushFinalBlock (); byte [] encryptedDataBytes = tempStream. getBuffer (); return Convert. toBase64String (encryptedDataBytes, 0, (int) tempStream. length);} catch {throw new StringEncryptorException ("Unable to encrypt data. ") ;}} public static string Decrypt (string sourceData) {byte [] key = new byte [] {1, 2, 3, 4, 5, 6, 7, 8}; byte [] iv = new byte [] {1, 2, 3, 4, 5, 6, 7, 8}; try {byte [] encryptedDataBytes = Convert. fromBase64String (sourceData); MemoryStream tempStream = new MemoryStream (encryptedDataBytes, 0, encryptedDataBytes. length); DESCryptoServiceProvider decryptor = new DESCryptoServiceProvider (); CryptoStream decryptionStream = new CryptoStream (tempStream, decryptor. createDecryptor (key, iv), CryptoStreamMode. read); StreamReader allDataReader = new StreamReader (decryptionStream); return allDataReader. readToEnd ();} catch {throw new StringEncryptorException ("Unable to decrypt data. ");}}}}

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.