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);
변경 후
반응형
'메모장' 카테고리의 다른 글
스팀 클라우드 세이브 파일 덮어 씌우는 방법 (0) | 2025.07.01 |
---|---|
SteamPipeGUI를 이용한 Steamworks Build Upload (스팀웍스 빌드 업로드) (0) | 2025.06.15 |
[유니티 C#] 활성화 여부와 관계없이 모든 오브젝트를 찾는 헬퍼 메서드 (0) | 2024.07.14 |
DoPath 사용해보기 (0) | 2024.07.07 |
DOTween - SetEase 열거형 (0) | 2024.06.19 |