Encrypt 128 ECB - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:AES

Description

Encrypt 128 ECB

Demo Code


using System.Text;
using System.Security.Cryptography;

public class Main{
        public static string Encrypt128ECB(string data, string key)
        {/*from w  w  w  .  java2s  .com*/
            var keyBytes = Encoding.ASCII.GetBytes(key);
            var aes = Aes.Create();
            aes.BlockSize = 128;
            aes.KeySize = 128;
            aes.Key = keyBytes;
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.Zeros;
            aes.IV = keyBytes;
            
            var encryptor = aes.CreateEncryptor(keyBytes, keyBytes);
            var res = encryptor.TransformFinalBlock(Encoding.ASCII.GetBytes(data), 0, data.Length);

            return StringUtils.GetBytesToHexString(res).ToLower();
        }
}

Related Tutorials