Aes
Aes.Create создаёт экземпляр
в using. По умолчанию
KeySize равен 256,
BlockSize - 128.
Ключ и IV
задают вручную - без
GenerateKey и GenerateIV.
Синтаксис
using Aes aes = Aes.Create();
aes.Key = key;
aes.IV = iv;
Пример
Давайте проверить размер ключа:
using System;
using System.Security.Cryptography;
class Program
{
static void Main()
{
using Aes aes = Aes.Create();
bool res = aes.KeySize == 256;
Console.WriteLine(res);
}
}
Результат выполнения кода:
True
Пример
Давайте проверить размер блока:
using System;
using System.Security.Cryptography;
class Program
{
static void Main()
{
using Aes aes = Aes.Create();
bool res = aes.BlockSize == 128;
Console.WriteLine(res);
}
}
Результат выполнения кода:
True
Пример
Давайте пройти круг шифрования и расшифровки:
using System;
using System.Security.Cryptography;
using System.Text;
class Program
{
static void Main()
{
byte[] key = Encoding.UTF8.GetBytes(
"12345678901234567890123456789012");
byte[] iv = Encoding.UTF8.GetBytes("1234567890123456");
byte[] plain = Encoding.UTF8.GetBytes("abcde");
using Aes aes = Aes.Create();
aes.Key = key;
aes.IV = iv;
byte[] cipher = aes.CreateEncryptor()
.TransformFinalBlock(plain, 0, plain.Length);
byte[] decoded = aes.CreateDecryptor()
.TransformFinalBlock(cipher, 0, cipher.Length);
string res = Encoding.UTF8.GetString(decoded);
Console.WriteLine(res);
}
}
Результат выполнения кода:
"abcde"
Смотрите также
-
Key,
ключ AES -
IV,
начальный вектор -
CreateEncryptor,
шифрование -
CryptoStream,
поток шифра