0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C# - AES128で1ブロックだけ暗号化する

Last updated at Posted at 2022-01-31

概要

AES128で16byteのbyte配列を暗号化・復号するサンプルコードです。
※セキュアなコーディングとかは詳しくないので、正式なものに使うのに耐えれるかは保証できません。

ソースコード

AES128Sample.cs

using System;
using System.IO;
using System.Security.Cryptography;

class AES128Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        byte[] plain = new byte[]{0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
        byte[] key   = new byte[]{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};

        byte[] cipher;
        cipher = Encrypt1BlockAes128(plain, key);
        DumpArray(cipher);
        DumpArray(Decrypt1BlockAes128(cipher, key));
    }

    static void DumpArray(byte[] a)
    {
        for ( int i=0; i<a.Length; i++ ) {
            Console.Write(a[i].ToString("X02"));
        }
        Console.WriteLine();
    }

    static byte[] Decrypt1BlockAes128(byte[] cipher, byte[] key)
    {
        return EncDec1BlockAes128(cipher, key, false);
    }
    
    static byte[] Encrypt1BlockAes128(byte[] plain, byte[] key)
    {
        return EncDec1BlockAes128(plain, key, true);
    }

    static byte[] EncDec1BlockAes128(byte[] a, byte[] key, bool isEncryptMode)
    {
        const int N = 16; // 128bit = 16bytes

        if ( a == null || key == null ) {
            throw new ArgumentNullException();
        }
        if ( a.Length != N || key.Length != N ) {
            throw new ArgumentException();
        }

        byte[] buf = new byte[N];

        //using (AesManaged aes = new AesManaged()) // obsoleted
        using (Aes aes = Aes.Create())
        {
            aes.BlockSize = N*8;
            aes.Key = key;
            aes.IV = new byte[N]; // all zero. refer to https://docs.microsoft.com/ja-jp/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.None;
            ICryptoTransform encryptorOrDecryptor;
            if ( isEncryptMode ) {
                encryptorOrDecryptor = aes.CreateEncryptor(aes.Key, aes.IV);
            }
            else {
                encryptorOrDecryptor = aes.CreateDecryptor(aes.Key, aes.IV);
            }

            using (var ms = new MemoryStream(a))
            using (var cs = new CryptoStream(ms, encryptorOrDecryptor, CryptoStreamMode.Read))
            {
                cs.Read(buf, 0, N);
            }
        }
        return buf;
    }
}

検算

Test Vector的なやつ

FIPS-197 の C.1 AES-128 (Nk=4, Nr=10)

PLAINTEXT: 00112233445566778899aabbccddeeff
KEY: 000102030405060708090a0b0c0d0e0f
round[10].output 69c4e0d86a7b0430d8cdb78070b4c55a

参考サイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?