Ver3.0 ๐ C# ์ํธํ์ ํด์ฑ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ๋ฐ์ดํฐ ๋ณด์ ์๋ฒฝ ๊ตฌํํ๊ธฐ

๐ C# ์ํธํ์ ํด์ฑ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ๋ฐ์ดํฐ ๋ณด์ ์๋ฒฝ ๊ตฌํํ๊ธฐ
์น๊ตฌ์ฒ๋ผ ์ฝ๊ฒ ๋ฐฐ์ฐ๋ ์ค์ ๋ณด์ ํ๋ก๊ทธ๋๋ฐ
๐ฏ ์ ์ง๊ธ ๋ฐ์ดํฐ ๋ณด์์ด ์ค์ํ ๊น?
์ผ, ์์ฆ ๋ด์ค ๋ณด๋ฉด ๊ฐ์ธ์ ๋ณด ์ ์ถ ์ฌ๊ฑด์ด ์ผ๋ง๋ ๋ง์์ง ์์? ๐ฑ 2023๋
ํ ํด์๋ง ๊ตญ๋ด์์ ์๋ฐฑ๋ง ๊ฑด์ ๊ฐ์ธ์ ๋ณด๊ฐ ํดํน๋นํ๋ค๋ ํต๊ณ๊ฐ ์์ด. ๊ทธ๋์ ๊ฐ๋ฐ์๋ผ๋ฉด ์ด์ ๋ฐ์ดํฐ ๋ณด์์ ์ ํ์ด ์๋๋ผ ํ์์ผ!
ํนํ C#์ผ๋ก ๊ฐ๋ฐํ๋ ์น๊ตฌ๋ค์ ์ ๋ง ์ด์ด ์ข์. ์๋๊ณ ? .NET Framework์ .NET Core๊ฐ ์ ๊ณตํ๋ ์ํธํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์์ฒญ๋๊ฒ ๊ฐ๋ ฅํ๊ฑฐ๋ ! ๐ช ์ค๋์ ์ด ๊ฐ๋ ฅํ ๋๊ตฌ๋ค์ ์ด๋ป๊ฒ ์ค์ ์์ ํ์ฉํ๋์ง ์น๊ตฌํํ
์๊ธฐํ๋ฏ์ด ์ฝ๊ฒ ํ์ด๋ณผ๊ฒ.
๐ ์ํธํ vs ํด์ฑ, ๋ญ๊ฐ ๋ค๋ฅธ ๊ฑฐ์ผ?
๋ง์ ์ฌ๋๋ค์ด ์ํธํ์ ํด์ฑ์ ํท๊ฐ๋ ค ํด. ๋๋ ์ฒ์์ ๊ทธ๋ฌ์ด ๐
๊ทผ๋ฐ ์ด ๋์ ์ฐจ์ด๋ฅผ ์๋ฉด ์ธ์ ๋ญ ์จ์ผ ํ ์ง ๋ช
ํํด์ ธ.
์ํธํ(Encryption)๋ ์๋ฐฉํฅ์ด์ผ. ์ฆ, ์ํธํํ ๋ฐ์ดํฐ๋ฅผ ๋ค์ ์๋๋๋ก ๋ณตํธํํ ์ ์์ด. ๋ง์น ์๋ฌผ์ ๋ฅผ ์ ๊ทธ๊ณ ์ด์ ๋ก ๋ค์ ์ฌ๋ ๊ฒ์ฒ๋ผ ๋ง์ด์ผ. ํ์ผ์ ์ํธํํ๊ฑฐ๋, ๋คํธ์ํฌ๋ก ๋ฐ์ดํฐ๋ฅผ ์์ ํ๊ฒ ์ ์กํ ๋ ์ฌ์ฉํด.
ํด์ฑ(Hashing)์ ๋จ๋ฐฉํฅ์ด์ผ. ํ๋ฒ ํด์ํ๋ฉด ์ ๋ ์๋ ๋ฐ์ดํฐ๋ก ๋๋๋ฆด ์ ์์ด. ๋น๋ฐ๋ฒํธ ์ ์ฅํ ๋ ์ฃผ๋ก ์ฐ๋๋ฐ, ์๋ํ๋ฉด ์ฐ๋ฆฌ๋ ์ฌ์ฉ์์ ์ค์ ๋น๋ฐ๋ฒํธ๋ฅผ ์ ํ์๊ฐ ์๊ฑฐ๋ . ๋ก๊ทธ์ธํ ๋ ์
๋ ฅํ ๋น๋ฐ๋ฒํธ๋ฅผ ํด์ํด์ ์ ์ฅ๋ ํด์๊ฐ๊ณผ ๋น๊ต๋ง ํ๋ฉด ๋๋๊น!
๐ก๏ธ C#์์ ์ ๊ณตํ๋ ์ํธํ ์๊ณ ๋ฆฌ์ฆ๋ค
C#์ System.Security.Cryptography ๋ค์์คํ์ด์ค๋ ์ง์ง ๋ณด๋ฌผ์ฐฝ๊ณ ์ผ ๐ ์ฌ๊ธฐ์ ์ฐ๋ฆฌ๊ฐ ํ์ํ ๊ฑฐ์ ๋ชจ๋ ์ํธํ ๋๊ตฌ๊ฐ ๋ค์ด์๊ฑฐ๋ . ํ๋์ฉ ์ดํด๋ณด์!
๐ ๋์นญํค ์ํธํ (Symmetric Encryption)
๋์นญํค ์ํธํ๋ ์ํธํํ ๋ ์ฐ๋ ํค์ ๋ณตํธํํ ๋ ์ฐ๋ ํค๊ฐ ๊ฐ์. ๋ง์น ์ง ์ด์ ํ๋๋ก ๋ฌธ์ ์ ๊ทธ๊ณ ์ฌ๋ ๊ฒ์ฒ๋ผ ๋ง์ด์ผ. ์๋๊ฐ ๋น ๋ฅด๊ณ ํจ์จ์ ์ด๋ผ์ ๋์ฉ๋ ๋ฐ์ดํฐ ์ํธํ์ ์ ํฉํด.
C#์์ ์ฃผ๋ก ์ฌ์ฉํ๋ ๋์นญํค ์๊ณ ๋ฆฌ์ฆ์ ๋ค์๊ณผ ๊ฐ์:
๐ ๋น๋์นญํค ์ํธํ (Asymmetric Encryption)
๋น๋์นญํค๋ ๊ณต๊ฐํค(Public Key)์ ๊ฐ์ธํค(Private Key) ๋ ๊ฐ๋ฅผ ์ฌ์ฉํด. ๊ณต๊ฐํค๋ก ์ํธํํ๋ฉด ๊ฐ์ธํค๋ก๋ง ๋ณตํธํํ ์ ์๊ณ , ๊ทธ ๋ฐ๋๋ ๊ฐ๋ฅํด. ๋์งํธ ์๋ช
์ด๋ ํค ๊ตํ์ ์ฃผ๋ก ์ฌ์ฉํ์ง.
๋ํ์ ์ธ ์๊ณ ๋ฆฌ์ฆ์:
๐จ ์ค์ ! AES ์ํธํ ๊ตฌํํ๊ธฐ
์, ์ด์ ์ด๋ก ์ ์ถฉ๋ถํ ๋ดค์ผ๋ ์ค์ ์ฝ๋๋ฅผ ์์ฑํด๋ณด์! AES ์ํธํ๋ฅผ ๊ตฌํํ๋ ์์ ํ ์์ ๋ฅผ ๋ณด์ฌ์ค๊ฒ. ์ด ์ฝ๋๋ ์ค์ ํ๋ก์ ํธ์ ๋ฐ๋ก ์ ์ฉํ ์ ์์ ์ ๋๋ก ์์ฑ๋ ์๊ฒ ๋ง๋ค์์ด ๐ป
๋ฌธ์์ด์ AES-256์ผ๋ก ์ํธํํ๊ณ ๋ณตํธํํ๋ ํด๋์ค๋ฅผ ๋ง๋ค ๊ฑฐ์ผ. ํค์ IV(Initialization Vector)๋ฅผ ์์ ํ๊ฒ ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ๋ ํฌํจํ ๊ฒ!
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace DataSecurityExample
{
public class AesEncryption
{
// AES-256์ ์ฌ์ฉํ๋ฏ๋ก 32๋ฐ์ดํธ(256๋นํธ) ํค๊ฐ ํ์ํด
private static readonly int KeySize = 256;
private static readonly int BlockSize = 128;
/// <summary>
/// ๋ฌธ์์ด์ AES๋ก ์ํธํํ๋ ๋ฉ์๋
/// </summary>
/// <param name="plainText">์ํธํํ ํ๋ฌธ</param>
/// <param name="key">์ํธํ ํค (32๋ฐ์ดํธ)</param>
/// <param name="iv">์ด๊ธฐํ ๋ฒกํฐ (16๋ฐ์ดํธ)</param>
/// <returns>Base64๋ก ์ธ์ฝ๋ฉ๋ ์ํธ๋ฌธ</returns>
public static string Encrypt(string plainText, byte[] key, byte[] iv)
{
// ์
๋ ฅ๊ฐ ๊ฒ์ฆ - ์ค์ ์์ ๊ผญ ํ์ํด!
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException(nameof(plainText));
if (key == null || key.Length != 32)
throw new ArgumentException("ํค๋ 32๋ฐ์ดํธ์ฌ์ผ ํฉ๋๋ค.", nameof(key));
if (iv == null || iv.Length != 16)
throw new ArgumentException("IV๋ 16๋ฐ์ดํธ์ฌ์ผ ํฉ๋๋ค.", nameof(iv));
byte[] encrypted;
// AES ๊ฐ์ฒด ์์ฑ - using ๋ฌธ์ผ๋ก ์๋ ๋ฆฌ์์ค ํด์
using (Aes aes = Aes.Create())
{
aes.KeySize = KeySize;
aes.BlockSize = BlockSize;
aes.Key = key;
aes.IV = iv;
aes.Mode = CipherMode.CBC; // CBC ๋ชจ๋ ์ฌ์ฉ
aes.Padding = PaddingMode.PKCS7; // PKCS7 ํจ๋ฉ
// ์ํธํ ๋ณํ๊ธฐ ์์ฑ
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
// ๋ฉ๋ชจ๋ฆฌ ์คํธ๋ฆผ์ผ๋ก ์ํธํ ์ํ
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
// ํ๋ฌธ์ ์คํธ๋ฆผ์ ์ฐ๋ฉด ์๋์ผ๋ก ์ํธํ๋ผ
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Base64๋ก ์ธ์ฝ๋ฉํด์ ๋ฌธ์์ด๋ก ๋ฐํ
return Convert.ToBase64String(encrypted);
}
/// <summary>
/// AES๋ก ์ํธํ๋ ๋ฌธ์์ด์ ๋ณตํธํํ๋ ๋ฉ์๋
/// </summary>
/// <param name="cipherText">Base64๋ก ์ธ์ฝ๋ฉ๋ ์ํธ๋ฌธ</param>
/// <param name="key">๋ณตํธํ ํค (32๋ฐ์ดํธ)</param>
/// <param name="iv">์ด๊ธฐํ ๋ฒกํฐ (16๋ฐ์ดํธ)</param>
/// <returns>๋ณตํธํ๋ ํ๋ฌธ</returns>
public static string Decrypt(string cipherText, byte[] key, byte[] iv)
{
// ์
๋ ฅ๊ฐ ๊ฒ์ฆ
if (string.IsNullOrEmpty(cipherText))
throw new ArgumentNullException(nameof(cipherText));
if (key == null || key.Length != 32)
throw new ArgumentException("ํค๋ 32๋ฐ์ดํธ์ฌ์ผ ํฉ๋๋ค.", nameof(key));
if (iv == null || iv.Length != 16)
throw new ArgumentException("IV๋ 16๋ฐ์ดํธ์ฌ์ผ ํฉ๋๋ค.", nameof(iv));
string plaintext = null;
// Base64 ๋์ฝ๋ฉ
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes aes = Aes.Create())
{
aes.KeySize = KeySize;
aes.BlockSize = BlockSize;
aes.Key = key;
aes.IV = iv;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
// ๋ณตํธํ ๋ณํ๊ธฐ ์์ฑ
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherBytes))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// ์คํธ๋ฆผ์์ ์ฝ์ผ๋ฉด ์๋์ผ๋ก ๋ณตํธํ๋ผ
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
/// <summary>
/// ์์ ํ ๋๋ค ํค ์์ฑ
/// </summary>
/// <returns>32๋ฐ์ดํธ ๋๋ค ํค</returns>
public static byte[] GenerateRandomKey()
{
using (Aes aes = Aes.Create())
{
aes.KeySize = KeySize;
aes.GenerateKey();
return aes.Key;
}
}
/// <summary>
/// ์์ ํ ๋๋ค IV ์์ฑ
/// </summary>
/// <returns>16๋ฐ์ดํธ ๋๋ค IV</returns>
public static byte[] GenerateRandomIV()
{
using (Aes aes = Aes.Create())
{
aes.GenerateIV();
return aes.IV;
}
}
}
}
์ด์ ์ด ํด๋์ค๋ฅผ ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง ๋ณด์:
class Program
{
static void Main(string[] args)
{
try
{
// 1. ํค์ IV ์์ฑ (์ค์ ์์๋ ์์ ํ ๊ณณ์ ์ ์ฅํด์ผ ํด!)
byte[] key = AesEncryption.GenerateRandomKey();
byte[] iv = AesEncryption.GenerateRandomIV();
Console.WriteLine("=== AES ์ํธํ ํ
์คํธ ===\n");
// 2. ์ํธํํ ๋ฐ์ดํฐ
string originalText = "์๋
ํ์ธ์! ์ด๊ฒ์ ๋น๋ฐ ๋ฉ์์ง์
๋๋ค. ๐";
Console.WriteLine($"์๋ณธ ํ
์คํธ: {originalText}");
// 3. ์ํธํ
string encryptedText = AesEncryption.Encrypt(originalText, key, iv);
Console.WriteLine($"\n์ํธํ๋ ํ
์คํธ: {encryptedText}");
// 4. ๋ณตํธํ
string decryptedText = AesEncryption.Decrypt(encryptedText, key, iv);
Console.WriteLine($"\n๋ณตํธํ๋ ํ
์คํธ: {decryptedText}");
// 5. ๊ฒ์ฆ
Console.WriteLine($"\n์๋ณธ๊ณผ ๋ณตํธํ ๊ฒฐ๊ณผ ์ผ์น: {originalText == decryptedText}");
// ํค์ IV๋ฅผ Base64๋ก ์ถ๋ ฅ (์ ์ฅ ๋ชฉ์ )
Console.WriteLine($"\n์์ฑ๋ ํค (Base64): {Convert.ToBase64String(key)}");
Console.WriteLine($"์์ฑ๋ IV (Base64): {Convert.ToBase64String(iv)}");
}
catch (Exception ex)
{
Console.WriteLine($"์ค๋ฅ ๋ฐ์: {ex.Message}");
}
Console.ReadLine();
}
}
2. IV๋ ๋งค๋ฒ ๋ค๋ฅด๊ฒ: ๊ฐ์ ํค๋ฅผ ์ฌ์ฉํ๋๋ผ๋ IV๋ ๋งค๋ฒ ์๋ก ์์ฑํด์ผ ํด. ๊ทธ๋์ผ ๊ฐ์ ํ๋ฌธ์ด๋ผ๋ ๋ค๋ฅธ ์ํธ๋ฌธ์ด ๋์.
3. ์ ์ก ์ ์ฃผ์: IV๋ ์ํธ๋ฌธ๊ณผ ํจ๊ป ์ ์กํด๋ ๋์ง๋ง, ํค๋ ์ ๋ ๊ฐ์ด ๋ณด๋ด๋ฉด ์ ๋ผ!
๐ ์ค์ ! ๋น๋ฐ๋ฒํธ ํด์ฑ ๊ตฌํํ๊ธฐ
๋น๋ฐ๋ฒํธ๋ ์ ๋ ํ๋ฌธ์ผ๋ก ์ ์ฅํ๋ฉด ์ ๋ผ! ๐ซ ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ํดํน๋นํ๋ฉด ๋ชจ๋ ์ฌ์ฉ์์ ๋น๋ฐ๋ฒํธ๊ฐ ๋
ธ์ถ๋๊ฑฐ๋ . ๊ทธ๋์ ์ฐ๋ฆฌ๋ ํด์ฑ์ ์ฌ์ฉํด์ผ ํด.
๊ทผ๋ฐ ๋จ์ ํด์ฑ๋ง์ผ๋ก๋ ๋ถ์กฑํด. Rainbow Table ๊ณต๊ฒฉ์ด๋ Dictionary ๊ณต๊ฒฉ์ ์ทจ์ฝํ๊ฑฐ๋ . ๊ทธ๋์ Salt๋ฅผ ์ถ๊ฐํ๊ณ , ๊ณ์ฐ์ ์ฌ๋ฌ ๋ฒ ๋ฐ๋ณตํ๋ ๋ฐฉ๋ฒ์ ์จ์ผ ํด!
๐ง Salt๊ฐ ๋ญ์ผ?
Salt๋ ๋น๋ฐ๋ฒํธ์ ์ถ๊ฐํ๋ ๋๋ค ๋ฐ์ดํฐ์ผ. ๊ฐ์ ๋น๋ฐ๋ฒํธ๋ผ๋ Salt๊ฐ ๋ค๋ฅด๋ฉด ์์ ํ ๋ค๋ฅธ ํด์๊ฐ์ด ๋์. ์ด๋ ๊ฒ ํ๋ฉด ๋ฏธ๋ฆฌ ๊ณ์ฐ๋ ํด์ ํ ์ด๋ธ(Rainbow Table)๋ก ๊ณต๊ฒฉํ๋ ๊ฒ ๋ถ๊ฐ๋ฅํด์ ธ ๐ฏ
๐ช PBKDF2๋ก ์์ ํ๊ฒ ๊ตฌํํ๊ธฐ
PBKDF2(Password-Based Key Derivation Function 2)๋ ๋น๋ฐ๋ฒํธ ํด์ฑ์ ํ์ค์ด์ผ. ํด์ ํจ์๋ฅผ ์์ฒ ๋ฒ ๋ฐ๋ณตํด์ ๋ฌด์ฐจ๋ณ ๋์ ๊ณต๊ฒฉ์ ์ด๋ ต๊ฒ ๋ง๋ค์ด. C#์์๋ Rfc2898DeriveBytes ํด๋์ค๋ก ์ฝ๊ฒ ๊ตฌํํ ์ ์์ด!
using System;
using System.Security.Cryptography;
using System.Text;
namespace DataSecurityExample
{
public class PasswordHasher
{
// ๋ณด์ ์ค์ ๊ฐ๋ค
private const int SaltSize = 32; // Salt ํฌ๊ธฐ (๋ฐ์ดํธ)
private const int HashSize = 32; // ํด์ ํฌ๊ธฐ (๋ฐ์ดํธ)
private const int Iterations = 100000; // ๋ฐ๋ณต ํ์ (๋์์๋ก ์์ ํ์ง๋ง ๋๋ ค์ ธ)
/// <summary>
/// ๋น๋ฐ๋ฒํธ๋ฅผ ํด์ํํ๋ ๋ฉ์๋
/// </summary>
/// <param name="password">ํด์ํ ๋น๋ฐ๋ฒํธ</param>
/// <returns>Salt์ ํด์๊ฐ ๊ฒฐํฉ๋ ๋ฌธ์์ด</returns>
public static string HashPassword(string password)
{
if (string.IsNullOrEmpty(password))
throw new ArgumentNullException(nameof(password));
// 1. ๋๋ค Salt ์์ฑ
byte[] salt = new byte[SaltSize];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(salt);
}
// 2. PBKDF2๋ก ํด์ ์์ฑ
byte[] hash = GenerateHash(password, salt, Iterations, HashSize);
// 3. Salt์ ํด์๋ฅผ ๊ฒฐํฉํด์ ์ ์ฅ
// ํ์: {iterations}.{salt}.{hash} (๋ชจ๋ Base64 ์ธ์ฝ๋ฉ)
string result = $"{Iterations}.{Convert.ToBase64String(salt)}.{Convert.ToBase64String(hash)}";
return result;
}
/// <summary>
/// ์
๋ ฅ๋ ๋น๋ฐ๋ฒํธ๊ฐ ์ ์ฅ๋ ํด์์ ์ผ์นํ๋์ง ๊ฒ์ฆ
/// </summary>
/// <param name="password">๊ฒ์ฆํ ๋น๋ฐ๋ฒํธ</param>
/// <param name="hashedPassword">์ ์ฅ๋ ํด์ ๋ฌธ์์ด</param>
/// <returns>์ผ์น ์ฌ๋ถ</returns>
public static bool VerifyPassword(string password, string hashedPassword)
{
if (string.IsNullOrEmpty(password))
throw new ArgumentNullException(nameof(password));
if (string.IsNullOrEmpty(hashedPassword))
throw new ArgumentNullException(nameof(hashedPassword));
try
{
// 1. ์ ์ฅ๋ ํด์ ๋ฌธ์์ด ํ์ฑ
string[] parts = hashedPassword.Split('.');
if (parts.Length != 3)
return false;
int iterations = int.Parse(parts[0]);
byte[] salt = Convert.FromBase64String(parts[1]);
byte[] originalHash = Convert.FromBase64String(parts[2]);
// 2. ์
๋ ฅ๋ ๋น๋ฐ๋ฒํธ๋ก ํด์ ์์ฑ
byte[] testHash = GenerateHash(password, salt, iterations, originalHash.Length);
// 3. ๋ ํด์๋ฅผ ์์ ํ๊ฒ ๋น๊ต (ํ์ด๋ฐ ๊ณต๊ฒฉ ๋ฐฉ์ง)
return SlowEquals(originalHash, testHash);
}
catch
{
return false;
}
}
/// <summary>
/// PBKDF2๋ฅผ ์ฌ์ฉํด์ ํด์ ์์ฑ
/// </summary>
private static byte[] GenerateHash(string password, byte[] salt, int iterations, int hashSize)
{
using (var pbkdf2 = new Rfc2898DeriveBytes(password, salt, iterations, HashAlgorithmName.SHA256))
{
return pbkdf2.GetBytes(hashSize);
}
}
/// <summary>
/// ํ์ด๋ฐ ๊ณต๊ฒฉ์ ๋ฐฉ์งํ๋ ์์ ํ ๋น๊ต ๋ฉ์๋
/// </summary>
private static bool SlowEquals(byte[] a, byte[] b)
{
// ๊ธธ์ด๊ฐ ๋ค๋ฅด๋ฉด false
if (a.Length != b.Length)
return false;
// ๋ชจ๋ ๋ฐ์ดํธ๋ฅผ ๋น๊ต (์ค๊ฐ์ ๋ฉ์ถ์ง ์์)
uint diff = 0;
for (int i = 0; i < a.Length; i++)
{
diff |= (uint)(a[i] ^ b[i]);
}
return diff == 0;
}
/// <summary>
/// ๋น๋ฐ๋ฒํธ ๊ฐ๋ ๊ฒ์ฌ (๋ณด๋์ค ๊ธฐ๋ฅ!)
/// </summary>
public static PasswordStrength CheckPasswordStrength(string password)
{
if (string.IsNullOrEmpty(password))
return PasswordStrength.VeryWeak;
int score = 0;
// ๊ธธ์ด ์ฒดํฌ
if (password.Length >= 8) score++;
if (password.Length >= 12) score++;
if (password.Length >= 16) score++;
// ๋ฌธ์ ์ข
๋ฅ ์ฒดํฌ
if (password.Any(char.IsLower)) score++;
if (password.Any(char.IsUpper)) score++;
if (password.Any(char.IsDigit)) score++;
if (password.Any(c => !char.IsLetterOrDigit(c))) score++;
// ์ ์์ ๋ฐ๋ผ ๊ฐ๋ ๋ฐํ
if (score <= 2) return PasswordStrength.VeryWeak;
if (score <= 4) return PasswordStrength.Weak;
if (score <= 6) return PasswordStrength.Medium;
if (score <= 8) return PasswordStrength.Strong;
return PasswordStrength.VeryStrong;
}
}
public enum PasswordStrength
{
VeryWeak,
Weak,
Medium,
Strong,
VeryStrong
}
}
์ฌ์ฉ ์์ ๋ฅผ ๋ณด์:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== ๋น๋ฐ๋ฒํธ ํด์ฑ ํ
์คํธ ===\n");
// 1. ํ์๊ฐ์
์๋๋ฆฌ์ค
string userPassword = "MySecureP@ssw0rd!";
// ๋น๋ฐ๋ฒํธ ๊ฐ๋ ์ฒดํฌ
var strength = PasswordHasher.CheckPasswordStrength(userPassword);
Console.WriteLine($"๋น๋ฐ๋ฒํธ ๊ฐ๋: {strength}");
// ๋น๋ฐ๋ฒํธ ํด์ํ
string hashedPassword = PasswordHasher.HashPassword(userPassword);
Console.WriteLine($"\nํด์๋ ๋น๋ฐ๋ฒํธ:\n{hashedPassword}");
// 2. ๋ก๊ทธ์ธ ์๋๋ฆฌ์ค - ์ฌ๋ฐ๋ฅธ ๋น๋ฐ๋ฒํธ
Console.WriteLine("\n--- ๋ก๊ทธ์ธ ํ
์คํธ (์ฌ๋ฐ๋ฅธ ๋น๋ฐ๋ฒํธ) ---");
bool isValid = PasswordHasher.VerifyPassword(userPassword, hashedPassword);
Console.WriteLine($"์ธ์ฆ ๊ฒฐ๊ณผ: {(isValid ? "โ
์ฑ๊ณต" : "โ ์คํจ")}");
// 3. ๋ก๊ทธ์ธ ์๋๋ฆฌ์ค - ์๋ชป๋ ๋น๋ฐ๋ฒํธ
Console.WriteLine("\n--- ๋ก๊ทธ์ธ ํ
์คํธ (์๋ชป๋ ๋น๋ฐ๋ฒํธ) ---");
bool isInvalid = PasswordHasher.VerifyPassword("WrongPassword", hashedPassword);
Console.WriteLine($"์ธ์ฆ ๊ฒฐ๊ณผ: {(isInvalid ? "โ
์ฑ๊ณต" : "โ ์คํจ")}");
// 4. ๊ฐ์ ๋น๋ฐ๋ฒํธ๋ ๋งค๋ฒ ๋ค๋ฅธ ํด์๊ฐ ์์ฑ ํ์ธ
Console.WriteLine("\n--- ๊ฐ์ ๋น๋ฐ๋ฒํธ์ ๋ค๋ฅธ ํด์๊ฐ ---");
string hash1 = PasswordHasher.HashPassword(userPassword);
string hash2 = PasswordHasher.HashPassword(userPassword);
Console.WriteLine($"ํด์1: {hash1.Substring(0, 50)}...");
Console.WriteLine($"ํด์2: {hash2.Substring(0, 50)}...");
Console.WriteLine($"๋ ํด์๊ฐ ๋ค๋ฆ: {hash1 != hash2}");
Console.ReadLine();
}
}
๐ SHA-256๊ณผ SHA-512 ํด์ฑ
๋น๋ฐ๋ฒํธ ์ธ์๋ ํ์ผ ๋ฌด๊ฒฐ์ฑ ๊ฒ์ฆ, ๋ฐ์ดํฐ ์ฒดํฌ์ฌ ๋ฑ์๋ SHA(Secure Hash Algorithm) ๊ณ์ด์ ๋ง์ด ์จ. SHA-256๊ณผ SHA-512๊ฐ ๊ฐ์ฅ ๋๋ฆฌ ์ฌ์ฉ๋๋๋ฐ, ๊ฐ๊ฐ 256๋นํธ์ 512๋นํธ ํด์๊ฐ์ ์์ฑํด ๐ฒ
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace DataSecurityExample
{
public class HashingUtility
{
/// <summary>
/// SHA-256์ผ๋ก ๋ฌธ์์ด ํด์ฑ
/// </summary>
public static string ComputeSHA256(string input)
{
if (string.IsNullOrEmpty(input))
throw new ArgumentNullException(nameof(input));
using (SHA256 sha256 = SHA256.Create())
{
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] hashBytes = sha256.ComputeHash(inputBytes);
// ๋ฐ์ดํธ ๋ฐฐ์ด์ 16์ง์ ๋ฌธ์์ด๋ก ๋ณํ
return ByteArrayToHexString(hashBytes);
}
}
/// <summary>
/// SHA-512๋ก ๋ฌธ์์ด ํด์ฑ
/// </summary>
public static string ComputeSHA512(string input)
{
if (string.IsNullOrEmpty(input))
throw new ArgumentNullException(nameof(input));
using (SHA512 sha512 = SHA512.Create())
{
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] hashBytes = sha512.ComputeHash(inputBytes);
return ByteArrayToHexString(hashBytes);
}
}
/// <summary>
/// ํ์ผ์ SHA-256 ํด์ ๊ณ์ฐ (๋ฌด๊ฒฐ์ฑ ๊ฒ์ฆ์ฉ)
/// </summary>
public static string ComputeFileSHA256(string filePath)
{
if (!File.Exists(filePath))
throw new FileNotFoundException("ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค.", filePath);
using (SHA256 sha256 = SHA256.Create())
using (FileStream stream = File.OpenRead(filePath))
{
byte[] hashBytes = sha256.ComputeHash(stream);
return ByteArrayToHexString(hashBytes);
}
}
/// <summary>
/// HMAC-SHA256 (ํค ๊ธฐ๋ฐ ํด์)
/// API ์๋ช
์ด๋ ๋ฉ์์ง ์ธ์ฆ์ ์ฌ์ฉ
/// </summary>
public static string ComputeHMACSHA256(string message, string secret)
{
if (string.IsNullOrEmpty(message))
throw new ArgumentNullException(nameof(message));
if (string.IsNullOrEmpty(secret))
throw new ArgumentNullException(nameof(secret));
byte[] secretBytes = Encoding.UTF8.GetBytes(secret);
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
using (HMACSHA256 hmac = new HMACSHA256(secretBytes))
{
byte[] hashBytes = hmac.ComputeHash(messageBytes);
return ByteArrayToHexString(hashBytes);
}
}
/// <summary>
/// ๋ฐ์ดํธ ๋ฐฐ์ด์ 16์ง์ ๋ฌธ์์ด๋ก ๋ณํ
/// </summary>
private static string ByteArrayToHexString(byte[] bytes)
{
StringBuilder sb = new StringBuilder(bytes.Length * 2);
foreach (byte b in bytes)
{
sb.AppendFormat("{0:x2}", b);
}
return sb.ToString();
}
/// <summary>
/// ๋ ํด์๊ฐ์ด ๊ฐ์์ง ๋น๊ต
/// </summary>
public static bool CompareHashes(string hash1, string hash2)
{
if (string.IsNullOrEmpty(hash1) || string.IsNullOrEmpty(hash2))
return false;
// ๋์๋ฌธ์ ๊ตฌ๋ถ ์์ด ๋น๊ต
return string.Equals(hash1, hash2, StringComparison.OrdinalIgnoreCase);
}
}
}
์ค์ ํ์ฉ ์์ :
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== SHA ํด์ฑ ํ
์คํธ ===\n");
string testData = "Hello, Secure World!";
// 1. SHA-256 ํด์ฑ
string sha256Hash = HashingUtility.ComputeSHA256(testData);
Console.WriteLine($"์๋ณธ ๋ฐ์ดํฐ: {testData}");
Console.WriteLine($"SHA-256: {sha256Hash}");
// 2. SHA-512 ํด์ฑ
string sha512Hash = HashingUtility.ComputeSHA512(testData);
Console.WriteLine($"\nSHA-512: {sha512Hash}");
// 3. ํ์ผ ๋ฌด๊ฒฐ์ฑ ๊ฒ์ฆ
Console.WriteLine("\n--- ํ์ผ ๋ฌด๊ฒฐ์ฑ ๊ฒ์ฆ ---");
string filePath = "test.txt";
File.WriteAllText(filePath, "์ด๊ฒ์ ํ
์คํธ ํ์ผ์
๋๋ค.");
string fileHash1 = HashingUtility.ComputeFileSHA256(filePath);
Console.WriteLine($"ํ์ผ ํด์: {fileHash1}");
// ํ์ผ ์์
File.AppendAllText(filePath, " ์์ ๋จ!");
string fileHash2 = HashingUtility.ComputeFileSHA256(filePath);
Console.WriteLine($"์์ ํ ํด์: {fileHash2}");
Console.WriteLine($"ํ์ผ ๋ณ๊ฒฝ ๊ฐ์ง: {!HashingUtility.CompareHashes(fileHash1, fileHash2)}");
// 4. HMAC-SHA256 (API ์๋ช
)
Console.WriteLine("\n--- API ์๋ช
์์ ---");
string apiMessage = "GET /api/users?id=123";
string apiSecret = "my-secret-key-12345";
string signature = HashingUtility.ComputeHMACSHA256(apiMessage, apiSecret);
Console.WriteLine($"API ์์ฒญ: {apiMessage}");
Console.WriteLine($"์๋ช
: {signature}");
// ์ ๋ฆฌ
File.Delete(filePath);
Console.ReadLine();
}
}
๐ RSA ๋น๋์นญํค ์ํธํ ๊ตฌํ
์, ์ด์ ๋น๋์นญํค ์ํธํ๋ฅผ ์์๋ณผ ์ฐจ๋ก์ผ! RSA๋ ๊ณต๊ฐํค์ ๊ฐ์ธํค๋ฅผ ์ฌ์ฉํด์ ์์ ํ๊ฒ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ์ ์๊ฒ ํด์ค. ํนํ ํค ๊ตํ์ด๋ ๋์งํธ ์๋ช ์ ์๋ฒฝํด ๐
2. ๊ฐ์ธํค๋ก ์๋ช ํ๋ฉด โ ๊ณต๊ฐํค๋ก ๊ฒ์ฆ ๊ฐ๋ฅ (๋์งํธ ์๋ช )
์ด ๋ ๊ฐ์ง ํน์ฑ ๋๋ถ์ RSA๋ ์์ ํ ํต์ ์ ํต์ฌ์ด ๋ผ!
using System;
using System.Security.Cryptography;
using System.Text;
namespace DataSecurityExample
{
public class RsaEncryption
{
// RSA ํค ํฌ๊ธฐ (๋นํธ)
private const int KeySize = 2048; // ์ต์ 2048๋นํธ ๊ถ์ฅ!
/// <summary>
/// RSA ํค ์ ์์ฑ
/// </summary>
/// <param name="publicKey">์์ฑ๋ ๊ณต๊ฐํค (XML ํ์)</param>
/// <param name="privateKey">์์ฑ๋ ๊ฐ์ธํค (XML ํ์)</param>
public static void GenerateKeys(out string publicKey, out string privateKey)
{
using (RSA rsa = RSA.Create(KeySize))
{
// ๊ณต๊ฐํค ์ถ์ถ (๊ณต๊ฐํด๋ ์์ )
publicKey = rsa.ToXmlString(false);
// ๊ฐ์ธํค ์ถ์ถ (์ ๋ ๊ณต๊ฐํ๋ฉด ์ ๋จ!)
privateKey = rsa.ToXmlString(true);
}
}
/// <summary>
/// ๊ณต๊ฐํค๋ก ๋ฐ์ดํฐ ์ํธํ
/// </summary>
/// <param name="plainText">์ํธํํ ํ๋ฌธ</param>
/// <param name="publicKey">๊ณต๊ฐํค (XML ํ์)</param>
/// <returns>Base64๋ก ์ธ์ฝ๋ฉ๋ ์ํธ๋ฌธ</returns>
public static string Encrypt(string plainText, string publicKey)
{
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException(nameof(plainText));
if (string.IsNullOrEmpty(publicKey))
throw new ArgumentNullException(nameof(publicKey));
using (RSA rsa = RSA.Create())
{
// ๊ณต๊ฐํค ๋ก๋
rsa.FromXmlString(publicKey);
// ํ๋ฌธ์ ๋ฐ์ดํธ๋ก ๋ณํ
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
// RSA ์ํธํ (OAEP ํจ๋ฉ ์ฌ์ฉ - ๋ ์์ ํจ)
byte[] encryptedBytes = rsa.Encrypt(plainBytes, RSAEncryptionPadding.OaepSHA256);
// Base64๋ก ์ธ์ฝ๋ฉ
return Convert.ToBase64String(encryptedBytes);
}
}
/// <summary>
/// ๊ฐ์ธํค๋ก ๋ฐ์ดํฐ ๋ณตํธํ
/// </summary>
/// <param name="cipherText">Base64๋ก ์ธ์ฝ๋ฉ๋ ์ํธ๋ฌธ</param>
/// <param name="privateKey">๊ฐ์ธํค (XML ํ์)</param>
/// <returns>๋ณตํธํ๋ ํ๋ฌธ</returns>
public static string Decrypt(string cipherText, string privateKey)
{
if (string.IsNullOrEmpty(cipherText))
throw new ArgumentNullException(nameof(cipherText));
if (string.IsNullOrEmpty(privateKey))
throw new ArgumentNullException(nameof(privateKey));
using (RSA rsa = RSA.Create())
{
// ๊ฐ์ธํค ๋ก๋
rsa.FromXmlString(privateKey);
// Base64 ๋์ฝ๋ฉ
byte[] encryptedBytes = Convert.FromBase64String(cipherText);
// RSA ๋ณตํธํ
byte[] decryptedBytes = rsa.Decrypt(encryptedBytes, RSAEncryptionPadding.OaepSHA256);
// ๋ฐ์ดํธ๋ฅผ ๋ฌธ์์ด๋ก ๋ณํ
return Encoding.UTF8.GetString(decryptedBytes);
}
}
/// <summary>
/// ๊ฐ์ธํค๋ก ๋ฐ์ดํฐ ์๋ช
/// </summary>
/// <param name="data">์๋ช
ํ ๋ฐ์ดํฐ</param>
/// <param name="privateKey">๊ฐ์ธํค (XML ํ์)</param>
/// <returns>Base64๋ก ์ธ์ฝ๋ฉ๋ ์๋ช
</returns>
public static string SignData(string data, string privateKey)
{
if (string.IsNullOrEmpty(data))
throw new ArgumentNullException(nameof(data));
if (string.IsNullOrEmpty(privateKey))
throw new ArgumentNullException(nameof(privateKey));
using (RSA rsa = RSA.Create())
{
rsa.FromXmlString(privateKey);
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
// SHA-256์ผ๋ก ํด์ํ๊ณ ์๋ช
byte[] signature = rsa.SignData(dataBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
return Convert.ToBase64String(signature);
}
}
/// <summary>
/// ๊ณต๊ฐํค๋ก ์๋ช
๊ฒ์ฆ
/// </summary>
/// <param name="data">์๋ณธ ๋ฐ์ดํฐ</param>
/// <param name="signature">Base64๋ก ์ธ์ฝ๋ฉ๋ ์๋ช
</param>
/// <param name="publicKey">๊ณต๊ฐํค (XML ํ์)</param>
/// <returns>์๋ช
์ด ์ ํจํ์ง ์ฌ๋ถ</returns>
public static bool VerifySignature(string data, string signature, string publicKey)
{
if (string.IsNullOrEmpty(data))
throw new ArgumentNullException(nameof(data));
if (string.IsNullOrEmpty(signature))
throw new ArgumentNullException(nameof(signature));
if (string.IsNullOrEmpty(publicKey))
throw new ArgumentNullException(nameof(publicKey));
try
{
using (RSA rsa = RSA.Create())
{
rsa.FromXmlString(publicKey);
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
byte[] signatureBytes = Convert.FromBase64String(signature);
// ์๋ช
๊ฒ์ฆ
return rsa.VerifyData(dataBytes, signatureBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
}
catch
{
return false;
}
}
/// <summary>
/// RSA๋ก ์ํธํํ ์ ์๋ ์ต๋ ๋ฐ์ดํฐ ํฌ๊ธฐ ๊ณ์ฐ
/// </summary>
public static int GetMaxDataSize(int keySize)
{
// OAEP SHA-256 ํจ๋ฉ์ ์ฌ์ฉํ ๋์ ์ต๋ ํฌ๊ธฐ
// ๊ณต์: (ํค ํฌ๊ธฐ / 8) - (2 * ํด์ ํฌ๊ธฐ) - 2
return (keySize / 8) - (2 * 32) - 2; // SHA-256์ 32๋ฐ์ดํธ
}
}
}
RSA ์ฌ์ฉ ์์ :
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== RSA ์ํธํ ๋ฐ ์๋ช
ํ
์คํธ ===\n");
// 1. ํค ์ ์์ฑ
Console.WriteLine("1. RSA ํค ์ ์์ฑ ์ค...");
RsaEncryption.GenerateKeys(out string publicKey, out string privateKey);
Console.WriteLine("โ
ํค ์์ฑ ์๋ฃ!\n");
// 2. ๋ฐ์ดํฐ ์ํธํ/๋ณตํธํ
Console.WriteLine("2. ๋ฐ์ดํฐ ์ํธํ ํ
์คํธ");
string originalMessage = "์ด๊ฒ์ ๋น๋ฐ ๋ฉ์์ง์
๋๋ค! ๐";
Console.WriteLine($"์๋ณธ ๋ฉ์์ง: {originalMessage}");
string encryptedMessage = RsaEncryption.Encrypt(originalMessage, publicKey);
Console.WriteLine($"์ํธํ๋ ๋ฉ์์ง: {encryptedMessage.Substring(0, 50)}...");
string decryptedMessage = RsaEncryption.Decrypt(encryptedMessage, privateKey);
Console.WriteLine($"๋ณตํธํ๋ ๋ฉ์์ง: {decryptedMessage}");
Console.WriteLine($"๋ณตํธํ ์ฑ๊ณต: {originalMessage == decryptedMessage}\n");
// 3. ๋์งํธ ์๋ช
Console.WriteLine("3. ๋์งํธ ์๋ช
ํ
์คํธ");
string document = "์ค์ํ ๊ณ์ฝ์ ๋ด์ฉ์
๋๋ค.";
Console.WriteLine($"๋ฌธ์: {document}");
string signature = RsaEncryption.SignData(document, privateKey);
Console.WriteLine($"์๋ช
: {signature.Substring(0, 50)}...");
bool isValid = RsaEncryption.VerifySignature(document, signature, publicKey);
Console.WriteLine($"์๋ช
๊ฒ์ฆ: {(isValid ? "โ
์ ํจํจ" : "โ ๋ฌดํจํจ")}\n");
// 4. ์์กฐ๋ ๋ฌธ์ ๊ฒ์ฆ
Console.WriteLine("4. ์์กฐ ๋ฌธ์ ๊ฒ์ฆ ํ
์คํธ");
string tamperedDocument = "์ค์ํ ๊ณ์ฝ์ ๋ด์ฉ์
๋๋ค. (์์กฐ๋จ)";
bool isTamperedValid = RsaEncryption.VerifySignature(tamperedDocument, signature, publicKey);
Console.WriteLine($"์์กฐ ๋ฌธ์ ๊ฒ์ฆ: {(isTamperedValid ? "โ
์ ํจํจ" : "โ ๋ฌดํจํจ (์ ์)")}");
// 5. ์ต๋ ์ํธํ ํฌ๊ธฐ ํ์ธ
Console.WriteLine($"\n5. RSA-2048๋ก ์ํธํ ๊ฐ๋ฅํ ์ต๋ ํฌ๊ธฐ: {RsaEncryption.GetMaxDataSize(2048)} ๋ฐ์ดํธ");
Console.ReadLine();
}
}
2. ์๋: RSA๋ AES๋ณด๋ค ํจ์ฌ ๋๋ ค. ๊ทธ๋์ ๋์ฉ๋ ๋ฐ์ดํฐ ์ํธํ์๋ ๋ถ์ ํฉํด.
3. ํค ๊ด๋ฆฌ: ๊ฐ์ธํค๋ ์ ๋ ๋ ธ์ถ๋๋ฉด ์ ๋ผ! ์์ ํ ์ ์ฅ์์ ๋ณด๊ดํ๊ณ , ๊ฐ๋ฅํ๋ฉด ํ๋์จ์ด ๋ณด์ ๋ชจ๋(HSM)์ ์ฌ์ฉํด.
๐ ํ์ด๋ธ๋ฆฌ๋ ์ํธํ: AES + RSA
์ค์ ์์๋ AES์ RSA๋ฅผ ์กฐํฉํด์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์. ์ด๊ฑธ ํ์ด๋ธ๋ฆฌ๋ ์ํธํ๋ผ๊ณ ํด ๐ฏ
์๋ ๋ฐฉ์์ ์ด๋:
1. ๋๋ค AES ํค๋ฅผ ์์ฑํด
2. ์ค์ ๋ฐ์ดํฐ๋ ๋น ๋ฅธ AES๋ก ์ํธํํด
3. AES ํค๋ RSA ๊ณต๊ฐํค๋ก ์ํธํํด
4. ์ํธํ๋ ๋ฐ์ดํฐ์ ์ํธํ๋ AES ํค๋ฅผ ํจ๊ป ์ ์กํด
๋ฐ๋ ์ชฝ์์๋:
1. RSA ๊ฐ์ธํค๋ก AES ํค๋ฅผ ๋ณตํธํํด
2. ๋ณตํธํ๋ AES ํค๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณตํธํํด
์ด๋ ๊ฒ ํ๋ฉด RSA์ ์์ ์ฑ๊ณผ AES์ ์๋๋ฅผ ๋ชจ๋ ์ป์ ์ ์์ด!
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace DataSecurityExample
{
public class HybridEncryption
{
/// <summary>
/// ํ์ด๋ธ๋ฆฌ๋ ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ ์ํธํ
/// </summary>
/// <param name="plainText">์ํธํํ ํ๋ฌธ</param>
/// <param name="rsaPublicKey">RSA ๊ณต๊ฐํค</param>
/// <returns>์ํธํ๋ ํจํค์ง (AES ์ํธ๋ฌธ + RSA๋ก ์ํธํ๋ AES ํค/IV)</returns>
public static EncryptedPackage Encrypt(string plainText, string rsaPublicKey)
{
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException(nameof(plainText));
if (string.IsNullOrEmpty(rsaPublicKey))
throw new ArgumentNullException(nameof(rsaPublicKey));
// 1. AES ํค์ IV ์์ฑ
byte[] aesKey;
byte[] aesIV;
byte[] encryptedData;
using (Aes aes = Aes.Create())
{
aes.KeySize = 256;
aes.GenerateKey();
aes.GenerateIV();
aesKey = aes.Key;
aesIV = aes.IV;
// 2. AES๋ก ๋ฐ์ดํฐ ์ํธํ
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
encryptedData = msEncrypt.ToArray();
}
}
}
// 3. AES ํค์ IV๋ฅผ RSA๋ก ์ํธํ
byte[] encryptedAesKey;
byte[] encryptedAesIV;
using (RSA rsa = RSA.Create())
{
rsa.FromXmlString(rsaPublicKey);
encryptedAesKey = rsa.Encrypt(aesKey, RSAEncryptionPadding.OaepSHA256);
encryptedAesIV = rsa.Encrypt(aesIV, RSAEncryptionPadding.OaepSHA256);
}
// 4. ํจํค์ง๋ก ๋ฌถ์ด์ ๋ฐํ
return new EncryptedPackage
{
EncryptedData = Convert.ToBase64String(encryptedData),
EncryptedAesKey = Convert.ToBase64String(encryptedAesKey),
EncryptedAesIV = Convert.ToBase64String(encryptedAesIV)
};
}
/// <summary>
/// ํ์ด๋ธ๋ฆฌ๋ ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ ๋ณตํธํ
/// </summary>
/// <param name="package">์ํธํ๋ ํจํค์ง</param>
/// <param name="rsaPrivateKey">RSA ๊ฐ์ธํค</param>
/// <returns>๋ณตํธํ๋ ํ๋ฌธ</returns>
public static string Decrypt(EncryptedPackage package, string rsaPrivateKey)
{
if (package == null)
throw new ArgumentNullException(nameof(package));
if (string.IsNullOrEmpty(rsaPrivateKey))
throw new ArgumentNullException(nameof(rsaPrivateKey));
// 1. RSA๋ก AES ํค์ IV ๋ณตํธํ
byte[] aesKey;
byte[] aesIV;
using (RSA rsa = RSA.Create())
{
rsa.FromXmlString(rsaPrivateKey);
aesKey = rsa.Decrypt(Convert.FromBase64String(package.EncryptedAesKey), RSAEncryptionPadding.OaepSHA256);
aesIV = rsa.Decrypt(Convert.FromBase64String(package.EncryptedAesIV), RSAEncryptionPadding.OaepSHA256);
}
// 2. AES๋ก ๋ฐ์ดํฐ ๋ณตํธํ
string plainText;
byte[] encryptedData = Convert.FromBase64String(package.EncryptedData);
using (Aes aes = Aes.Create())
{
aes.Key = aesKey;
aes.IV = aesIV;
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (MemoryStream msDecrypt = new MemoryStream(encryptedData))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plainText = srDecrypt.ReadToEnd();
}
}
}
}
return plainText;
}
/// <summary>
/// ํ์ผ์ ํ์ด๋ธ๋ฆฌ๋ ๋ฐฉ์์ผ๋ก ์ํธํ
/// </summary>
public static EncryptedPackage EncryptFile(string filePath, string rsaPublicKey)
{
if (!File.Exists(filePath))
throw new FileNotFoundException("ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค.", filePath);
// ํ์ผ์ ์ฝ์ด์ ์ํธํ
string fileContent = File.ReadAllText(filePath);
return Encrypt(fileContent, rsaPublicKey);
}
/// <summary>
/// ํ์ด๋ธ๋ฆฌ๋ ๋ฐฉ์์ผ๋ก ์ํธํ๋ ๋ฐ์ดํฐ๋ฅผ ํ์ผ๋ก ์ ์ฅ
/// </summary>
public static void DecryptToFile(EncryptedPackage package, string rsaPrivateKey, string outputPath)
{
string decryptedContent = Decrypt(package, rsaPrivateKey);
File.WriteAllText(outputPath, decryptedContent);
}
}
/// <summary>
/// ์ํธํ๋ ๋ฐ์ดํฐ ํจํค์ง
/// </summary>
public class EncryptedPackage
{
public string EncryptedData { get; set; } // AES๋ก ์ํธํ๋ ์ค์ ๋ฐ์ดํฐ
public string EncryptedAesKey { get; set; } // RSA๋ก ์ํธํ๋ AES ํค
public string EncryptedAesIV { get; set; } // RSA๋ก ์ํธํ๋ AES IV
}
}
ํ์ด๋ธ๋ฆฌ๋ ์ํธํ ์ฌ์ฉ ์์ :
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== ํ์ด๋ธ๋ฆฌ๋ ์ํธํ ํ
์คํธ ===\n");
// 1. RSA ํค ์ ์์ฑ
RsaEncryption.GenerateKeys(out string publicKey, out string privateKey);
Console.WriteLine("โ
RSA ํค ์ ์์ฑ ์๋ฃ\n");
// 2. ๋์ฉ๋ ๋ฐ์ดํฐ ์ค๋น
StringBuilder largeData = new StringBuilder();
for (int i = 0; i < 100; i++)
{
largeData.AppendLine($"์ด๊ฒ์ {i + 1}๋ฒ์งธ ์ค์
๋๋ค. ํ์ด๋ธ๋ฆฌ๋ ์ํธํ ํ
์คํธ ์ค!");
}
string originalData = largeData.ToString();
Console.WriteLine($"์๋ณธ ๋ฐ์ดํฐ ํฌ๊ธฐ: {originalData.Length} ๋ฐ์ดํธ\n");
// 3. ํ์ด๋ธ๋ฆฌ๋ ์ํธํ
Console.WriteLine("์ํธํ ์ค...");
var startTime = DateTime.Now;
var encryptedPackage = HybridEncryption.Encrypt(originalData, publicKey);
var encryptTime = (DateTime.Now - startTime).TotalMilliseconds;
Console.WriteLine($"โ
์ํธํ ์๋ฃ ({encryptTime:F2}ms)");
Console.WriteLine($"์ํธํ๋ ๋ฐ์ดํฐ ํฌ๊ธฐ: {encryptedPackage.EncryptedData.Length} ๋ฐ์ดํธ");
Console.WriteLine($"์ํธํ๋ AES ํค ํฌ๊ธฐ: {encryptedPackage.EncryptedAesKey.Length} ๋ฐ์ดํธ\n");
// 4. ํ์ด๋ธ๋ฆฌ๋ ๋ณตํธํ
Console.WriteLine("๋ณตํธํ ์ค...");
startTime = DateTime.Now;
string decryptedData = HybridEncryption.Decrypt(encryptedPackage, privateKey);
var decryptTime = (DateTime.Now - startTime).TotalMilliseconds;
Console.WriteLine($"โ
๋ณตํธํ ์๋ฃ ({decryptTime:F2}ms)");
// 5. ๊ฒ์ฆ
bool isMatch = originalData == decryptedData;
Console.WriteLine($"\n์๋ณธ๊ณผ ๋ณตํธํ ๋ฐ์ดํฐ ์ผ์น: {(isMatch ? "โ
์ฑ๊ณต" : "โ ์คํจ")}");
// 6. ํ์ผ ์ํธํ ํ
์คํธ
Console.WriteLine("\n--- ํ์ผ ์ํธํ ํ
์คํธ ---");
string testFile = "test_document.txt";
File.WriteAllText(testFile, "์ด๊ฒ์ ๊ธฐ๋ฐ ๋ฌธ์์
๋๋ค.\n์ค์ํ ์ ๋ณด๊ฐ ํฌํจ๋์ด ์์ต๋๋ค.");
var filePackage = HybridEncryption.EncryptFile(testFile, publicKey);
Console.WriteLine("โ
ํ์ผ ์ํธํ ์๋ฃ");
string decryptedFile = "decrypted_document.txt";
HybridEncryption.DecryptToFile(filePackage, privateKey, decryptedFile);
Console.WriteLine($"โ
ํ์ผ ๋ณตํธํ ์๋ฃ: {decryptedFile}");
// ์ ๋ฆฌ
File.Delete(testFile);
File.Delete(decryptedFile);
Console.ReadLine();
}
}
๐ก๏ธ ์ค์ ๋ณด์ ํ๊ณผ ๋ฒ ์คํธ ํ๋ํฐ์ค
์ด๋ก ๊ณผ ์ฝ๋๋ฅผ ๋ค ๋ฐฐ์ ์ผ๋, ์ด์ ์ค์ ์์ ์ ๋ง ์ค์ํ ๋ณด์ ํ๋ค์ ์๋ ค์ค๊ฒ ๐ ์ด๊ฒ๋ค๋ง ์ ์ง์ผ๋ ๋๋ถ๋ถ์ ๋ณด์ ๋ฌธ์ ๋ฅผ ์๋ฐฉํ ์ ์์ด!
๐ 1. ํค ๊ด๋ฆฌ (Key Management)
โ ์ค์ ํ์ผ์ ํ๋ฌธ์ผ๋ก ํค ์ ์ฅ
โ Git์ ํค ํ์ผ ์ปค๋ฐ
โ ๋ก๊ทธ์ ํค ์ถ๋ ฅ
โ ์๋ฌ ๋ฉ์์ง์ ํค ํฌํจ
โ Azure Key Vault, AWS KMS ๊ฐ์ ํค ๊ด๋ฆฌ ์๋น์ค
โ Windows DPAPI (Data Protection API)
โ ํ๋์จ์ด ๋ณด์ ๋ชจ๋(HSM)
โ .NET Secret Manager (๊ฐ๋ฐ ํ๊ฒฝ)
// Windows DPAPI๋ฅผ ์ฌ์ฉํ ํค ๋ณดํธ ์์
using System.Security.Cryptography;
public class SecureKeyStorage
{
/// <summary>
/// ํค๋ฅผ ์์ ํ๊ฒ ์ํธํํด์ ์ ์ฅ
/// </summary>
public static void ProtectKey(byte[] key, string filePath)
{
// DPAPI๋ก ํค ์ํธํ (ํ์ฌ ์ฌ์ฉ์๋ง ๋ณตํธํ ๊ฐ๋ฅ)
byte[] protectedKey = ProtectedData.Protect(
key,
null, // ์ถ๊ฐ ์ํธ๋กํผ (์ ํ์ฌํญ)
DataProtectionScope.CurrentUser
);
// ์ํธํ๋ ํค๋ฅผ ํ์ผ๋ก ์ ์ฅ
File.WriteAllBytes(filePath, protectedKey);
}
/// <summary>
/// ๋ณดํธ๋ ํค๋ฅผ ๋ณตํธํํด์ ๋ก๋
/// </summary>
public static byte[] UnprotectKey(string filePath)
{
// ์ํธํ๋ ํค ์ฝ๊ธฐ
byte[] protectedKey = File.ReadAllBytes(filePath);
// DPAPI๋ก ๋ณตํธํ
byte[] key = ProtectedData.Unprotect(
protectedKey,
null,
DataProtectionScope.CurrentUser
);
return key;
}
}
// ์ฌ์ฉ ์์
byte[] mySecretKey = AesEncryption.GenerateRandomKey();
SecureKeyStorage.ProtectKey(mySecretKey, "protected_key.dat");
// ๋์ค์ ํค ๋ก๋
byte[] loadedKey = SecureKeyStorage.UnprotectKey("protected_key.dat");
๐ 2. Salt์ IV๋ ๋งค๋ฒ ์๋ก ์์ฑ
์ด๊ฑฐ ์ง์ง ์ค์ํด! โ ๏ธ ๊ฐ์ Salt๋ IV๋ฅผ ์ฌ์ฌ์ฉํ๋ฉด ๋ณด์์ด ํฌ๊ฒ ์ฝํ๋ผ. ํนํ ๋น๋ฐ๋ฒํธ ํด์ฑํ ๋ Salt๋ ์ฌ์ฉ์๋ง๋ค ๋ค๋ฅด๊ฒ ์์ฑํด์ผ ํ๊ณ , AES ์ํธํํ ๋ IV๋ ์ํธํํ ๋๋ง๋ค ์๋ก ๋ง๋ค์ด์ผ ํด.
๐ฏ 3. ์ ์ ํ ์๊ณ ๋ฆฌ์ฆ ์ ํ
| ์ฉ๋ | ์ถ์ฒ ์๊ณ ๋ฆฌ์ฆ | ์ด์ |
|---|---|---|
| ๋น๋ฐ๋ฒํธ ์ ์ฅ | PBKDF2, bcrypt, Argon2 | ๋๋ฆฐ ํด์ฑ์ผ๋ก ๋ฌด์ฐจ๋ณ ๋์ ๊ณต๊ฒฉ ๋ฐฉ์ด |
| ๋์ฉ๋ ๋ฐ์ดํฐ ์ํธํ | AES-256 | ๋น ๋ฅด๊ณ ์์ ํจ |
| ํค ๊ตํ | RSA-2048 ์ด์, ECDH | ๋น๋์นญํค์ ์์ ์ฑ |
| ๋์งํธ ์๋ช | RSA, ECDSA | ๋ถ์ธ ๋ฐฉ์ง ๊ธฐ๋ฅ |
| ํ์ผ ๋ฌด๊ฒฐ์ฑ | SHA-256, SHA-512 | ์ถฉ๋ ์ ํญ์ฑ |
| API ์ธ์ฆ | HMAC-SHA256 | ํค ๊ธฐ๋ฐ ๋ฉ์์ง ์ธ์ฆ |
โก 4. ์ฑ๋ฅ ์ต์ ํ
๋ณด์๊ณผ ์ฑ๋ฅ์ ํธ๋ ์ด๋์คํ ๊ด๊ณ์ผ. ํ์ง๋ง ๋ช ๊ฐ์ง ํ์ผ๋ก ๋ ๋ค ์ก์ ์ ์์ด:
1. ๊ฐ์ฒด ์ฌ์ฌ์ฉ: ์ํธํ ๊ฐ์ฒด๋ฅผ ๋งค๋ฒ ์์ฑํ์ง ๋ง๊ณ ํ๋ง ์ฌ์ฉ
2. ์คํธ๋ฆผ ์ฒ๋ฆฌ: ๋์ฉ๋ ํ์ผ์ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ฆฌ์ง ๋ง๊ณ ์คํธ๋ฆผ์ผ๋ก ์ฒ๋ฆฌ
3. ๋ณ๋ ฌ ์ฒ๋ฆฌ: ์ฌ๋ฌ ํ์ผ์ ์ํธํํ ๋๋ Parallel.ForEach ํ์ฉ
4. ์ ์ ํ ๋ฒํผ ํฌ๊ธฐ: ํ์ผ ์ํธํ ์ 8KB~64KB ๋ฒํผ ์ฌ์ฉ
// ๋์ฉ๋ ํ์ผ ์ํธํ ์ต์ ํ ์์
public class OptimizedFileEncryption
{
private const int BufferSize = 64 * 1024; // 64KB ๋ฒํผ
public static void EncryptLargeFile(string inputPath, string outputPath, byte[] key, byte[] iv)
{
using (Aes aes = Aes.Create())
{
aes.Key = key;
aes.IV = iv;
using (FileStream fsInput = new FileStream(inputPath, FileMode.Open, FileAccess.Read))
using (FileStream fsOutput = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
using (ICryptoTransform encryptor = aes.CreateEncryptor())
using (CryptoStream cs = new CryptoStream(fsOutput, encryptor, CryptoStreamMode.Write))
{
byte[] buffer = new byte[BufferSize];
int bytesRead;
// ์คํธ๋ฆผ์ผ๋ก ์ฒ๋ฆฌ - ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ !
while ((bytesRead = fsInput.Read(buffer, 0, buffer.Length)) > 0)
{
cs.Write(buffer, 0, bytesRead);
}
}
}
}
}
๐จ 5. ์๋ฌ ์ฒ๋ฆฌ์ ๋ก๊น
๋ณด์ ๊ด๋ จ ์๋ฌ ์ฒ๋ฆฌ๋ ์กฐ์ฌํด์ผ ํด. ๋๋ฌด ์์ธํ ์๋ฌ ๋ฉ์์ง๋ ๊ณต๊ฒฉ์์๊ฒ ํํธ๋ฅผ ์ค ์ ์๊ฑฐ๋ ๐ต๏ธ
โ ์ข์ ์:
"๋ก๊ทธ์ธ์ ์คํจํ์ต๋๋ค. ์์ด๋ ๋๋ ๋น๋ฐ๋ฒํธ๋ฅผ ํ์ธํด์ฃผ์ธ์."
public class SecureErrorHandling
{
private static readonly ILogger _logger = LogManager.GetLogger("Security");
public static bool SecureLogin(string username, string password)
{
try
{
// ๋ก๊ทธ์ธ ๋ก์ง...
var user = GetUser(username);
if (user == null)
{
// ๋ณด์ ๋ก๊ทธ (๋ด๋ถ์ฉ)
_logger.Warn($"Login attempt for non-existent user: {username}");
// ์ฌ์ฉ์์๊ฒ๋ ์ผ๋ฐ์ ์ธ ๋ฉ์์ง
return false;
}
bool isValid = PasswordHasher.VerifyPassword(password, user.PasswordHash);
if (!isValid)
{
// ๋ณด์ ๋ก๊ทธ
_logger.Warn($"Failed login attempt for user: {username}");
}
else
{
_logger.Info($"Successful login for user: {username}");
}
return isValid;
}
catch (Exception ex)
{
// ์์ธํ ์๋ฌ๋ ๋ก๊ทธ์๋ง
_logger.Error($"Login error for user {username}: {ex.Message}", ex);
// ์ฌ์ฉ์์๊ฒ๋ ์ผ๋ฐ์ ์ธ ๋ฉ์์ง
throw new SecurityException("๋ก๊ทธ์ธ ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.");
}
}
}
๐ ์ค์ ์๋๋ฆฌ์ค: ์์ ํ API ํต์
์, ์ด์ ๋ฐฐ์ด ๊ฑธ ์ข
ํฉํด์ ์ค์ ์๋๋ฆฌ์ค๋ฅผ ๊ตฌํํด๋ณด์! API ํต์ ์ ์์ ํ๊ฒ ํ๋ ์์ ํ ์์ ์ผ ๐
์๋๋ฆฌ์ค: ํด๋ผ์ด์ธํธ๊ฐ ์๋ฒ์ ๋ฏผ๊ฐํ ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๊ณ , ์๋ฒ๋ ์๋ต์ ์ํธํํด์ ๋ณด๋ด๋ ์ํฉ
using System;
using System.Text.Json;
using System.Security.Cryptography;
namespace DataSecurityExample
{
/// <summary>
/// ์์ ํ API ํต์ ์ ์ํ ํด๋์ค
/// </summary>
public class SecureApiCommunication
{
private readonly string _rsaPublicKey;
private readonly string _rsaPrivateKey;
private readonly string _apiSecret;
public SecureApiCommunication(string rsaPublicKey, string rsaPrivateKey, string apiSecret)
{
_rsaPublicKey = rsaPublicKey;
_rsaPrivateKey = rsaPrivateKey;
_apiSecret = apiSecret;
}
/// <summary>
/// ์์ ํ API ์์ฒญ ์์ฑ
/// </summary>
public SecureApiRequest CreateRequest<T>(T data)
{
// 1. ๋ฐ์ดํฐ๋ฅผ JSON์ผ๋ก ์ง๋ ฌํ
string jsonData = JsonSerializer.Serialize(data);
// 2. ํ์์คํฌํ ์ถ๊ฐ (์ฌ์ ๊ณต๊ฒฉ ๋ฐฉ์ง)
long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
// 3. ํ์ด๋ธ๋ฆฌ๋ ์ํธํ
var encryptedPackage = HybridEncryption.Encrypt(jsonData, _rsaPublicKey);
// 4. HMAC ์๋ช
์์ฑ (๋ฌด๊ฒฐ์ฑ ๋ณด์ฅ)
string signatureData = $"{encryptedPackage.EncryptedData}|{timestamp}";
string signature = HashingUtility.ComputeHMACSHA256(signatureData, _apiSecret);
return new SecureApiRequest
{
EncryptedData = encryptedPackage.EncryptedData,
EncryptedAesKey = encryptedPackage.EncryptedAesKey,
EncryptedAesIV = encryptedPackage.EncryptedAesIV,
Timestamp = timestamp,
Signature = signature
};
}
/// <summary>
/// API ์์ฒญ ๊ฒ์ฆ ๋ฐ ๋ณตํธํ
/// </summary>
public T ProcessRequest<T>(SecureApiRequest request)
{
// 1. ํ์์คํฌํ ๊ฒ์ฆ (5๋ถ ์ด๋ด๋ง ์ ํจ)
long currentTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
if (Math.Abs(currentTimestamp - request.Timestamp) > 300)
{
throw new SecurityException("์์ฒญ์ด ๋ง๋ฃ๋์์ต๋๋ค.");
}
// 2. HMAC ์๋ช
๊ฒ์ฆ
string signatureData = $"{request.EncryptedData}|{request.Timestamp}";
string expectedSignature = HashingUtility.ComputeHMACSHA256(signatureData, _apiSecret);
if (!HashingUtility.CompareHashes(request.Signature, expectedSignature))
{
throw new SecurityException("์๋ช
๊ฒ์ฆ์ ์คํจํ์ต๋๋ค.");
}
// 3. ๋ฐ์ดํฐ ๋ณตํธํ
var package = new EncryptedPackage
{
EncryptedData = request.EncryptedData,
EncryptedAesKey = request.EncryptedAesKey,
EncryptedAesIV = request.EncryptedAesIV
};
string jsonData = HybridEncryption.Decrypt(package, _rsaPrivateKey);
// 4. JSON ์ญ์ง๋ ฌํ
return JsonSerializer.Deserialize<T>(jsonData);
}
/// <summary>
/// ์์ ํ API ์๋ต ์์ฑ
/// </summary>
public SecureApiResponse CreateResponse<T>(T data, bool success, string message = null)
{
var responseData = new ApiResponseData<T>
{
Success = success,
Message = message,
Data = data,
Timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
};
string jsonData = JsonSerializer.Serialize(responseData);
var encryptedPackage = HybridEncryption.Encrypt(jsonData, _rsaPublicKey);
return new SecureApiResponse
{
EncryptedData = encryptedPackage.EncryptedData,
EncryptedAesKey = encryptedPackage.EncryptedAesKey,
EncryptedAesIV = encryptedPackage.EncryptedAesIV
};
}
/// <summary>
/// API ์๋ต ๋ณตํธํ
/// </summary>
public ApiResponseData<T> ProcessResponse<T>(SecureApiResponse response)
{
var package = new EncryptedPackage
{
EncryptedData = response.EncryptedData,
EncryptedAesKey = response.EncryptedAesKey,
EncryptedAesIV = response.EncryptedAesIV
};
string jsonData = HybridEncryption.Decrypt(package, _rsaPrivateKey);
return JsonSerializer.Deserialize<ApiResponseData<T>>(jsonData);
}
}
// ๋ฐ์ดํฐ ๋ชจ๋ธ๋ค
public class SecureApiRequest
{
public string EncryptedData { get; set; }
public string EncryptedAesKey { get; set; }
public string EncryptedAesIV { get; set; }
public long Timestamp { get; set; }
public string Signature { get; set; }
}
public class SecureApiResponse
{
public string EncryptedData { get; set; }
public string EncryptedAesKey { get; set; }
public string EncryptedAesIV { get; set; }
}
public class ApiResponseData<T>
{
public bool Success { get; set; }
public string Message { get; set; }
public T Data { get; set; }
public long Timestamp { get; set; }
}
}
์ค์ ์ฌ์ฉ ์์ :
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== ์์ ํ API ํต์ ์๋ฎฌ๋ ์ด์
===\n");
// 1. ์ด๊ธฐ ์ค์
RsaEncryption.GenerateKeys(out string publicKey, out string privateKey);
string apiSecret = "super-secret-api-key-12345";
var clientApi = new SecureApiCommunication(publicKey, privateKey, apiSecret);
var serverApi = new SecureApiCommunication(publicKey, privateKey, apiSecret);
// 2. ํด๋ผ์ด์ธํธ โ ์๋ฒ ์์ฒญ
Console.WriteLine("๐ค ํด๋ผ์ด์ธํธ: ๋ฏผ๊ฐํ ๋ฐ์ดํฐ ์ ์ก");
var sensitiveData = new
{
UserId = 12345,
CreditCard = "1234-5678-9012-3456",
Amount = 50000,
Purpose = "์ฌ๋ฅ๋ท์์ ํ๋ก๊ทธ๋๋ฐ ๊ฐ์ ๊ตฌ๋งค"
};
var request = clientApi.CreateRequest(sensitiveData);
Console.WriteLine($"์ํธํ๋ ์์ฒญ ์์ฑ ์๋ฃ");
Console.WriteLine($"์๋ช
: {request.Signature.Substring(0, 20)}...\n");
// 3. ์๋ฒ์์ ์์ฒญ ์ฒ๋ฆฌ
Console.WriteLine("๐ฅ ์๋ฒ: ์์ฒญ ์์ ๋ฐ ๊ฒ์ฆ");
try
{
var receivedData = serverApi.ProcessRequest<dynamic>(request);
Console.WriteLine("โ
์๋ช
๊ฒ์ฆ ์ฑ๊ณต");
Console.WriteLine("โ
๋ฐ์ดํฐ ๋ณตํธํ ์ฑ๊ณต");
Console.WriteLine($"์์ ๋ฐ์ดํฐ: UserId={receivedData.GetProperty("UserId")}, Amount={receivedData.GetProperty("Amount")}\n");
// 4. ์๋ฒ โ ํด๋ผ์ด์ธํธ ์๋ต
Console.WriteLine("๐ค ์๋ฒ: ์๋ต ์์ฑ");
var responseData = new
{
TransactionId = "TXN-" + Guid.NewGuid().ToString().Substring(0, 8),
Status = "Approved",
ProcessedAt = DateTime.Now
};
var response = serverApi.CreateResponse(responseData, true, "๊ฒฐ์ ๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ฒ๋ฆฌ๋์์ต๋๋ค.");
Console.WriteLine("์ํธํ๋ ์๋ต ์์ฑ ์๋ฃ\n");
// 5. ํด๋ผ์ด์ธํธ์์ ์๋ต ์ฒ๋ฆฌ
Console.WriteLine("๐ฅ ํด๋ผ์ด์ธํธ: ์๋ต ์์ ");
var processedResponse = clientApi.ProcessResponse<dynamic>(response);
Console.WriteLine($"โ
์๋ต ๋ณตํธํ ์ฑ๊ณต");
Console.WriteLine($"๊ฒฐ๊ณผ: {processedResponse.Message}");
Console.WriteLine($"๊ฑฐ๋ ID: {processedResponse.Data.GetProperty("TransactionId")}");
}
catch (SecurityException ex)
{
Console.WriteLine($"โ ๋ณด์ ์ค๋ฅ: {ex.Message}");
}
Console.ReadLine();
}
}
2. ๋ฌด๊ฒฐ์ฑ: HMAC ์๋ช ์ผ๋ก ๋ณ์กฐ ๋ฐฉ์ง
3. ์ธ์ฆ: API ์ํฌ๋ฆฟ์ผ๋ก ๋ฐ์ ์ ํ์ธ
4. ์ฌ์ ๊ณต๊ฒฉ ๋ฐฉ์ง: ํ์์คํฌํ๋ก ์ค๋๋ ์์ฒญ ๊ฑฐ๋ถ
5. ๋ถ์ธ ๋ฐฉ์ง: ๋์งํธ ์๋ช ์ผ๋ก ๊ฑฐ๋ ์ฆ๋ช
๐ ์ฌ๋ฅ๋ท์์ ๋ณด์ ์ง์ ๊ณต์ ํ๊ธฐ
์ฌ๊ธฐ๊น์ง ์ฝ์๋ค๋ฉด ์ถํํด! ๐ ์ด์ ๋๋ C# ๋ฐ์ดํฐ ๋ณด์์ ํต์ฌ์ ๋ชจ๋ ์ดํดํ ๊ฑฐ์ผ. ์ด ์ง์์ ์ ๋ง ๊ฐ์น์์ด. ์ค์ ๋ก ๋ง์ ๊ธฐ์
๋ค์ด ๋ณด์ ์ ๋ฌธ๊ฐ๋ฅผ ์ฐพ๊ณ ์๊ฑฐ๋ .
์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ์ด๋ฐ ์ ๋ฌธ ์ง์์ ๊ณต์ ํ๋ฉด ์ด๋จ๊น? ์๋ฅผ ๋ค์ด:
โข "C# ๋ณด์ ์ฝ๋ ๋ฆฌ๋ทฐ ์๋น์ค" - ๋ค๋ฅธ ๊ฐ๋ฐ์์ ์ฝ๋๋ฅผ ๊ฒํ ํ๊ณ ๋ณด์ ์ทจ์ฝ์ ์ ์ฐพ์์ค
โข "์ํธํ ๊ตฌํ ์ปจ์คํ
" - ํ๋ก์ ํธ์ ๋ง๋ ์ํธํ ๋ฐฉ์์ ์ค๊ณํด์ค
โข "๋ณด์ ๊ต์ก ๋ฐ ๋ฉํ ๋ง" - ์ฃผ๋์ด ๊ฐ๋ฐ์๋ค์๊ฒ ๋ณด์ ๊ฐ๋ฐ ๋ฐฉ๋ฒ์ ๊ฐ๋ฅด์ณ์ค
โข "๋ณด์ ๊ฐ์ฌ ๋ฐ ์ง๋จ" - ๊ธฐ์กด ์์คํ
์ ๋ณด์ ์์ค์ ํ๊ฐํด์ค
์ด๋ฐ ์ฌ๋ฅ๋ค์ ์์ฅ์์ ๋์ ๊ฐ์น๋ฅผ ์ธ์ ๋ฐ๊ณ ์์ด. ํนํ ๊ธ์ต, ์๋ฃ, ์ ์์๊ฑฐ๋ ๋ถ์ผ์์๋ ํ์์ ์ด์ง ๐ผ
๐ ๋ณด์ ์ฒดํฌ๋ฆฌ์คํธ
๋ง์ง๋ง์ผ๋ก ํ๋ก์ ํธ์ ์ ์ฉํ ์ ์๋ ์ค์ ์ฒดํฌ๋ฆฌ์คํธ๋ฅผ ์ ๋ฆฌํด์ค๊ฒ. ์ด๊ฑธ ํ๋์ฉ ์ฒดํฌํ๋ฉด์ ๋ณด์์ ๊ฐํํด๋ด!
โก IV๋ ๋งค๋ฒ ์๋ก ์์ฑํ๊ณ ์ํธ๋ฌธ๊ณผ ํจ๊ป ์ ์ฅ
โก CBC ๋ชจ๋ ์ฌ์ฉ ์ PKCS7 ํจ๋ฉ ์ ์ฉ
โก ํค๋ ์์ ํ ์ ์ฅ์์ ๋ณด๊ด (Key Vault, DPAPI ๋ฑ)
โก ๋์ฉ๋ ํ์ผ์ ์คํธ๋ฆผ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌ
โก ์ํธํ ์คํจ ์ ์ ์ ํ ์๋ฌ ์ฒ๋ฆฌ
โก Salt๋ ์ฌ์ฉ์๋ง๋ค ๋ค๋ฅด๊ฒ ์์ฑ (์ต์ 32๋ฐ์ดํธ)
โก ๋ฐ๋ณต ํ์๋ 100,000ํ ์ด์
โก ํ์ผ ๋ฌด๊ฒฐ์ฑ์ SHA-256 ์ด์ ์ฌ์ฉ
โก API ์๋ช ์ HMAC-SHA256 ์ฌ์ฉ
โก ํด์ ๋น๊ต๋ ํ์ด๋ฐ ๊ณต๊ฒฉ ๋ฐฉ์ง ํจ์ ์ฌ์ฉ
โก OAEP ํจ๋ฉ ์ฌ์ฉ (Pkcs1์ ์ทจ์ฝ)
โก ๊ฐ์ธํค๋ ์ ๋ ๋ ธ์ถํ์ง ์๊ธฐ
โก ๋์ฉ๋ ๋ฐ์ดํฐ๋ ํ์ด๋ธ๋ฆฌ๋ ๋ฐฉ์ ์ฌ์ฉ
โก ํค ์์ ์ ๊ธฐ์ ์ผ๋ก ๊ฐฑ์
โก ๋์งํธ ์๋ช ์ SHA-256 ์ด์ ์ฌ์ฉ
โก SQL Injection ๋ฐฉ์ง (Parameterized Query)
โก XSS ๋ฐฉ์ง (์ ์ถ๋ ฅ ์ธ์ฝ๋ฉ)
โก CSRF ํ ํฐ ์ฌ์ฉ
โก HTTPS ๊ฐ์ ์ฌ์ฉ
โก ๋ณด์ ํค๋ ์ค์ (CSP, HSTS ๋ฑ)
โก ์ ๊ธฐ์ ์ธ ๋ณด์ ์ ๋ฐ์ดํธ
โก ๋ก๊น ๋ฐ ๋ชจ๋ํฐ๋ง ๊ตฌํ
โก ์ต์ ๊ถํ ์์น ์ ์ฉ
โก ์ ๊ธฐ์ ์ธ ๋ณด์ ๊ฐ์ฌ
๐ ๋ค์ ๋จ๊ณ: ๋ ๊น์ด ํ๊ณ ๋ค๊ธฐ
์ฌ๊ธฐ๊น์ง ๋ฐฐ์ด ๋ด์ฉ์ผ๋ก๋ ์ถฉ๋ถํ ์์ ํ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ค ์ ์์ด. ํ์ง๋ง ๋ณด์์ ๋์ด ์๋ ๋ถ์ผ์ผ ๐ ๋ ๊น์ด ๊ณต๋ถํ๊ณ ์ถ๋ค๋ฉด ์ด๋ฐ ์ฃผ์ ๋ค์ ์ถ์ฒํด:
1. ํ์๊ณก์ ์ํธ(ECC)
RSA๋ณด๋ค ์งง์ ํค๋ก ๊ฐ์ ๋ณด์ ์์ค์ ์ ๊ณตํด. ๋ชจ๋ฐ์ผ์ด๋ IoT์ ์ต์ ํ๋ผ ์์ด.
2. ์์ ๋ด์ฑ ์ํธ(Post-Quantum Cryptography)
๋ฏธ๋์ ์์ ์ปดํจํฐ์๋ ์์ ํ ์ํธ ์๊ณ ๋ฆฌ์ฆ. NIST์์ ํ์คํ ์งํ ์ค์ด์ผ.
3. ์์ง์ ์ฆ๋ช (Zero-Knowledge Proof)
์ ๋ณด๋ฅผ ๊ณต๊ฐํ์ง ์๊ณ ๋ ๊ทธ๊ฒ์ ์๊ณ ์๋ค๋ ๊ฑธ ์ฆ๋ช ํ๋ ๊ธฐ์ . ๋ธ๋ก์ฒด์ธ์์ ํ์ฉ๋ผ.
4. ๋ํ ์ํธ(Homomorphic Encryption)
์ํธํ๋ ์ํ์์ ์ฐ์ฐ์ด ๊ฐ๋ฅํ ํ์ ์ ์ธ ๊ธฐ์ . ํด๋ผ์ฐ๋ ๋ณด์์ ๋ฏธ๋์ผ.
5. ํ๋์จ์ด ๋ณด์ ๋ชจ๋(HSM)
๋ฌผ๋ฆฌ์ ์ผ๋ก ํค๋ฅผ ๋ณดํธํ๋ ์ ์ฉ ํ๋์จ์ด. ๊ธ์ต๊ถ์์ ํ์๋ก ์ฌ์ฉํด.
6. ๋ณด์ ํ๋กํ ์ฝ
TLS/SSL, OAuth 2.0, OpenID Connect ๋ฑ ์ค์ ํ๋กํ ์ฝ ๊ตฌํ.
๐ฌ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์, ์ ๋ง ๊ธด ์ฌ์ ์ด์์ง? ๐
ํ์ง๋ง ์ด์ ๋๋ C#์ผ๋ก ๋ฐ์ดํฐ ๋ณด์์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ์์ ํ ์ดํดํ์ด. ์ํธํ, ํด์ฑ, ๋์งํธ ์๋ช
๊น์ง ๋ชจ๋ ํต์ฌ ๊ฐ๋
์ ๋ค๋ค๊ฑฐ๋ !
๊ธฐ์ตํด, ๋ณด์์ ํ ๋ฒ ๊ตฌํํ๊ณ ๋๋๋ ๊ฒ ์๋์ผ. ๊ณ์ ์
๋ฐ์ดํธํ๊ณ , ์๋ก์ด ์ทจ์ฝ์ ์ ํ์ตํ๊ณ , ์ต์ ๋ณด์ ํธ๋ ๋๋ฅผ ๋ฐ๋ผ๊ฐ์ผ ํด. ํ์ง๋ง ์ค๋ ๋ฐฐ์ด ๊ธฐ์ด๊ฐ ํํํ๋ค๋ฉด ์ด๋ค ๋ณํ์๋ ๋์ํ ์ ์์ ๊ฑฐ์ผ ๐ช
ํน์ ๋งํ๋ ๋ถ๋ถ์ด ์๊ฑฐ๋ ๋ ๊ถ๊ธํ ๊ฒ ์๋ค๋ฉด, ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ์ ๋ฌธ๊ฐ๋ค๊ณผ ์ํตํด๋ด. ๋๋ก๋ ๊ฒฝํ ๋ง์ ๊ฐ๋ฐ์์ ์กฐ์ธ ํ๋ง๋๊ฐ ๋ฉฐ์น ๊ฐ์ ์ฝ์ง์ ์ค์ฌ์ฃผ๊ฑฐ๋ ๐
์, ์ด์ ๋ฐฐ์ด ๊ฑธ ์ค์ ์ ์ ์ฉํด๋ณผ ์๊ฐ์ด์ผ! ์์ ํ๊ณ ๊ฒฌ๊ณ ํ ์ ํ๋ฆฌ์ผ์ด์
์ ๋ง๋ค์ด๋ด. ํ์ดํ
! ๐
1. ์ถฉ๋ถํ ํ ์คํธ (๋จ์ ํ ์คํธ, ํตํฉ ํ ์คํธ)
2. ์ฝ๋ ๋ฆฌ๋ทฐ (ํนํ ๋ณด์ ์ ๋ฌธ๊ฐ์ ๊ฒํ )
3. ๋ณด์ ์ค์บ ๋๊ตฌ ์คํ
4. ์ฑ๋ฅ ํ ์คํธ
์ด ๊ณผ์ ๋ค์ ๊ฑฐ์ณ์ผ ํด. ์์ ํ ๊ฐ๋ฐ ๋๊ธธ ๋ฐ๋ผ! ๐ก๏ธ
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.