AES Encryption decrypts C # code

Source: Internet
Author: User
Tags pkcs7
Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;  Using System.Security.Cryptography;        Using System.IO; namespace Aesdemo {public static class Aeshelper {//// AES encryption /////PlainText that is encrypted///Secret key///Vector///
 
  
   Ciphertext
  
           public static byte[] Aesencrypt (byte[] Data, String Key, String Vector) {byte[] bkey = NE              W BYTE[32];              Array.copy (Encoding.UTF8.GetBytes (Key.padright (bkey.length)), Bkey, bkey.length);              byte[] Bvector = new BYTE[16];                    Array.copy (Encoding.UTF8.GetBytes (Vector.padright (bvector.length)), Bvector, bvector.length); byte[] cryptograph = null;              Encrypted ciphertext Rijndael Aes = Rijndael.create ();                  try {//open a memory stream using (MemoryStream memories = new MemoryStream ()) {//wraps the memory stream object into an encrypted stream object using (CryptoStream encryptor = new CryptoStream (Memo                      Ry, Aes.createencryptor (Bkey, Bvector), CryptoStreamMode.Write)                   {//PlainText data written to encrypted stream Encryptor.write (data, 0, data.length);       Encryptor.flushfinalblock ();                      Cryptograph = Memory.toarray ();              }}} catch {cryptograph = null;          } return cryptograph;  }                ///// AES decryption /////Decrypted cipher.///Secret key///Vector///
 
  
   plaintext
  
           public static byte[] Aesdecrypt (byte[] Data, String Key, String Vector) {byte[] bkey = NE              W BYTE[32];              Array.copy (Encoding.UTF8.GetBytes (Key.padright (bkey.length)), Bkey, bkey.length);              byte[] Bvector = new BYTE[16];                    Array.copy (Encoding.UTF8.GetBytes (Vector.padright (bvector.length)), Bvector, bvector.length); byte[] original = null;              decrypted plaintext Rijndael Aes = Rijndael.create ();                  try {//open up a memory stream to store ciphertext using (MemoryStream memory = new MemoryStream (Data)) {//wraps the memory stream object into an encrypted stream object using (CryptoStream decryptor = new Cryptost                      Ream (Memory, Aes.createdecryptor (Bkey, Bvector), CryptoStreamMode.Read)) {//plaintext storage using (MemoryStream originalmemory = new Memory    Stream ())                      {byte[] Buffer = new byte[1024];                              Int32 readbytes = 0;                                  while ((Readbytes = Decryptor.read (Buffer, 0, buffer.length)) > 0) {                              Originalmemory.write (Buffer, 0, readbytes);                          } original = Originalmemory.toarray (); }}}} catch {original = Nu              ll          } return original;  }}} does not use vectors: public static class Aescrypto {////IV vectors are fixed values///           private static byte[] _iv = {//85, 60, 12, 116,//99, 189, 173, 19,//                138, 183, 232, 248,//82, 232, 200, 242//}; public static byte[] Decrypt (byte[] encryptedbytes, byte[] key) {MemoryStream mstream = new MemoryStream (encry  Ptedbytes);  Mstream.write (encryptedbytes, 0, encryptedbytes.length);  Mstream.seek (0, Seekorigin.begin);              RijndaelManaged AES = new RijndaelManaged (); Aes.              Mode = CIPHERMODE.ECB; Aes.              Padding = PADDINGMODE.PKCS7; Aes.  KeySize = 128; Aes.  key = key;  AES.IV = _iv; CryptoStream cryptostream = new CryptoStream (Mstream, AES.  CreateDecryptor (), cryptostreammode.read);  try {byte[] tmp = new byte[encryptedbytes.length + 32];  int len = cryptostream.read (tmp, 0, encryptedbytes.length + 32);  byte[] ret = new byte[Len];  Array.copy (TMP, 0, ret, 0, Len);  return ret;  } finally {cryptostream.close (); MsTream.  Close (); Aes.  Clear (); }} public static byte[] Encrypt (byte[] plainbytes, byte[] key) {MemoryStream mstre              am = new MemoryStream ();                    RijndaelManaged AES = new RijndaelManaged (); Aes.              Mode = CIPHERMODE.ECB; Aes.              Padding = PADDINGMODE.PKCS7; Aes.              KeySize = 128; Aes.                    Key = _key; Aes.              key = key;              AES.IV = _iv; CryptoStream cryptostream = new CryptoStream (Mstream, AES.              CreateEncryptor (), cryptostreammode.write);                  try {cryptostream.write (plainbytes, 0, plainbytes.length);                  Cryptostream.flushfinalblock ();              return Mstream.toarray ();                  } finally {cryptostream.close ();                  Mstream.close (); Aes.              Clear (); }          }      }
  • Related Article

    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.