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

public static class AesEncryptionUtil
{
    // 테스트용 하드코딩 키/IV 
    private static readonly byte[] _key = System.Text.Encoding.UTF8.GetBytes("키가 들어갈 자리");
    private static readonly byte[] _iv = System.Text.Encoding.UTF8.GetBytes("IV가 들어갈 자리");

    public static byte[] Encrypt(byte[] plainBytes)
    {
        if (plainBytes == null || plainBytes.Length == 0)
            throw new ArgumentException("암호화할 데이터가 없습니다.");

        using (var aes = Aes.Create())
        {
            aes.Key = _key;
            aes.IV = _iv;
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.PKCS7;

            using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
            using (var msEncrypt = new MemoryStream())
            using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                csEncrypt.Write(plainBytes, 0, plainBytes.Length);
                csEncrypt.FlushFinalBlock();
                return msEncrypt.ToArray();
            }
        }
    }

    public static byte[] Decrypt(byte[] cipherBytes)
    {
        if (cipherBytes == null || cipherBytes.Length == 0)
            throw new ArgumentException("복호화할 데이터가 없습니다.");

        using (var aes = Aes.Create())
        {
            aes.Key = _key;
            aes.IV = _iv;
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.PKCS7;

            using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
            using (var msDecrypt = new MemoryStream(cipherBytes))
            using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            using (var msPlain = new MemoryStream())
            {
                csDecrypt.CopyTo(msPlain);
                return msPlain.ToArray();
            }
        }
    }
}

암호화/복호화 유틸 클래스 코드

 

 

 

<암호화 예시 코드>

   var json = JsonConvert.SerializeObject(appInfo, Formatting.Indented);
   File.WriteAllText(GameConstants.APP_INFO_PATH, json);

변경 전

 

 

  var json = JsonConvert.SerializeObject(appInfo, Formatting.Indented);

   // JSON 문자열 → UTF8 바이트(평문)
   byte[] plainBytes = Encoding.UTF8.GetBytes(json);

   // AES 암호화 → 바이트 배열(cipherBytes)
   byte[] cipherBytes = AesEncryptionUtil.Encrypt(plainBytes);

   File.WriteAllBytes(GameConstants.APP_INFO_PATH, cipherBytes);

변경 후

 

 

 

 

 

<복호화 예시 코드>

   var gameInfoJson = File.ReadAllText(GameConstants.GAME_INFO_PATH);
   GameManager.Instance.GameInfo = JsonConvert.DeserializeObject<GameInfo>(gameInfoJson, settings);

변경 전 

 

   
   // 암호화된 바이트 읽기
  byte[]  cipherAppBytes = File.ReadAllBytes(GameConstants.GAME_INFO_PATH);

   // AES 복호화
   byte[] plainAppBytes = AesEncryptionUtil.Decrypt(cipherAppBytes);

   // UTF-8 문자열(JSON)로 변환
   var gameInfoJson = Encoding.UTF8.GetString(plainAppBytes);


   GameManager.Instance.GameInfo = JsonConvert.DeserializeObject<GameInfo>(gameInfoJson, settings);

변경 후

 

 

반응형

+ Recent posts