Ver3.0 ๐ C++ ์ํธํ์ OpenSSL๋ก ๋ณด์ ๋ง์คํฐํ๊ธฐ

๐ C++ ์ํธํ์ OpenSSL๋ก ๋ณด์ ๋ง์คํฐํ๊ธฐ
์น๊ตฌ์ฒ๋ผ ์ฝ๊ฒ ๋ฐฐ์ฐ๋ ์ํธํ ๊ธฐ์ ์ ๋ชจ๋ ๊ฒ
์ผ, ์์ฆ ์ธ์์ ๋ณด์ ์์ด๋ ์๋ฌด๊ฒ๋ ๋ชปํ๋ ๊ฑฐ ์์ง? ๐ค ์ํ ์ฑ ํ๋ ์ด์ด๋, ์นดํก ํ๋ ๋ณด๋ด๋ ๋ค ์ํธํ๊ฐ ๋ค์ด๊ฐ ์์ด. ๊ทผ๋ฐ ์ด๊ฑธ ์ง์ ๊ตฌํํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํ ๊น? ๋ฐ๋ก C++๊ณผ OpenSSL์ด๋ผ๋ ๊ฐ๋ ฅํ ์กฐํฉ์ด ํ์ํด!
์ค๋์ ๋๋ ํจ๊ป C++๋ก ์ํธํ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ํํค์ณ๋ณผ ๊ฑฐ์ผ. ๊ฑฑ์ ๋ง, ์ด๋ ต์ง ์์. ๋ง์น ๋ ๊ณ ๋ธ๋ก ์กฐ๋ฆฝํ๋ฏ์ด ํ๋์ฉ ์์๊ฐ๋ฉด ๋ผ! ๐งฑ
๐ฏ ์ OpenSSL์ธ๊ฐ?
๋จผ์ OpenSSL์ด ๋ญ์ง๋ถํฐ ์์์ผ๊ฒ ์ง? OpenSSL์ ์ํธํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ ๋ ๊ฐ์์ผ. ์ ์ธ๊ณ ์๋ง์ ์๋ฒ์ ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ฌ์ฉ๋๊ณ ์์ด. ๋ง์น ๋ณด์๊ณ์ ์ค์์ค ์๋ฏธ ๋์ดํ ๊ฐ์ ์กด์ฌ์ง! ๐ช
โข ๋น๋์นญํค ์ํธํ (RSA, ECC ๋ฑ) ๐
โข ํด์ ํจ์ (SHA-256, SHA-512, MD5 ๋ฑ) #๏ธโฃ
โข ๋์งํธ ์๋ช โ๏ธ
โข SSL/TLS ํ๋กํ ์ฝ ์ง์ ๐
โข ์ธ์ฆ์ ๊ด๋ฆฌ ๐
ํนํ C++๊ณผ์ ๊ถํฉ์ด ์ ๋ง ์ข์. ์ฑ๋ฅ๋ ๋ฐ์ด๋๊ณ , ์ ์์ค ์ ์ด๊ฐ ๊ฐ๋ฅํด์ ์ธ๋ฐํ ๋ณด์ ๊ตฌํ์ด ๊ฐ๋ฅํ๊ฑฐ๋ . ๋ง์ฝ ๋ค๊ฐ ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ๋ณด์ ๊ด๋ จ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ค๋ฉด, OpenSSL์ ํ์ ๋๊ตฌ๊ฐ ๋ ๊ฑฐ์ผ!
๐ ๏ธ ๊ฐ๋ฐ ํ๊ฒฝ ์ธํ ํ๊ธฐ
์, ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ์์ํด๋ณผ๊น? ๋จผ์ OpenSSL์ ์ค์นํด์ผ ํด. ์ด์์ฒด์ ๋ณ๋ก ๋ฐฉ๋ฒ์ด ์กฐ๊ธ์ฉ ๋ค๋ฅด๋๊น ์ ๋ฐ๋ผ์! ๐
Windows ํ๊ฒฝ
์๋์ฐ ์ฌ์ฉ์๋ผ๋ฉด ์ด๋ ๊ฒ ํด:
2. ์ค์น ํ ํ๊ฒฝ ๋ณ์์ OpenSSL ๊ฒฝ๋ก ์ถ๊ฐ
3. Visual Studio ํ๋ก์ ํธ ์ค์ ์์ Include ๋๋ ํ ๋ฆฌ์ Library ๋๋ ํ ๋ฆฌ ์ถ๊ฐ
4. ๋ง์ปค ์ ๋ ฅ์ libssl.lib์ libcrypto.lib ์ถ๊ฐ
Linux/macOS ํ๊ฒฝ
๋ฆฌ๋ ์ค๋ ๋งฅ ์ฌ์ฉ์๋ ํจ์ฌ ๊ฐ๋จํด:
# Ubuntu/Debian
sudo apt-get install libssl-dev
# macOS (Homebrew)
brew install openssl
# CentOS/RHEL
sudo yum install openssl-devel์ค์น๊ฐ ๋๋ฌ์ผ๋ฉด ์ ๋๋ก ์ค์น๋๋์ง ํ์ธํด๋ณด์:
openssl version๋ฒ์ ์ ๋ณด๊ฐ ๋์ค๋ฉด ์ฑ๊ณต์ด์ผ! ๐
๐ ๋์นญํค ์ํธํ ๊ตฌํํ๊ธฐ
๋์นญํค ์ํธํ๋ ์ํธํ์ ๋ณตํธํ์ ๊ฐ์ ํค๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ์์ด์ผ. ๋น ๋ฅด๊ณ ํจ์จ์ ์ด์ด์ ๋์ฉ๋ ๋ฐ์ดํฐ ์ํธํ์ ์ ํฉํด. ๊ฐ์ฅ ๋ง์ด ์ฐ์ด๋ AES(Advanced Encryption Standard)๋ก ์์ ๋ฅผ ๋ง๋ค์ด๋ณผ๊ฒ!
AES-256 ์ํธํ ๊ธฐ๋ณธ ๊ตฌ์กฐ
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <iostream>
#include <vector>
#include <string>
class AESCrypto {
private:
unsigned char key[32]; // 256๋นํธ ํค
unsigned char iv[16]; // ์ด๊ธฐํ ๋ฒกํฐ
public:
AESCrypto() {
// ๋๋ค ํค์ IV ์์ฑ
RAND_bytes(key, sizeof(key));
RAND_bytes(iv, sizeof(iv));
}
// ํค์ IV๋ฅผ ์ง์ ์ค์ ํ๋ ์์ฑ์
AESCrypto(const unsigned char* k, const unsigned char* i) {
memcpy(key, k, 32);
memcpy(iv, i, 16);
}
std::vector<unsigned char> encrypt(const std::string& plaintext) {
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
std::vector<unsigned char> ciphertext;
if (!ctx) {
throw std::runtime_error("์ํธํ ์ปจํ
์คํธ ์์ฑ ์คํจ");
}
// ์ํธํ ์ด๊ธฐํ
if (EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key, iv) != 1) {
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("์ํธํ ์ด๊ธฐํ ์คํจ");
}
// ์ถ๋ ฅ ๋ฒํผ ํฌ๊ธฐ ๊ณ์ฐ
int max_output_len = plaintext.length() + AES_BLOCK_SIZE;
ciphertext.resize(max_output_len);
int len = 0;
int ciphertext_len = 0;
// ์ํธํ ์ํ
if (EVP_EncryptUpdate(ctx, ciphertext.data(), &len,
reinterpret_cast<const unsigned char*>(plaintext.c_str()),
plaintext.length()) != 1) {
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("์ํธํ ์คํจ");
}
ciphertext_len = len;
// ์ต์ข
๋ธ๋ก ์ฒ๋ฆฌ
if (EVP_EncryptFinal_ex(ctx, ciphertext.data() + len, &len) != 1) {
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("์ํธํ ๋ง๋ฌด๋ฆฌ ์คํจ");
}
ciphertext_len += len;
ciphertext.resize(ciphertext_len);
EVP_CIPHER_CTX_free(ctx);
return ciphertext;
}
std::string decrypt(const std::vector<unsigned char>& ciphertext) {
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
throw std::runtime_error("๋ณตํธํ ์ปจํ
์คํธ ์์ฑ ์คํจ");
}
// ๋ณตํธํ ์ด๊ธฐํ
if (EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key, iv) != 1) {
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("๋ณตํธํ ์ด๊ธฐํ ์คํจ");
}
std::vector<unsigned char> plaintext(ciphertext.size() + AES_BLOCK_SIZE);
int len = 0;
int plaintext_len = 0;
// ๋ณตํธํ ์ํ
if (EVP_DecryptUpdate(ctx, plaintext.data(), &len,
ciphertext.data(), ciphertext.size()) != 1) {
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("๋ณตํธํ ์คํจ");
}
plaintext_len = len;
// ์ต์ข
๋ธ๋ก ์ฒ๋ฆฌ
if (EVP_DecryptFinal_ex(ctx, plaintext.data() + len, &len) != 1) {
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("๋ณตํธํ ๋ง๋ฌด๋ฆฌ ์คํจ");
}
plaintext_len += len;
EVP_CIPHER_CTX_free(ctx);
return std::string(reinterpret_cast<char*>(plaintext.data()), plaintext_len);
}
};์ค์ ์ฌ์ฉ ์์
int main() {
try {
// AES ์ํธํ ๊ฐ์ฒด ์์ฑ
AESCrypto crypto;
std::string original = "์๋
ํ์ธ์! ์ด๊ฒ์ ๋น๋ฐ ๋ฉ์์ง์
๋๋ค. ๐";
std::cout << "์๋ณธ ๋ฉ์์ง: " << original << std::endl;
// ์ํธํ
auto encrypted = crypto.encrypt(original);
std::cout << "์ํธํ ์๋ฃ! ํฌ๊ธฐ: " << encrypted.size() << " bytes" << std::endl;
// ๋ณตํธํ
std::string decrypted = crypto.decrypt(encrypted);
std::cout << "๋ณตํธํ๋ ๋ฉ์์ง: " << decrypted << std::endl;
// ๊ฒ์ฆ
if (original == decrypted) {
std::cout << "โ
์ํธํ/๋ณตํธํ ์ฑ๊ณต!" << std::endl;
}
} catch (const std::exception& e) {
std::cerr << "์ค๋ฅ ๋ฐ์: " << e.what() << std::endl;
return 1;
}
return 0;
}๐ ๋น๋์นญํค ์ํธํ (RSA) ๊ตฌํํ๊ธฐ
๋น๋์นญํค ์ํธํ๋ ๊ณต๊ฐํค์ ๊ฐ์ธํค ๋ ๊ฐ์ ํค๋ฅผ ์ฌ์ฉํด. ๊ณต๊ฐํค๋ก ์ํธํํ๋ฉด ๊ฐ์ธํค๋ก๋ง ๋ณตํธํํ ์ ์์ด. ๋์งํธ ์๋ช ์ด๋ ํค ๊ตํ์ ์ฃผ๋ก ์ฌ์ฉ๋์ง! ๐จ
RSA ํค ์ ์์ฑํ๊ธฐ
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
class RSACrypto {
private:
RSA* rsa_keypair;
public:
RSACrypto(int key_length = 2048) {
// RSA ํค ์ ์์ฑ
BIGNUM* bne = BN_new();
BN_set_word(bne, RSA_F4); // ๊ณต๊ฐ ์ง์ 65537
rsa_keypair = RSA_new();
if (RSA_generate_key_ex(rsa_keypair, key_length, bne, nullptr) != 1) {
BN_free(bne);
throw std::runtime_error("RSA ํค ์์ฑ ์คํจ");
}
BN_free(bne);
}
~RSACrypto() {
if (rsa_keypair) {
RSA_free(rsa_keypair);
}
}
// ๊ณต๊ฐํค๋ก ์ํธํ
std::vector<unsigned char> encryptPublic(const std::string& plaintext) {
int rsa_size = RSA_size(rsa_keypair);
std::vector<unsigned char> encrypted(rsa_size);
int result = RSA_public_encrypt(
plaintext.length(),
reinterpret_cast<const unsigned char*>(plaintext.c_str()),
encrypted.data(),
rsa_keypair,
RSA_PKCS1_OAEP_PADDING
);
if (result == -1) {
throw std::runtime_error("๊ณต๊ฐํค ์ํธํ ์คํจ");
}
encrypted.resize(result);
return encrypted;
}
// ๊ฐ์ธํค๋ก ๋ณตํธํ
std::string decryptPrivate(const std::vector<unsigned char>& ciphertext) {
int rsa_size = RSA_size(rsa_keypair);
std::vector<unsigned char> decrypted(rsa_size);
int result = RSA_private_decrypt(
ciphertext.size(),
ciphertext.data(),
decrypted.data(),
rsa_keypair,
RSA_PKCS1_OAEP_PADDING
);
if (result == -1) {
throw std::runtime_error("๊ฐ์ธํค ๋ณตํธํ ์คํจ");
}
return std::string(reinterpret_cast<char*>(decrypted.data()), result);
}
// ๊ฐ์ธํค๋ก ์๋ช
std::vector<unsigned char> sign(const std::string& message) {
// SHA-256 ํด์ ๊ณ์ฐ
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256(reinterpret_cast<const unsigned char*>(message.c_str()),
message.length(), hash);
std::vector<unsigned char> signature(RSA_size(rsa_keypair));
unsigned int sig_len;
if (RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
signature.data(), &sig_len, rsa_keypair) != 1) {
throw std::runtime_error("์๋ช
์์ฑ ์คํจ");
}
signature.resize(sig_len);
return signature;
}
// ๊ณต๊ฐํค๋ก ์๋ช
๊ฒ์ฆ
bool verify(const std::string& message,
const std::vector<unsigned char>& signature) {
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256(reinterpret_cast<const unsigned char*>(message.c_str()),
message.length(), hash);
int result = RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH,
signature.data(), signature.size(), rsa_keypair);
return result == 1;
}
// ํค๋ฅผ ํ์ผ๋ก ์ ์ฅ
void saveKeys(const std::string& public_file, const std::string& private_file) {
// ๊ณต๊ฐํค ์ ์ฅ
FILE* pub_file = fopen(public_file.c_str(), "wb");
if (pub_file) {
PEM_write_RSAPublicKey(pub_file, rsa_keypair);
fclose(pub_file);
}
// ๊ฐ์ธํค ์ ์ฅ
FILE* priv_file = fopen(private_file.c_str(), "wb");
if (priv_file) {
PEM_write_RSAPrivateKey(priv_file, rsa_keypair, nullptr,
nullptr, 0, nullptr, nullptr);
fclose(priv_file);
}
}
};โข ๊ฐ์ธํค๋ ์ ๋ ๋น๋ฐ๋ก ์ ์ง ๐คซ
โข ๋์นญํค๋ณด๋ค ๋๋ฆฌ์ง๋ง ํค ๋ฐฐํฌ๊ฐ ์์ ํจ ๐ข
โข ์ฃผ๋ก ๋์นญํค๋ฅผ ์์ ํ๊ฒ ์ ๋ฌํ๋ ์ฉ๋๋ก ์ฌ์ฉ ๐ฆ
โข ๋์งํธ ์๋ช ์ผ๋ก ๋ฌด๊ฒฐ์ฑ๊ณผ ์ธ์ฆ ๋ณด์ฅ โ
๐จ ํด์ ํจ์ ํ์ฉํ๊ธฐ
ํด์ ํจ์๋ ์์์ ๊ธธ์ด ๋ฐ์ดํฐ๋ฅผ ๊ณ ์ ๋ ๊ธธ์ด์ ๊ฐ์ผ๋ก ๋ณํํด. ๋น๋ฐ๋ฒํธ ์ ์ฅ, ๋ฐ์ดํฐ ๋ฌด๊ฒฐ์ฑ ๊ฒ์ฆ ๋ฑ์ ํ์์ ์ด์ผ! ๐ฏ
๋ค์ํ ํด์ ์๊ณ ๋ฆฌ์ฆ ๊ตฌํ
#include <openssl/sha.h>
#include <openssl/md5.h>
#include <iomanip>
#include <sstream>
class HashUtil {
public:
// SHA-256 ํด์
static std::string sha256(const std::string& input) {
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256(reinterpret_cast<const unsigned char*>(input.c_str()),
input.length(), hash);
return bytesToHex(hash, SHA256_DIGEST_LENGTH);
}
// SHA-512 ํด์
static std::string sha512(const std::string& input) {
unsigned char hash[SHA512_DIGEST_LENGTH];
SHA512(reinterpret_cast<const unsigned char*>(input.c_str()),
input.length(), hash);
return bytesToHex(hash, SHA512_DIGEST_LENGTH);
}
// MD5 ํด์ (๋ณด์์ ๊ถ์ฅํ์ง ์์, ํธํ์ฑ ๋ชฉ์ ์ผ๋ก๋ง ์ฌ์ฉ)
static std::string md5(const std::string& input) {
unsigned char hash[MD5_DIGEST_LENGTH];
MD5(reinterpret_cast<const unsigned char*>(input.c_str()),
input.length(), hash);
return bytesToHex(hash, MD5_DIGEST_LENGTH);
}
// HMAC (Hash-based Message Authentication Code)
static std::string hmacSha256(const std::string& key, const std::string& data) {
unsigned char hash[SHA256_DIGEST_LENGTH];
HMAC(EVP_sha256(), key.c_str(), key.length(),
reinterpret_cast<const unsigned char*>(data.c_str()),
data.length(), hash, nullptr);
return bytesToHex(hash, SHA256_DIGEST_LENGTH);
}
private:
static std::string bytesToHex(const unsigned char* data, size_t len) {
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (size_t i = 0; i < len; ++i) {
ss << std::setw(2) << static_cast<int>(data[i]);
}
return ss.str();
}
};๋น๋ฐ๋ฒํธ ์์ ํ๊ฒ ์ ์ฅํ๊ธฐ
๋น๋ฐ๋ฒํธ๋ ์ ๋ ํ๋ฌธ์ผ๋ก ์ ์ฅํ๋ฉด ์ ๋ผ! Salt์ ํจ๊ป ํด์ํด์ ์ ์ฅํด์ผ ํด. ๐ง
#include <openssl/evp.h>
class PasswordManager {
public:
struct HashedPassword {
std::string hash;
std::string salt;
};
// ๋น๋ฐ๋ฒํธ ํด์ฑ (PBKDF2 ์ฌ์ฉ)
static HashedPassword hashPassword(const std::string& password) {
HashedPassword result;
// ๋๋ค Salt ์์ฑ (16๋ฐ์ดํธ)
unsigned char salt[16];
RAND_bytes(salt, sizeof(salt));
result.salt = bytesToHex(salt, sizeof(salt));
// PBKDF2๋ก ํด์ ์์ฑ
unsigned char hash[32];
PKCS5_PBKDF2_HMAC(
password.c_str(), password.length(),
salt, sizeof(salt),
100000, // ๋ฐ๋ณต ํ์ (๋์์๋ก ์์ ํ์ง๋ง ๋๋ฆผ)
EVP_sha256(),
sizeof(hash),
hash
);
result.hash = bytesToHex(hash, sizeof(hash));
return result;
}
// ๋น๋ฐ๋ฒํธ ๊ฒ์ฆ
static bool verifyPassword(const std::string& password,
const std::string& stored_hash,
const std::string& stored_salt) {
// Salt๋ฅผ ๋ฐ์ดํธ๋ก ๋ณํ
std::vector<unsigned char> salt = hexToBytes(stored_salt);
// ์
๋ ฅ๋ ๋น๋ฐ๋ฒํธ๋ก ํด์ ์์ฑ
unsigned char hash[32];
PKCS5_PBKDF2_HMAC(
password.c_str(), password.length(),
salt.data(), salt.size(),
100000,
EVP_sha256(),
sizeof(hash),
hash
);
std::string computed_hash = bytesToHex(hash, sizeof(hash));
// ํ์ด๋ฐ ๊ณต๊ฒฉ ๋ฐฉ์ง๋ฅผ ์ํ ์์ ์๊ฐ ๋น๊ต
return constantTimeCompare(computed_hash, stored_hash);
}
private:
static std::string bytesToHex(const unsigned char* data, size_t len) {
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (size_t i = 0; i < len; ++i) {
ss << std::setw(2) << static_cast<int>(data[i]);
}
return ss.str();
}
static std::vector<unsigned char> hexToBytes(const std::string& hex) {
std::vector<unsigned char> bytes;
for (size_t i = 0; i < hex.length(); i += 2) {
std::string byte_str = hex.substr(i, 2);
bytes.push_back(static_cast<unsigned char>(std::stoi(byte_str, nullptr, 16)));
}
return bytes;
}
static bool constantTimeCompare(const std::string& a, const std::string& b) {
if (a.length() != b.length()) return false;
int result = 0;
for (size_t i = 0; i < a.length(); ++i) {
result |= a[i] ^ b[i];
}
return result == 0;
}
};๐ SSL/TLS ํต์ ๊ตฌํํ๊ธฐ
์ด์ ์ง์ง ์ค์ ์ด์ผ! ์น ์๋ฒ์ ์์ ํ๊ฒ ํต์ ํ๋ ค๋ฉด SSL/TLS๊ฐ ํ์์ง. HTTPS๊ฐ ๋ฐ๋ก ์ด๊ฑฐ์ผ! ๐
SSL ํด๋ผ์ด์ธํธ ๊ตฌํ
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
class SSLClient {
private:
SSL_CTX* ctx;
SSL* ssl;
int socket_fd;
public:
SSLClient() : ctx(nullptr), ssl(nullptr), socket_fd(-1) {
// OpenSSL ์ด๊ธฐํ
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
// SSL ์ปจํ
์คํธ ์์ฑ
const SSL_METHOD* method = TLS_client_method();
ctx = SSL_CTX_new(method);
if (!ctx) {
throw std::runtime_error("SSL ์ปจํ
์คํธ ์์ฑ ์คํจ");
}
// ์ต์ TLS ๋ฒ์ ๋ง ํ์ฉ
SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
}
~SSLClient() {
disconnect();
if (ctx) {
SSL_CTX_free(ctx);
}
}
bool connect(const std::string& hostname, int port) {
// ์์ผ ์์ฑ
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0) {
return false;
}
// ์๋ฒ ์ฃผ์ ์ค์
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
inet_pton(AF_INET, hostname.c_str(), &server_addr.sin_addr);
// TCP ์ฐ๊ฒฐ
if (::connect(socket_fd, (struct sockaddr*)&server_addr,
sizeof(server_addr)) < 0) {
close(socket_fd);
return false;
}
// SSL ๊ฐ์ฒด ์์ฑ
ssl = SSL_new(ctx);
SSL_set_fd(ssl, socket_fd);
// SSL ํธ๋์
ฐ์ดํฌ
if (SSL_connect(ssl) != 1) {
ERR_print_errors_fp(stderr);
return false;
}
// ์ธ์ฆ์ ๊ฒ์ฆ
X509* cert = SSL_get_peer_certificate(ssl);
if (cert) {
std::cout << "์๋ฒ ์ธ์ฆ์ ํ์ธ ์๋ฃ โ
" << std::endl;
X509_free(cert);
} else {
std::cout << "โ ๏ธ ์๋ฒ ์ธ์ฆ์ ์์" << std::endl;
}
return true;
}
std::string sendRequest(const std::string& request) {
if (!ssl) {
throw std::runtime_error("์ฐ๊ฒฐ๋์ง ์์");
}
// ๋ฐ์ดํฐ ์ ์ก
int bytes_sent = SSL_write(ssl, request.c_str(), request.length());
if (bytes_sent <= 0) {
throw std::runtime_error("์ ์ก ์คํจ");
}
// ์๋ต ์์
char buffer[4096];
std::string response;
int bytes_received;
while ((bytes_received = SSL_read(ssl, buffer, sizeof(buffer) - 1)) > 0) {
buffer[bytes_received] = '\0';
response += buffer;
// HTTP ์๋ต ์๋ฃ ํ์ธ
if (response.find("\r\n\r\n") != std::string::npos) {
break;
}
}
return response;
}
void disconnect() {
if (ssl) {
SSL_shutdown(ssl);
SSL_free(ssl);
ssl = nullptr;
}
if (socket_fd >= 0) {
close(socket_fd);
socket_fd = -1;
}
}
// ์ํธํ ์ค์ํธ ์ ๋ณด ์ถ๋ ฅ
void printCipherInfo() {
if (!ssl) return;
const char* cipher = SSL_get_cipher(ssl);
std::cout << "์ฌ์ฉ ์ค์ธ ์ํธํ: " << cipher << std::endl;
const char* version = SSL_get_version(ssl);
std::cout << "TLS ๋ฒ์ : " << version << std::endl;
}
};HTTPS ์์ฒญ ์์
int main() {
try {
SSLClient client;
// HTTPS ์๋ฒ์ ์ฐ๊ฒฐ
if (client.connect("93.184.216.34", 443)) { // example.com IP
std::cout << "โ
SSL ์ฐ๊ฒฐ ์ฑ๊ณต!" << std::endl;
client.printCipherInfo();
// HTTP GET ์์ฒญ
std::string request =
"GET / HTTP/1.1\r\n"
"Host: example.com\r\n"
"Connection: close\r\n"
"\r\n";
std::string response = client.sendRequest(request);
std::cout << "\n์๋ฒ ์๋ต:\n" << response << std::endl;
client.disconnect();
} else {
std::cout << "โ ์ฐ๊ฒฐ ์คํจ" << std::endl;
}
} catch (const std::exception& e) {
std::cerr << "์ค๋ฅ: " << e.what() << std::endl;
return 1;
}
return 0;
}2๏ธโฃ ์๋ฒ๊ฐ ์ธ์ฆ์์ ๊ณต๊ฐํค ์ ์ก (Server Hello)
3๏ธโฃ ํด๋ผ์ด์ธํธ๊ฐ ์ธ์ฆ์ ๊ฒ์ฆ
4๏ธโฃ ์ธ์ ํค ๊ตํ (๋น๋์นญํค ์ํธํ ์ฌ์ฉ)
5๏ธโฃ ์ดํ ํต์ ์ ์ธ์ ํค๋ก ๋์นญํค ์ํธํ ๐
๐ก๏ธ ์ค์ ๋ณด์ ํจํด๊ณผ ๋ฒ ์คํธ ํ๋ํฐ์ค
์ด๋ก ์ ์ถฉ๋ถํ ๋ฐฐ์ ์ผ๋, ์ด์ ์ค์ ์์ ๊ผญ ์ง์ผ์ผ ํ ๋ณด์ ์์น๋ค์ ์์๋ณด์! ๐ช
1. ์์ ํ ๋์ ์์ฑ
class SecureRandom {
public:
// ์ํธํ์ ์ผ๋ก ์์ ํ ๋์ ์์ฑ
static std::vector<unsigned char> generateBytes(size_t length) {
std::vector<unsigned char> buffer(length);
if (RAND_bytes(buffer.data(), length) != 1) {
throw std::runtime_error("๋์ ์์ฑ ์คํจ");
}
return buffer;
}
// ์์ ํ ๋๋ค ๋ฌธ์์ด ์์ฑ
static std::string generateString(size_t length) {
const char charset[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
auto random_bytes = generateBytes(length);
std::string result;
result.reserve(length);
for (unsigned char byte : random_bytes) {
result += charset[byte % (sizeof(charset) - 1)];
}
return result;
}
// UUID ์์ฑ
static std::string generateUUID() {
auto bytes = generateBytes(16);
// UUID ๋ฒ์ 4 ํ์์ผ๋ก ๋ณํ
bytes[6] = (bytes[6] & 0x0F) | 0x40; // ๋ฒ์ 4
bytes[8] = (bytes[8] & 0x3F) | 0x80; // Variant
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (size_t i = 0; i < 16; ++i) {
if (i == 4 || i == 6 || i == 8 || i == 10) {
ss << '-';
}
ss << std::setw(2) << static_cast<int>(bytes[i]);
}
return ss.str();
}
};- โ rand() ํจ์๋ก ์ํธํ ํค ์์ฑ
- โ ์๊ฐ ๊ธฐ๋ฐ ์๋๋ก ๋์ ์์ฑ
- โ ์์ธก ๊ฐ๋ฅํ ํจํด ์ฌ์ฉ
- โ ํญ์ RAND_bytes() ๊ฐ์ CSPRNG ์ฌ์ฉ!
2. ๋ฉ๋ชจ๋ฆฌ ๋ณด์
// ๋ฏผ๊ฐํ ๋ฐ์ดํฐ๋ฅผ ์์ ํ๊ฒ ๋ค๋ฃจ๋ ํด๋์ค
template<typename T>
class SecureMemory {
private:
T* data;
size_t size;
public:
SecureMemory(size_t n) : size(n) {
data = new T[n];
// ๋ฉ๋ชจ๋ฆฌ๋ฅผ 0์ผ๋ก ์ด๊ธฐํ
OPENSSL_cleanse(data, n * sizeof(T));
}
~SecureMemory() {
if (data) {
// ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์์ ํ๊ฒ ์ง์ (์ปดํ์ผ๋ฌ ์ต์ ํ ๋ฐฉ์ง)
OPENSSL_cleanse(data, size * sizeof(T));
delete[] data;
}
}
// ๋ณต์ฌ ๋ฐฉ์ง
SecureMemory(const SecureMemory&) = delete;
SecureMemory& operator=(const SecureMemory&) = delete;
T* get() { return data; }
const T* get() const { return data; }
size_t getSize() const { return size; }
};
// ์์ ํ ๋ฌธ์์ด ํด๋์ค
class SecureString {
private:
std::vector<char> data;
public:
SecureString(const std::string& str) {
data.assign(str.begin(), str.end());
data.push_back('\0');
}
~SecureString() {
// ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์์ ํ๊ฒ ์ง์
OPENSSL_cleanse(data.data(), data.size());
}
const char* c_str() const {
return data.data();
}
size_t length() const {
return data.size() - 1;
}
};3. ์๋ฌ ์ฒ๋ฆฌ์ ๋ก๊น
class CryptoLogger {
public:
enum class Level {
DEBUG,
INFO,
WARNING,
ERROR
};
static void log(Level level, const std::string& message) {
std::string level_str;
switch (level) {
case Level::DEBUG: level_str = "๐ DEBUG"; break;
case Level::INFO: level_str = "โน๏ธ INFO"; break;
case Level::WARNING: level_str = "โ ๏ธ WARNING"; break;
case Level::ERROR: level_str = "โ ERROR"; break;
}
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
std::cout << "[" << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S")
<< "] " << level_str << ": " << message << std::endl;
}
static void logOpenSSLError() {
unsigned long err;
while ((err = ERR_get_error()) != 0) {
char err_buf[256];
ERR_error_string_n(err, err_buf, sizeof(err_buf));
log(Level::ERROR, std::string("OpenSSL: ") + err_buf);
}
}
};4. ํค ๊ด๋ฆฌ ์์คํ
class KeyManager {
private:
std::map<std::string, SecureMemory<unsigned char>> keys;
std::string master_password;
public:
KeyManager(const std::string& password) : master_password(password) {}
// ํค ์ ์ฅ (์ํธํํด์ ์ ์ฅ)
void storeKey(const std::string& key_id,
const unsigned char* key_data,
size_t key_length) {
// ๋ง์คํฐ ํค๋ก ํค๋ฅผ ์ํธํ
auto encrypted = encryptWithMasterKey(key_data, key_length);
// ์์ ํ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ
keys.emplace(key_id, SecureMemory<unsigned char>(encrypted.size()));
memcpy(keys[key_id].get(), encrypted.data(), encrypted.size());
CryptoLogger::log(CryptoLogger::Level::INFO,
"ํค ์ ์ฅ ์๋ฃ: " + key_id);
}
// ํค ๋ก๋
std::vector<unsigned char> loadKey(const std::string& key_id) {
auto it = keys.find(key_id);
if (it == keys.end()) {
throw std::runtime_error("ํค๋ฅผ ์ฐพ์ ์ ์์: " + key_id);
}
// ๋ณตํธํํด์ ๋ฐํ
return decryptWithMasterKey(it->second.get(), it->second.getSize());
}
// ํค ์ญ์
void deleteKey(const std::string& key_id) {
keys.erase(key_id);
CryptoLogger::log(CryptoLogger::Level::INFO,
"ํค ์ญ์ ์๋ฃ: " + key_id);
}
// ํค ํ์ (์ฃผ๊ธฐ์ ์ผ๋ก ํค๋ฅผ ๋ณ๊ฒฝ)
void rotateKey(const std::string& key_id) {
auto new_key = SecureRandom::generateBytes(32);
storeKey(key_id, new_key.data(), new_key.size());
CryptoLogger::log(CryptoLogger::Level::INFO,
"ํค ํ์ ์๋ฃ: " + key_id);
}
private:
std::vector<unsigned char> encryptWithMasterKey(
const unsigned char* data, size_t length) {
// ๋ง์คํฐ ํจ์ค์๋๋ก ํค ์ ๋
unsigned char key[32];
unsigned char salt[16];
RAND_bytes(salt, sizeof(salt));
PKCS5_PBKDF2_HMAC(
master_password.c_str(), master_password.length(),
salt, sizeof(salt),
100000,
EVP_sha256(),
sizeof(key),
key
);
// AES๋ก ์ํธํ
// (๊ตฌํ ์๋ต - ์์ AESCrypto ํด๋์ค ํ์ฉ)
std::vector<unsigned char> result;
// ... ์ํธํ ๋ก์ง
return result;
}
std::vector<unsigned char> decryptWithMasterKey(
const unsigned char* data, size_t length) {
// ๋ณตํธํ ๋ก์ง
std::vector<unsigned char> result;
// ... ๋ณตํธํ ๋ก์ง
return result;
}
};
๐จ ์ค์ ํ๋ก์ ํธ: ํ์ผ ์ํธํ ๋๊ตฌ
์ด์ ๋ฐฐ์ด ๊ฑธ ๋ค ํฉ์ณ์ ์ค์ฉ์ ์ธ ํ์ผ ์ํธํ ๋๊ตฌ๋ฅผ ๋ง๋ค์ด๋ณด์! ๐
#include <fstream>
class FileEncryptor {
private:
AESCrypto crypto;
public:
FileEncryptor(const std::string& password) {
// ํจ์ค์๋๋ก ํค ์์ฑ
unsigned char key[32];
unsigned char iv[16];
PKCS5_PBKDF2_HMAC(
password.c_str(), password.length(),
reinterpret_cast<const unsigned char*>("salt1234"), // ์ค์ ๋ก๋ ๋๋ค salt ์ฌ์ฉ
8,
100000,
EVP_sha256(),
sizeof(key),
key
);
RAND_bytes(iv, sizeof(iv));
crypto = AESCrypto(key, iv);
}
bool encryptFile(const std::string& input_path,
const std::string& output_path) {
try {
// ํ์ผ ์ฝ๊ธฐ
std::ifstream input(input_path, std::ios::binary);
if (!input) {
CryptoLogger::log(CryptoLogger::Level::ERROR,
"ํ์ผ ์ด๊ธฐ ์คํจ: " + input_path);
return false;
}
// ํ์ผ ๋ด์ฉ ์ฝ๊ธฐ
std::string content(
(std::istreambuf_iterator<char>(input)),
std::istreambuf_iterator<char>()
);
input.close();
CryptoLogger::log(CryptoLogger::Level::INFO,
"ํ์ผ ํฌ๊ธฐ: " + std::to_string(content.size()) + " bytes");
// ์ํธํ
auto encrypted = crypto.encrypt(content);
// ์ํธํ๋ ๋ฐ์ดํฐ ์ ์ฅ
std::ofstream output(output_path, std::ios::binary);
if (!output) {
CryptoLogger::log(CryptoLogger::Level::ERROR,
"์ถ๋ ฅ ํ์ผ ์์ฑ ์คํจ: " + output_path);
return false;
}
output.write(reinterpret_cast<const char*>(encrypted.data()),
encrypted.size());
output.close();
CryptoLogger::log(CryptoLogger::Level::INFO,
"โ
์ํธํ ์๋ฃ: " + output_path);
return true;
} catch (const std::exception& e) {
CryptoLogger::log(CryptoLogger::Level::ERROR,
std::string("์ํธํ ์ค ์ค๋ฅ: ") + e.what());
return false;
}
}
bool decryptFile(const std::string& input_path,
const std::string& output_path) {
try {
// ์ํธํ๋ ํ์ผ ์ฝ๊ธฐ
std::ifstream input(input_path, std::ios::binary);
if (!input) {
CryptoLogger::log(CryptoLogger::Level::ERROR,
"ํ์ผ ์ด๊ธฐ ์คํจ: " + input_path);
return false;
}
std::vector<unsigned char> encrypted(
(std::istreambuf_iterator<char>(input)),
std::istreambuf_iterator<char>()
);
input.close();
// ๋ณตํธํ
std::string decrypted = crypto.decrypt(encrypted);
// ๋ณตํธํ๋ ๋ฐ์ดํฐ ์ ์ฅ
std::ofstream output(output_path, std::ios::binary);
if (!output) {
CryptoLogger::log(CryptoLogger::Level::ERROR,
"์ถ๋ ฅ ํ์ผ ์์ฑ ์คํจ: " + output_path);
return false;
}
output.write(decrypted.c_str(), decrypted.size());
output.close();
CryptoLogger::log(CryptoLogger::Level::INFO,
"โ
๋ณตํธํ ์๋ฃ: " + output_path);
return true;
} catch (const std::exception& e) {
CryptoLogger::log(CryptoLogger::Level::ERROR,
std::string("๋ณตํธํ ์ค ์ค๋ฅ: ") + e.what());
return false;
}
}
// ํ์ผ ๋ฌด๊ฒฐ์ฑ ๊ฒ์ฆ
std::string calculateFileHash(const std::string& file_path) {
std::ifstream file(file_path, std::ios::binary);
if (!file) {
throw std::runtime_error("ํ์ผ ์ด๊ธฐ ์คํจ");
}
std::string content(
(std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>()
);
return HashUtil::sha256(content);
}
};์ฌ์ฉ ์์
int main() {
std::cout << "๐ ํ์ผ ์ํธํ ๋๊ตฌ" << std::endl;
std::cout << "===================" << std::endl << std::endl;
std::string password;
std::cout << "๋น๋ฐ๋ฒํธ ์
๋ ฅ: ";
std::getline(std::cin, password);
FileEncryptor encryptor(password);
// ์๋ณธ ํ์ผ ํด์ ๊ณ์ฐ
std::string original_hash = encryptor.calculateFileHash("secret.txt");
std::cout << "์๋ณธ ํ์ผ ํด์: " << original_hash << std::endl << std::endl;
// ์ํธํ
if (encryptor.encryptFile("secret.txt", "secret.txt.enc")) {
std::cout << "โ
ํ์ผ ์ํธํ ์ฑ๊ณต!" << std::endl << std::endl;
// ๋ณตํธํ
if (encryptor.decryptFile("secret.txt.enc", "secret_decrypted.txt")) {
std::cout << "โ
ํ์ผ ๋ณตํธํ ์ฑ๊ณต!" << std::endl << std::endl;
// ๋ฌด๊ฒฐ์ฑ ๊ฒ์ฆ
std::string decrypted_hash =
encryptor.calculateFileHash("secret_decrypted.txt");
std::cout << "๋ณตํธํ ํ์ผ ํด์: " << decrypted_hash << std::endl;
if (original_hash == decrypted_hash) {
std::cout << "โ
๋ฌด๊ฒฐ์ฑ ๊ฒ์ฆ ์ฑ๊ณต! ํ์ผ์ด ์๋ฒฝํ๊ฒ ๋ณต์๋์์ต๋๋ค."
<< std::endl;
} else {
std::cout << "โ ๋ฌด๊ฒฐ์ฑ ๊ฒ์ฆ ์คํจ! ํ์ผ์ด ์์๋์์ต๋๋ค."
<< std::endl;
}
}
}
return 0;
}
โก ์ฑ๋ฅ ์ต์ ํ ํ
๋ณด์๋ ์ค์ํ์ง๋ง ์ฑ๋ฅ๋ ๋ฌด์ํ ์ ์์ง! ์ค์ ์์ ์ธ ์ ์๋ ์ต์ ํ ๊ธฐ๋ฒ๋ค์ ์๋ ค์ค๊ฒ. ๐๏ธ
1. ๋์ฉ๋ ํ์ผ ์ฒ๋ฆฌ
class StreamEncryptor {
private:
static const size_t BUFFER_SIZE = 1024 * 1024; // 1MB ๋ฒํผ
public:
static bool encryptLargeFile(const std::string& input_path,
const std::string& output_path,
const unsigned char* key,
const unsigned char* iv) {
std::ifstream input(input_path, std::ios::binary);
std::ofstream output(output_path, std::ios::binary);
if (!input || !output) {
return false;
}
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key, iv);
std::vector<unsigned char> in_buffer(BUFFER_SIZE);
std::vector<unsigned char> out_buffer(BUFFER_SIZE + EVP_CIPHER_block_size(EVP_aes_256_cbc()));
int out_len;
// ์ฒญํฌ ๋จ์๋ก ์ํธํ
while (input.read(reinterpret_cast<char*>(in_buffer.data()), BUFFER_SIZE) || input.gcount() > 0) {
int bytes_read = input.gcount();
EVP_EncryptUpdate(ctx, out_buffer.data(), &out_len,
in_buffer.data(), bytes_read);
output.write(reinterpret_cast<char*>(out_buffer.data()), out_len);
}
// ์ต์ข
๋ธ๋ก
EVP_EncryptFinal_ex(ctx, out_buffer.data(), &out_len);
output.write(reinterpret_cast<char*>(out_buffer.data()), out_len);
EVP_CIPHER_CTX_free(ctx);
return true;
}
};2. ๋ฉํฐ์ค๋ ๋ฉ ํ์ฉ
#include <thread>
#include <future>
#include <queue>
#include <mutex>
class ParallelEncryptor {
private:
size_t num_threads;
public:
ParallelEncryptor(size_t threads = std::thread::hardware_concurrency())
: num_threads(threads) {}
std::vector<std::vector<unsigned char>> encryptMultipleFiles(
const std::vector<std::string>& file_paths,
const unsigned char* key,
const unsigned char* iv) {
std::vector<std::future<std::vector<unsigned char>>> futures;
for (const auto& path : file_paths) {
futures.push_back(
std::async(std::launch::async, [path, key, iv]() {
// ํ์ผ ์ฝ๊ธฐ
std::ifstream file(path, std::ios::binary);
std::string content(
(std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>()
);
// ์ํธํ
AESCrypto crypto(key, iv);
return crypto.encrypt(content);
})
);
}
// ๊ฒฐ๊ณผ ์์ง
std::vector<std::vector<unsigned char>> results;
for (auto& future : futures) {
results.push_back(future.get());
}
return results;
}
};โข AES-256: ์ฝ 1.2GB/s
โข RSA-2048 ์ํธํ: ์ฝ 1000 ops/s
โข RSA-2048 ๋ณตํธํ: ์ฝ 50 ops/s
โข SHA-256: ์ฝ 400MB/s
์ค์ ์ฑ๋ฅ์ CPU, ๋ฉ๋ชจ๋ฆฌ, ๋์คํฌ ์๋์ ๋ฐ๋ผ ๋ฌ๋ผ์ ธ!
3. ํ๋์จ์ด ๊ฐ์ ํ์ฉ
class HardwareAcceleration {
public:
static bool isAESNISupported() {
// CPU๊ฐ AES-NI๋ฅผ ์ง์ํ๋์ง ํ์ธ
#ifdef __x86_64__
unsigned int eax, ebx, ecx, edx;
__asm__ __volatile__(
"cpuid"
: "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
: "a"(1)
);
return (ecx & (1 << 25)) != 0; // AES-NI ๋นํธ ํ์ธ
#else
return false;
#endif
}
static void enableHardwareAcceleration() {
if (isAESNISupported()) {
std::cout << "โ
AES-NI ํ๋์จ์ด ๊ฐ์ ์ฌ์ฉ ๊ฐ๋ฅ!" << std::endl;
// OpenSSL์ ์๋์ผ๋ก AES-NI๋ฅผ ์ฌ์ฉํจ
} else {
std::cout << "โ ๏ธ AES-NI ๋ฏธ์ง์, ์ํํธ์จ์ด ๊ตฌํ ์ฌ์ฉ" << std::endl;
}
}
};
๐ ๋๋ฒ๊น ๊ณผ ํ ์คํ
์ํธํ ์ฝ๋๋ ๋ฒ๊ทธ๊ฐ ์์ผ๋ฉด ์ ๋ง ์ํํด. ์ฒ ์ ํ ํ ์คํธ๊ฐ ํ์์ผ! ๐งช
๋จ์ ํ ์คํธ ์์
#include <cassert>
class CryptoTests {
public:
static void runAllTests() {
std::cout << "๐งช ์ํธํ ํ
์คํธ ์์..." << std::endl << std::endl;
testAESEncryption();
testRSAEncryption();
testHashFunctions();
testPasswordHashing();
testRandomGeneration();
std::cout << std::endl << "โ
๋ชจ๋ ํ
์คํธ ํต๊ณผ!" << std::endl;
}
private:
static void testAESEncryption() {
std::cout << "ํ
์คํธ: AES ์ํธํ/๋ณตํธํ... ";
AESCrypto crypto;
std::string original = "Test message ํ
์คํธ ๋ฉ์์ง ๐";
auto encrypted = crypto.encrypt(original);
std::string decrypted = crypto.decrypt(encrypted);
assert(original == decrypted);
assert(encrypted.size() > 0);
std::cout << "โ
" << std::endl;
}
static void testRSAEncryption() {
std::cout << "ํ
์คํธ: RSA ์ํธํ/๋ณตํธํ... ";
RSACrypto rsa(2048);
std::string original = "Secret message";
auto encrypted = rsa.encryptPublic(original);
std::string decrypted = rsa.decryptPrivate(encrypted);
assert(original == decrypted);
std::cout << "โ
" << std::endl;
}
static void testHashFunctions() {
std::cout << "ํ
์คํธ: ํด์ ํจ์... ";
std::string input = "Hello World";
std::string hash1 = HashUtil::sha256(input);
std::string hash2 = HashUtil::sha256(input);
// ๊ฐ์ ์
๋ ฅ์ ๊ฐ์ ํด์
assert(hash1 == hash2);
// ๋ค๋ฅธ ์
๋ ฅ์ ๋ค๋ฅธ ํด์
std::string hash3 = HashUtil::sha256("Hello World!");
assert(hash1 != hash3);
// ํด์ ๊ธธ์ด ํ์ธ (SHA-256์ 64์ hex)
assert(hash1.length() == 64);
std::cout << "โ
" << std::endl;
}
static void testPasswordHashing() {
std::cout << "ํ
์คํธ: ๋น๋ฐ๋ฒํธ ํด์ฑ... ";
std::string password = "MySecurePassword123!";
auto hashed = PasswordManager::hashPassword(password);
// ์ฌ๋ฐ๋ฅธ ๋น๋ฐ๋ฒํธ ๊ฒ์ฆ
assert(PasswordManager::verifyPassword(password, hashed.hash, hashed.salt));
// ์๋ชป๋ ๋น๋ฐ๋ฒํธ ๊ฒ์ฆ
assert(!PasswordManager::verifyPassword("WrongPassword", hashed.hash, hashed.salt));
std::cout << "โ
" << std::endl;
}
static void testRandomGeneration() {
std::cout << "ํ
์คํธ: ๋์ ์์ฑ... ";
auto random1 = SecureRandom::generateBytes(32);
auto random2 = SecureRandom::generateBytes(32);
// ๋ ๋์๋ ๋ฌ๋ผ์ผ ํจ
assert(random1 != random2);
// ๊ธธ์ด ํ์ธ
assert(random1.size() == 32);
std::cout << "โ
" << std::endl;
}
};๋ณด์ ์ทจ์ฝ์ ์ฒดํฌ๋ฆฌ์คํธ
- โ ํค๊ฐ ํ๋์ฝ๋ฉ๋์ด ์์ง ์์๊ฐ?
- โ ๋ฏผ๊ฐํ ๋ฐ์ดํฐ๊ฐ ๋ก๊ทธ์ ๋จ์ง ์๋๊ฐ?
- โ ๋ฉ๋ชจ๋ฆฌ์์ ๋ฏผ๊ฐํ ๋ฐ์ดํฐ๋ฅผ ์ ๋๋ก ์ง์ฐ๋๊ฐ?
- โ ์๋ฌ ๋ฉ์์ง๊ฐ ๋๋ฌด ๋ง์ ์ ๋ณด๋ฅผ ๋ ธ์ถํ์ง ์๋๊ฐ?
- โ ํ์ด๋ฐ ๊ณต๊ฒฉ์ ์ทจ์ฝํ์ง ์์๊ฐ?
- โ ๋์ ์์ฑ์ด ์ํธํ์ ์ผ๋ก ์์ ํ๊ฐ?
- โ ์ค๋๋ ์ํธํ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ์ง ์๋๊ฐ?
- โ ์ธ์ฆ์ ๊ฒ์ฆ์ ์ ๋๋ก ํ๋๊ฐ?
๐ ์ค๋ฌด์์ ์์ฃผ ์ฐ์ด๋ ํจํด
์ด์ ์ค๋ฌด์์ ์ ๋ง ๋ง์ด ์ฐ์ด๋ ํจํด๋ค์ ์์๋ณด์. ์ด๊ฑธ ์๋ฉด ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ๋ณด์ ํ๋ก์ ํธ๋ฅผ ๋งก์์ ๋ ๋ฐ๋ก ์จ๋จน์ ์ ์์ด! ๐ผ
1. JWT (JSON Web Token) ๊ตฌํ
#include <nlohmann/json.hpp> // JSON ๋ผ์ด๋ธ๋ฌ๋ฆฌ
#include <openssl/hmac.h>
class JWTManager {
private:
std::string secret_key;
public:
JWTManager(const std::string& key) : secret_key(key) {}
std::string createToken(const nlohmann::json& payload, int expiry_hours = 24) {
// Header
nlohmann::json header = {
{"alg", "HS256"},
{"typ", "JWT"}
};
// Payload์ ๋ง๋ฃ ์๊ฐ ์ถ๊ฐ
auto now = std::chrono::system_clock::now();
auto expiry = now + std::chrono::hours(expiry_hours);
auto exp_time = std::chrono::system_clock::to_time_t(expiry);
nlohmann::json full_payload = payload;
full_payload["exp"] = exp_time;
full_payload["iat"] = std::chrono::system_clock::to_time_t(now);
// Base64 ์ธ์ฝ๋ฉ
std::string header_b64 = base64Encode(header.dump());
std::string payload_b64 = base64Encode(full_payload.dump());
// ์๋ช
์์ฑ
std::string data = header_b64 + "." + payload_b64;
std::string signature = createSignature(data);
return data + "." + signature;
}
bool verifyToken(const std::string& token, nlohmann::json& payload) {
auto parts = split(token, '.');
if (parts.size() != 3) {
return false;
}
// ์๋ช
๊ฒ์ฆ
std::string data = parts[0] + "." + parts[1];
std::string expected_signature = createSignature(data);
if (parts[2] != expected_signature) {
return false;
}
// Payload ๋์ฝ๋ฉ
std::string payload_str = base64Decode(parts[1]);
payload = nlohmann::json::parse(payload_str);
// ๋ง๋ฃ ์๊ฐ ํ์ธ
if (payload.contains("exp")) {
auto exp_time = payload["exp"].get<time_t>();
auto now = std::chrono::system_clock::to_time_t(
std::chrono::system_clock::now()
);
if (now > exp_time) {
return false; // ํ ํฐ ๋ง๋ฃ
}
}
return true;
}
private:
std::string createSignature(const std::string& data) {
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int hash_len;
HMAC(EVP_sha256(),
secret_key.c_str(), secret_key.length(),
reinterpret_cast<const unsigned char*>(data.c_str()), data.length(),
hash, &hash_len);
return base64Encode(std::string(reinterpret_cast<char*>(hash), hash_len));
}
std::string base64Encode(const std::string& input) {
BIO* bio = BIO_new(BIO_s_mem());
BIO* b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bio = BIO_push(b64, bio);
BIO_write(bio, input.c_str(), input.length());
BIO_flush(bio);
BUF_MEM* buffer;
BIO_get_mem_ptr(bio, &buffer);
std::string result(buffer->data, buffer->length);
BIO_free_all(bio);
// URL-safe Base64๋ก ๋ณํ
std::replace(result.begin(), result.end(), '+', '-');
std::replace(result.begin(), result.end(), '/', '_');
result.erase(std::remove(result.begin(), result.end(), '='), result.end());
return result;
}
std::string base64Decode(const std::string& input) {
std::string padded = input;
std::replace(padded.begin(), padded.end(), '-', '+');
std::replace(padded.begin(), padded.end(), '_', '/');
while (padded.length() % 4 != 0) {
padded += '=';
}
BIO* bio = BIO_new_mem_buf(padded.c_str(), padded.length());
BIO* b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bio = BIO_push(b64, bio);
std::vector<char> buffer(padded.length());
int decoded_length = BIO_read(bio, buffer.data(), padded.length());
BIO_free_all(bio);
return std::string(buffer.data(), decoded_length);
}
std::vector<std::string> split(const std::string& str, char delimiter) {
std::vector<std::string> tokens;
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
};2. API ์์ฒญ ์๋ช
class APISignature {
public:
static std::string signRequest(
const std::string& method,
const std::string& path,
const std::string& body,
const std::string& timestamp,
const std::string& secret_key) {
// ์๋ช
ํ ๋ฐ์ดํฐ ๊ตฌ์ฑ
std::string data = method + "\n" + path + "\n" + timestamp + "\n" + body;
// HMAC-SHA256์ผ๋ก ์๋ช
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int hash_len;
HMAC(EVP_sha256(),
secret_key.c_str(), secret_key.length(),
reinterpret_cast<const unsigned char*>(data.c_str()), data.length(),
hash, &hash_len);
// Base64 ์ธ์ฝ๋ฉ
return base64Encode(hash, hash_len);
}
static bool verifyRequest(
const std::string& method,
const std::string& path,
const std::string& body,
const std::string& timestamp,
const std::string& signature,
const std::string& secret_key,
int max_age_seconds = 300) {
// ํ์์คํฌํ ๊ฒ์ฆ (๋ฆฌํ๋ ์ด ๊ณต๊ฒฉ ๋ฐฉ์ง)
auto now = std::chrono::system_clock::now();
auto request_time = std::chrono::system_clock::from_time_t(std::stoll(timestamp));
auto age = std::chrono::duration_cast<std::chrono::seconds>(now - request_time);
if (age.count() > max_age_seconds) {
return false; // ์์ฒญ์ด ๋๋ฌด ์ค๋๋จ
}
// ์๋ช
๊ฒ์ฆ
std::string expected_signature = signRequest(method, path, body, timestamp, secret_key);
return constantTimeCompare(signature, expected_signature);
}
private:
static std::string base64Encode(const unsigned char* data, size_t len) {
// Base64 ์ธ์ฝ๋ฉ ๊ตฌํ
// ...
return "";
}
static bool constantTimeCompare(const std::string& a, const std::string& b) {
if (a.length() != b.length()) return false;
int result = 0;
for (size_t i = 0; i < a.length(); ++i) {
result |= a[i] ^ b[i];
}
return result == 0;
}
};3. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ํธํ
class DatabaseEncryption {
private:
AESCrypto crypto;
public:
DatabaseEncryption(const std::string& master_key) {
// ๋ง์คํฐ ํค๋ก AES ํค ์์ฑ
unsigned char key[32];
unsigned char iv[16];
PKCS5_PBKDF2_HMAC(
master_key.c_str(), master_key.length(),
reinterpret_cast<const unsigned char*>("db_salt_12345"),
13,
100000,
EVP_sha256(),
sizeof(key),
key
);
RAND_bytes(iv, sizeof(iv));
crypto = AESCrypto(key, iv);
}
// ๋ฏผ๊ฐํ ํ๋ ์ํธํ
std::string encryptField(const std::string& plaintext) {
auto encrypted = crypto.encrypt(plaintext);
// Base64๋ก ์ธ์ฝ๋ฉํด์ DB์ ์ ์ฅ ๊ฐ๋ฅํ๊ฒ
return base64Encode(encrypted);
}
// ๋ฏผ๊ฐํ ํ๋ ๋ณตํธํ
std::string decryptField(const std::string& ciphertext_b64) {
auto ciphertext = base64Decode(ciphertext_b64);
return crypto.decrypt(ciphertext);
}
// ๊ฒ์ ๊ฐ๋ฅํ ์ํธํ (๊ฒฐ์ ์ ์ํธํ)
std::string encryptSearchable(const std::string& plaintext) {
// ๊ฐ์ ์
๋ ฅ์ ํญ์ ๊ฐ์ ์ถ๋ ฅ (๊ฒ์ ๊ฐ๋ฅ)
unsigned char hash[32];
SHA256(reinterpret_cast<const unsigned char*>(plaintext.c_str()),
plaintext.length(), hash);
return bytesToHex(hash, sizeof(hash));
}
private:
std::string base64Encode(const std::vector<unsigned char>& data) {
// Base64 ์ธ์ฝ๋ฉ
// ...
return "";
}
std::vector<unsigned char> base64Decode(const std::string& data) {
// Base64 ๋์ฝ๋ฉ
// ...
return {};
}
std::string bytesToHex(const unsigned char* data, size_t len) {
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (size_t i = 0; i < len; ++i) {
ss << std::setw(2) << static_cast<int>(data[i]);
}
return ss.str();
}
};
๐ ์ถ๊ฐ ํ์ต ๋ฆฌ์์ค
์ฌ๊ธฐ๊น์ง ์๋ค๋ฉด ์ ๋ง ๋๋จํด! ๐ ํ์ง๋ง ๋ณด์์ ๋์์ด ๋ฐฐ์์ผ ํ๋ ๋ถ์ผ์ผ. ๊ณ์ ๊ณต๋ถํ ์ ์๋ ๋ฆฌ์์ค๋ค์ ์ ๋ฆฌํด์ค๊ฒ.
| ๋ถ์ผ | ์ถ์ฒ ๋ฆฌ์์ค | ๋์ด๋ |
|---|---|---|
| OpenSSL ๊ณต์ ๋ฌธ์ | https://www.openssl.org/docs/ | โญโญโญ |
| ์ํธํ ๊ธฐ์ด | "Applied Cryptography" by Bruce Schneier | โญโญโญโญ |
| C++ ๋ณด์ ์ฝ๋ฉ | CERT C++ Coding Standard | โญโญโญ |
| ์ค์ ์์ | GitHub์ OpenSSL ์์ ํ๋ก์ ํธ๋ค | โญโญ |
| ๋ณด์ ์ทจ์ฝ์ | OWASP Top 10 | โญโญ |
| ์ต์ ํธ๋ ๋ | Crypto Stack Exchange | โญโญโญโญ |
2๏ธโฃ OpenSSL ์ค์น ๋ฐ ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
3๏ธโฃ ๊ฐ๋จํ ์ํธํ/๋ณตํธํ ํ๋ก๊ทธ๋จ ์์ฑ
4๏ธโฃ SSL/TLS ํต์ ๊ตฌํ
5๏ธโฃ ์ค์ ํ๋ก์ ํธ (ํ์ผ ์ํธํ, API ๋ณด์ ๋ฑ)
6๏ธโฃ ์ฑ๋ฅ ์ต์ ํ ๋ฐ ๋ณด์ ๊ฐํ
7๏ธโฃ ์ต์ ๋ณด์ ํธ๋ ๋ ํ์ต (์์ ๋ด์ฑ ์ํธ, ๋ํ ์ํธ ๋ฑ)
๐ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์, ์ฌ๊ธฐ๊น์ง ์ ๋ง ๊ธด ์ฌ์ ์ด์์ด! ๐ ์ฐ๋ฆฌ๊ฐ ํจ๊ป ๋ฐฐ์ด ๋ด์ฉ์ ์ ๋ฆฌํด๋ณด์:
- โ OpenSSL ์ค์น ๋ฐ ํ๊ฒฝ ์ค์
- โ AES ๋์นญํค ์ํธํ ๊ตฌํ
- โ RSA ๋น๋์นญํค ์ํธํ์ ๋์งํธ ์๋ช
- โ ํด์ ํจ์์ ๋น๋ฐ๋ฒํธ ์์ ํ๊ฒ ์ ์ฅํ๊ธฐ
- โ SSL/TLS ํต์ ๊ตฌํ
- โ ์ค์ ํ์ผ ์ํธํ ๋๊ตฌ ์ ์
- โ ์ฑ๋ฅ ์ต์ ํ ๊ธฐ๋ฒ
- โ JWT, API ์๋ช ๋ฑ ์ค๋ฌด ํจํด
- โ ๋ณด์ ํ ์คํ ๊ณผ ๋๋ฒ๊น
์ด์ ๋๋ C++๋ก ๋ณด์ ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์๋ ๋ฅ๋ ฅ์ ๊ฐ์ท์ด! ๐ช ๋ฌผ๋ก ์ฒ์์๋ ์ด๋ ค์ธ ์ ์์ง๋ง, ํ๋์ฉ ๋ฐ๋ผํ๋ค ๋ณด๋ฉด ๊ธ๋ฐฉ ์ต์ํด์ง ๊ฑฐ์ผ.
๋ณด์์ ํ ๋ฒ ๋ฐฐ์ฐ๊ณ ๋๋๋ ๊ฒ ์๋๋ผ ๊ณ์ ์ ๋ฐ์ดํธํด์ผ ํ๋ ๋ถ์ผ์ผ. ์๋ก์ด ์ทจ์ฝ์ ์ด ๋ฐ๊ฒฌ๋๊ณ , ์๋ก์ด ๊ณต๊ฒฉ ๊ธฐ๋ฒ์ด ๋์ค๊ณ , ์๋ก์ด ์ํธํ ์๊ณ ๋ฆฌ์ฆ์ด ๊ฐ๋ฐ๋๊ฑฐ๋ . ๊ทธ๋ฌ๋๊น ํญ์ ์ต์ ์ ๋ณด๋ฅผ ์ฒดํฌํ๊ณ ๊ณต๋ถํ๋ ์ต๊ด์ ๋ค์ด์! ๐
ํน์ ๋ณด์ ๊ด๋ จ ํ๋ก์ ํธ๊ฐ ํ์ํ๊ฑฐ๋, ๋ ๊น์ด ๋ฐฐ์ฐ๊ณ ์ถ๋ค๋ฉด ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ์ ๋ฌธ๊ฐ๋ค๊ณผ ์ํตํ๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ด์ผ. ์ค์ ๊ฒฝํ์ด ์์ด๋ฉด ๋๋ ๋ค๋ฅธ ์ฌ๋๋ค์ ๋์์ค ์ ์๋ ์ ๋ฌธ๊ฐ๊ฐ ๋ ๊ฑฐ์ผ! ๐
์, ์ด์ ์ง์ ์ฝ๋๋ฅผ ์์ฑํด๋ณด๋ฉด์ ์ค๋ ฅ์ ํค์๋ด! ๋งํ๋ ๋ถ๋ถ์ด ์์ผ๋ฉด ๊ณต์ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ๊ณ , ์ปค๋ฎค๋ํฐ์ ์ง๋ฌธํ๊ณ , ๊ณ์ ์๋ํด๋ณด๋ ๊ฑฐ์ผ. ์คํจ๋ ๋ฐฐ์์ ๊ณผ์ ์ด๋๊น ๋๋ ค์ํ์ง ๋ง! ๐ช
ํ์ดํ ! ๋๋ผ๋ฉด ์ถฉ๋ถํ ํ ์ ์์ด! ๐ฅ
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.