4
3

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#でAESを使用して文字列を暗号化、複合化する

Posted at

1. はじめに

  • C#で文字列をハッシュ化する関数を作りたい
  • Microsoft社のAESクラスを使用して暗号化、複合化したい

2. AESとは

  • 「Advanced Encryption Standard (AES)」の略でアメリカが2001年に標準暗号として定めた共通鍵暗号アルゴリズムである
  • AESはSPN構造のブロック暗号で、ブロック長は128ビット、鍵長は128ビット・192ビット・256ビットの3つが利用できる

3. サンプルコード

Utility.cs
using System.Security.Cryptography;
using System.Text;

namespace Utility
{
    /// <summary>
    /// 暗号化に関するユーティリティクラス
    /// </summary>
    public static class CryptographyUtil
    {

        // AES暗号化 key生成するための文字列 (256bitキー(32文字))
        private const string _aesKey = "12345678901234567890123456789012";
        // AES暗号化 初期化ベクトルを生成するための文字列 (128bit(16文字))
        private const string _aesIv = "1234567890123456";


        /// <summary>
        /// AESで暗号化する関数
        /// </summary>
        /// <param name="plain_text">暗号化する文字</param>
        /// <returns></returns>
        public static string AesEncrypt(string plain_text)
        {
            // 暗号化した文字列格納用
            string encrypted_str;

            // Aesオブジェクトを作成
            using (Aes aes = Aes.Create())
            {
                // Encryptorを作成
                using ICryptoTransform encryptor =
                    aes.CreateEncryptor(Encoding.UTF8.GetBytes(_aesKey), Encoding.UTF8.GetBytes(_aesIv));
                // 出力ストリームを作成
                using MemoryStream out_stream = new();
                // 暗号化して書き出す
                using (CryptoStream cs = new(out_stream, encryptor, CryptoStreamMode.Write))
                {
                    using StreamWriter sw = new(cs);
                    // 出力ストリームに書き出し
                    sw.Write(plain_text);
                }
                // Base64文字列にする
                byte[] result = out_stream.ToArray();
                encrypted_str = Convert.ToBase64String(result);
            }

            return encrypted_str;
        }


        /// <summary>
        /// AESで復号する関数
        /// </summary>
        /// <param name="base64_text">複合化する文字</param>
        /// <returns></returns>
        public static string AesDecrypt(string base64_text)
        {
            string plain_text;

            // Base64文字列をバイト型配列に変換
            byte[] cipher = Convert.FromBase64String(base64_text);

            // AESオブジェクトを作成
            using (Aes aes = Aes.Create())
            {
                // 復号器を作成
                using ICryptoTransform decryptor =
                    aes.CreateDecryptor(Encoding.UTF8.GetBytes(_aesKey), Encoding.UTF8.GetBytes(_aesIv));
                // 復号用ストリームを作成
                using MemoryStream in_stream = new(cipher);
                // 一気に復号
                using CryptoStream cs = new(in_stream, decryptor, CryptoStreamMode.Read);
                using StreamReader sr = new(cs);
                plain_text = sr.ReadToEnd();
            }
            return plain_text;
        }

    }
}

4. 参考文献

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?