Encrypt byte array with AES - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:AES

Description

Encrypt byte array with AES

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Security.Cryptography;
using System.Linq;
using System.IO;/*w  w  w .  ja v  a 2  s  .  c  o m*/
using System.Collections.Generic;
using System;

public class Main{
        public static byte[] Encrypt(byte[] input, string password)
        {
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[] { 0x43, 0x87, 0x23, 0x72, 0x45, 0x56, 0x68, 0x14, 0x62, 0x84 });
            MemoryStream ms = new MemoryStream();
            Aes aes = new AesManaged();
            aes.Key = pdb.GetBytes(aes.KeySize / 8);
            aes.IV = pdb.GetBytes(aes.BlockSize / 8);
            CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(input, 0, input.Length);
            cs.Close();
            return ms.ToArray();
        }
        public static string Encrypt(string input, string password)
        {
            return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(input), password));
        }
}

Related Tutorials