128 lines
4.5 KiB
C#
128 lines
4.5 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace LFP_Manager.Utils
|
|
{
|
|
public static class csCrypto
|
|
{
|
|
// AES-256 암호화 키 (32 bytes) 및 IV (16 bytes)
|
|
private static readonly byte[] Key = Encoding.UTF8.GetBytes("Amotech_LFP_Manager_Key_2026_TW!"); // 32 bytes
|
|
private static readonly byte[] IV = Encoding.UTF8.GetBytes("Amotech_LFP_IV26"); // 16 bytes
|
|
|
|
/// <summary>
|
|
/// 평문 문자열을 AES-256으로 암호화하여 Base64 문자열로 반환합니다.
|
|
/// </summary>
|
|
public static string Encrypt(string plainText)
|
|
{
|
|
if (string.IsNullOrEmpty(plainText))
|
|
return plainText;
|
|
|
|
// 이미 복호화 가능한 암호문인지 확인 (중복 암호화 방지)
|
|
if (IsEncrypted(plainText))
|
|
return plainText;
|
|
|
|
try
|
|
{
|
|
using (Aes aes = Aes.Create())
|
|
{
|
|
aes.Key = Key;
|
|
aes.IV = IV;
|
|
aes.Mode = CipherMode.CBC;
|
|
aes.Padding = PaddingMode.PKCS7;
|
|
|
|
using (MemoryStream ms = new MemoryStream())
|
|
{
|
|
using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
|
|
{
|
|
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
|
|
cs.Write(plainBytes, 0, plainBytes.Length);
|
|
cs.FlushFinalBlock();
|
|
}
|
|
return Convert.ToBase64String(ms.ToArray());
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// 암호화 실패 시 원본 반환
|
|
return plainText;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 암호화된 Base64 문자열을 복호화하여 평문으로 반환합니다.
|
|
/// 암호화되지 않은 기존 평문이 들어오면 그대로 반환하여 하위 호환성을 유지합니다.
|
|
/// </summary>
|
|
public static string Decrypt(string cipherText)
|
|
{
|
|
if (string.IsNullOrEmpty(cipherText))
|
|
return cipherText;
|
|
|
|
try
|
|
{
|
|
byte[] cipherBytes = Convert.FromBase64String(cipherText);
|
|
|
|
using (Aes aes = Aes.Create())
|
|
{
|
|
aes.Key = Key;
|
|
aes.IV = IV;
|
|
aes.Mode = CipherMode.CBC;
|
|
aes.Padding = PaddingMode.PKCS7;
|
|
|
|
using (MemoryStream ms = new MemoryStream())
|
|
{
|
|
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
|
|
{
|
|
cs.Write(cipherBytes, 0, cipherBytes.Length);
|
|
cs.FlushFinalBlock();
|
|
}
|
|
return Encoding.UTF8.GetString(ms.ToArray());
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// 복호화 실패 시(기존 평문인 경우 등) 원본 문자열 반환
|
|
return cipherText;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 해당 문자열이 현재 키로 복호화 가능한 암호문인지 확인합니다.
|
|
/// </summary>
|
|
public static bool IsEncrypted(string text)
|
|
{
|
|
if (string.IsNullOrEmpty(text)) return false;
|
|
try
|
|
{
|
|
byte[] cipherBytes = Convert.FromBase64String(text);
|
|
if (cipherBytes.Length == 0 || cipherBytes.Length % 16 != 0) return false;
|
|
|
|
using (Aes aes = Aes.Create())
|
|
{
|
|
aes.Key = Key;
|
|
aes.IV = IV;
|
|
aes.Mode = CipherMode.CBC;
|
|
aes.Padding = PaddingMode.PKCS7;
|
|
|
|
using (MemoryStream ms = new MemoryStream())
|
|
{
|
|
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
|
|
{
|
|
cs.Write(cipherBytes, 0, cipherBytes.Length);
|
|
cs.FlushFinalBlock();
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|