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 (); } } }