본문 바로가기
C#

C#] AES128, AES256 암호화 복호화 코드

by Fastlane 2020. 10. 16.
728x90
반응형

1. AES128 암호화, 복호화 코드 

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace COMMON.API
{
    public static class  HashHelper
    {
        #region Encrypt - AES128로 암호화

        public static string EncryptByAES128IncludedPassword(string text)
        {
            string result = HashHelper.EncryptByAES128(text, "비밀번호");
            return result;
        }

        public static string EncryptByAES128(string text, string password)
        {
            // 사전 설정
            UTF8Encoding ue = new UTF8Encoding();
            RijndaelManaged rijndael = new RijndaelManaged();
            rijndael.Padding = PaddingMode.PKCS7;
            rijndael.Mode = CipherMode.CBC;
            rijndael.KeySize = 128;

            // key 및 iv 설정
            byte[] pwdBytes = ue.GetBytes(password);
            byte[] keyBytes = new byte[16];
            byte[] IVBytes = new byte[16];
            int lenK = pwdBytes.Length;
            int lenIV = pwdBytes.Length;
            if (lenK > keyBytes.Length) { lenK = keyBytes.Length; }
            if (lenIV > IVBytes.Length) { lenIV = IVBytes.Length; }
            Array.Copy(pwdBytes, keyBytes, lenK);
            Array.Copy(pwdBytes, IVBytes, lenIV);
            rijndael.Key = keyBytes;
            rijndael.IV = IVBytes;

            byte[] message = ue.GetBytes(text);
            ICryptoTransform transform = rijndael.CreateEncryptor();
            // 암호화 수행 
            byte[] cipherBytes = transform.TransformFinalBlock(message, 0, message.Length);
            rijndael.Clear();

            // 16진수로 변환
            string hex = "";
            foreach (byte x in cipherBytes)
            {
                hex += x.ToString("x2");
            }
            return hex;
        }
        #endregion

        #region Decrypt - AES128로 복호화

        public static string DecryptByAES128IncludedPassword(string text)
        {
            string result = HashHelper.DecryptByAES128(text, "비밀번호");
            return result;
        }

        public static string DecryptByAES128(string text, string password)
        {
            // 사전 설정
            UTF8Encoding ue = new UTF8Encoding();
            RijndaelManaged rijndael = new RijndaelManaged();
            rijndael.Padding = PaddingMode.PKCS7;
            rijndael.Mode = CipherMode.CBC;
            rijndael.KeySize = 128;

            // 16진수 문자열을 바이트 배열로 변환
            byte[] cipherBytes = new byte[text.Length / 2];
            for (int i = 0; i < cipherBytes.Length; i++)
            {
                cipherBytes[i] = Convert.ToByte(text.Substring(i * 2, 2), 16);
            }

            // key 및 iv 설정
            byte[] pwdBytes = ue.GetBytes(password);
            byte[] keyBytes = new byte[16];
            byte[] IVBytes = new byte[16];
            int lenK = pwdBytes.Length;
            int lenIV = pwdBytes.Length;
            if (lenK > keyBytes.Length) { lenK = keyBytes.Length; }
            if (lenIV > IVBytes.Length) { lenIV = IVBytes.Length; }
            Array.Copy(pwdBytes, keyBytes, lenK);
            Array.Copy(pwdBytes, IVBytes, lenIV);
            rijndael.Key = keyBytes;
            rijndael.IV = IVBytes;

            ICryptoTransform transform = rijndael.CreateDecryptor();
            // 암호화 수행
            byte[] message = transform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
            rijndael.Clear();

            return Encoding.UTF8.GetString(message);
        }
        #endregion
    }
}

 

 

2. AES256 암호화, 복호화 코드 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace COMMON.API
{
    public static class  HashHelper
    {
        #region Encrypt - AES256로 암호화 

        public static string EncryptByAES256IncludedPassword(string text)
        {
            string result = HashHelper.EncryptByAES256(text, "비밀번호");
            return result;
        }

        public static string EncryptByAES256(string text, string password)
        {
            // 사전 설정
            UTF8Encoding ue = new UTF8Encoding();
            RijndaelManaged rijndael = new RijndaelManaged();
            rijndael.Padding = PaddingMode.PKCS7;
            rijndael.Mode = CipherMode.CBC;
            rijndael.KeySize = 256;

            // key 및 iv 설정
            byte[] pwdBytes = ue.GetBytes(password);
            byte[] keyBytes = new byte[32];
            byte[] IVBytes = new byte[16];
            int lenK = pwdBytes.Length;
            int lenIV = pwdBytes.Length;
            if (lenK > keyBytes.Length) { lenK = keyBytes.Length; }
            if (lenIV > IVBytes.Length) { lenIV = IVBytes.Length; }
            Array.Copy(pwdBytes, keyBytes, lenK);
            Array.Copy(pwdBytes, IVBytes, lenIV);
            rijndael.Key = keyBytes;
            rijndael.IV = IVBytes;

            byte[] message = ue.GetBytes(text);
            ICryptoTransform transform = rijndael.CreateEncryptor();
            // 암호화 수행 
            byte[] cipherBytes = transform.TransformFinalBlock(message, 0, message.Length);
            rijndael.Clear();

            // 16진수로 변환
            string hex = "";
            foreach (byte x in cipherBytes)
            {
                hex += x.ToString("x2");
            }
            return hex;
        }
        #endregion

        #region Decrypt - AES256로 복호화

        public static string DecryptByAES256IncludedPassword(string cryptotext)
        {
            string result = HashHelper.DecryptByAES256(cryptotext, "비밀번호");
            return result;
        }

        public static string DecryptByAES256(string cryptotext, string password)
        {
            // 사전 설정
            UTF8Encoding ue = new UTF8Encoding();
            RijndaelManaged rijndael = new RijndaelManaged();
            rijndael.Padding = PaddingMode.PKCS7;
            rijndael.Mode = CipherMode.CBC;
            rijndael.KeySize = 256;

            // 16진수 문자열을 바이트 배열로 변환
            byte[] cipherBytes = new byte[cryptotext.Length / 2];
            for (int i = 0; i < cipherBytes.Length; i++)
            {
                cipherBytes[i] = Convert.ToByte(cryptotext.Substring(i * 2, 2), 16);
            }

            // key 및 iv 설정
            byte[] pwdBytes = ue.GetBytes(password);
            byte[] keyBytes = new byte[32];
            byte[] IVBytes = new byte[16];
            int lenK = pwdBytes.Length;
            int lenIV = pwdBytes.Length;
            if (lenK > keyBytes.Length) { lenK = keyBytes.Length; }
            if (lenIV > IVBytes.Length) { lenIV = IVBytes.Length; }
            Array.Copy(pwdBytes, keyBytes, lenK);
            Array.Copy(pwdBytes, IVBytes, lenIV);
            rijndael.Key = keyBytes;
            rijndael.IV = IVBytes;

            ICryptoTransform transform = rijndael.CreateDecryptor();
            // 암호화 수행
            byte[] message = transform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
            rijndael.Clear();

            return Encoding.UTF8.GetString(message);
        }
        #endregion
    }
}
728x90
반응형

댓글