0
2

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暗号化

Last updated at Posted at 2022-07-21

QR決済に認証キーを保管する時に、予め暗号化することでセキュア面を強化する

    //初期化ベクトル
    private const string AES_IV = "rW$yZDcM?9L@pf6G";
    //暗号化鍵
    private const string AES_Key = "9%L4HP&eKWFYBi@4O2$yTvG!b]Kz9H&R";

    /// <summary>
    /// 対称鍵暗号を使って文字列を暗号化する
    /// </summary>
    /// <param name="text">暗号化する文字列</param>
    /// <returns>暗号化された文字列</returns>
    public static string Encrypt(string text)
    {
        // AES暗号化サービスプロバイダ
        AesCryptoServiceProvider aes = new AesCryptoServiceProvider();

        // ブロックサイズ(何文字単位で処理するか)
        aes.BlockSize = 128;
        // 鍵の長さ
        aes.KeySize = 256;
        // 暗号利用モード 
        aes.Mode = CipherMode.CBC;
        // パディング
        aes.Padding = PaddingMode.PKCS7;

        aes.IV = Encoding.UTF8.GetBytes(AES_IV);
        aes.Key = Encoding.UTF8.GetBytes(AES_Key);

        // 文字列をバイト型配列に変換
        byte[] src = Encoding.Unicode.GetBytes(text);

        // 暗号化する
        using (ICryptoTransform encrypt = aes.CreateEncryptor())
        {
            byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
            // Base64形式(64種類の英数字で表現)で返す
            return (Convert.ToBase64String(dest));
        }
    }

    /// <summary>
    /// 対称鍵暗号を使って暗号文を復号する
    /// </summary>
    /// <param name="cipher">暗号化された文字列</param>
    /// <returns>復号された文字列</returns>
    public static string Decrypt(string cipher)
    {
        string plain = string.Empty;

        try
        {
            if (!string.IsNullOrEmpty(cipher))
            {
                // AES暗号化サービスプロバイダ
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                aes.BlockSize = 128;
                aes.KeySize = 256;
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;
                aes.IV = Encoding.UTF8.GetBytes(AES_IV);
                aes.Key = Encoding.UTF8.GetBytes(AES_Key);

                // Base64形式の文字列からバイト型配列に変換
                byte[] src = Convert.FromBase64String(cipher);

                // 複号化する
                using (ICryptoTransform decrypt = aes.CreateDecryptor())
                {
                    byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length);
                    plain = Encoding.Unicode.GetString(dest);
                }
            }
        }
        catch (Exception)
        {
            //暗号化していない文字列が登録している場合に発生。空文字を設定する
            plain = "";
        }
        return plain;
    }

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?