C # encryption and decryption of strings or byte [] in memory

Source: Internet
Author: User

The program is as follows: the two most important functions are Encrypt and Decrypt. the encryption and decryption operations are performed respectively, and MemoryStream is used as the CryptoStream operation object. You only need to remember the encryption Key and initial vector (Key and IV) generated before encryption. You can use this Key and IV to encrypt and decrypt byte. As for the string, use Encoding to convert it to byte.

The encryption method is AES. You can change it to another encryption algorithm as needed.

Run:

Code:

Using System; </p> <p> using System. text; </p> <p> using System. IO; </p> <p> using System. security. cryptography; </p> <p> namespace Mgen. TTC </p> <p >{</p> <p> class Program </p> <p >{</p> <p> static void Main () </p> <p >{</p> <p> using (var aes = Aes. create () </p> <p >{</p> <p> byte [] key, iv; </p> <p> // by default, keys and IV are generated randomly. Here, we emphasize this process </p> <p> aes. generateKey (); </p> <p> aes. generateIV (); </p> <p> key = aes. key; </p> <p> iv = aes. IV; </p> <p> Console. writeLine ("Enter the text to be encrypted"); </p> <p> string src = Console. readLine (); </p> <p> Console. writeLine (); </p> <p> var datRaw = Encoding. default. getBytes (src); </p> <p> var datEnc = Encrypt (datRaw, key, iv); </p> <p> Console. writeLine ("Encrypted Key:"); </p> <p> PrintBytes (key); </p> <p> Console. writeLine (); </p> <p> Console. writeLine ("encrypted IV:"); </p> <p> PrintBytes (iv); </p> <p> Console. writeLine (); </p> <p> Console. writeLine ("source data"); </p> <p> PrintBytesB64 (datRaw); </p> <p> Console. writeLine (); </p> <p> Console. writeLine ("encrypted data"); </p> <p> PrintBytesB64 (datEnc); </p> <p> Console. writeLine (); </p> <p> Console. writeLine ("text decrypted using Key and IV"); </p> <p> var dec = Decrypt (datEnc, key, iv ); </p> <p> Console. writeLine (Encoding. default. getString (dec); </p> <p >}</p> <p> static byte [] Encrypt (byte [] src, byte [] key, byte [] iv) </p> <p >{</p> <p> using (var aes = Aes. create () </p> <p >{</p> <p> aes. key = key; </p> <p> aes. IV = iv; </p> <p> using (var MS = new MemoryStream () </p> <p> using (var cstream = new CryptoStream (MS, aes. createEncryptor (), CryptoStreamMode. write) </p> <p >{</p> <p> cstream. write (src, 0, src. length); </p> <p> cstream. close (); </p> <p> ms. close (); </p> <p> return ms. toArray (); </p> <p >}</p> <p> static byte [] Decrypt (byte [] dest, byte [] key, byte [] iv) </p> <p >{</p> <p> using (var aes = Aes. create () </p> <p >{</p> <p> aes. key = key; </p> <p> aes. IV = iv; </p> <p> using (var MS = new MemoryStream (dest) </p> <p> using (var destMs = new MemoryStream ()) </p> <p> using (var cstream = new CryptoStream (MS, aes. createDecryptor (), CryptoStreamMode. read) </p> <p >{</p> <p> byte [] data = new byte [100]; </p> <p> int readLen; </p> <p> while (readLen = cstream. read (data, 0,100)> 0) </p> <p> destMs. write (data, 0, readLen); </p> <p> return destMs. toArray (); </p> <p >}</p> <p> static void PrintBytes (byte [] data) </p> <p >{</p> <p> if (data = null) </p> <p> throw new ArgumentNullException ("data "); </p> <p> foreach (var B in data) </p> <p> Console. write ("{0: X2}", B); </p> <p> Console. writeLine (); </p> <p >}</p> <p> static void PrintBytesB64 (byte [] data) </p> <p >{</p> <p> if (data = null) </p> <p> throw new ArgumentNullException ("data "); </p> <p> Console. writeLine (Convert. toBase64String (data, Base64FormattingOptions. insertLineBreaks); </p> <p >}</p> <p>}

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.