์ฝ˜ํ…์ธ  ๋Œ€ํ‘œ ์ด๋ฏธ์ง€ - ๐Ÿ” C++ ์•”ํ˜ธํ™”์™€ OpenSSL๋กœ ๋ณด์•ˆ ๋งˆ์Šคํ„ฐํ•˜๊ธฐ

๐Ÿ” C++ ์•”ํ˜ธํ™”์™€ OpenSSL๋กœ ๋ณด์•ˆ ๋งˆ์Šคํ„ฐํ•˜๊ธฐ

์นœ๊ตฌ์ฒ˜๋Ÿผ ์‰ฝ๊ฒŒ ๋ฐฐ์šฐ๋Š” ์•”ํ˜ธํ™” ๊ธฐ์ˆ ์˜ ๋ชจ๋“  ๊ฒƒ

์•ผ, ์š”์ฆ˜ ์„ธ์ƒ์— ๋ณด์•ˆ ์—†์ด๋Š” ์•„๋ฌด๊ฒƒ๋„ ๋ชปํ•˜๋Š” ๊ฑฐ ์•Œ์ง€? ๐Ÿค” ์€ํ–‰ ์•ฑ ํ•˜๋‚˜ ์—ด์–ด๋„, ์นดํ†ก ํ•˜๋‚˜ ๋ณด๋‚ด๋„ ๋‹ค ์•”ํ˜ธํ™”๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ์–ด. ๊ทผ๋ฐ ์ด๊ฑธ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ? ๋ฐ”๋กœ C++๊ณผ OpenSSL์ด๋ผ๋Š” ๊ฐ•๋ ฅํ•œ ์กฐํ•ฉ์ด ํ•„์š”ํ•ด!


์˜ค๋Š˜์€ ๋„ˆ๋ž‘ ํ•จ๊ป˜ C++๋กœ ์•”ํ˜ธํ™” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํŒŒํ—ค์ณ๋ณผ ๊ฑฐ์•ผ. ๊ฑฑ์ • ๋งˆ, ์–ด๋ ต์ง€ ์•Š์•„. ๋งˆ์น˜ ๋ ˆ๊ณ  ๋ธ”๋ก ์กฐ๋ฆฝํ•˜๋“ฏ์ด ํ•˜๋‚˜์”ฉ ์Œ“์•„๊ฐ€๋ฉด ๋ผ! ๐Ÿงฑ

ํ‰๋ฌธ ๋ฐ์ดํ„ฐ OpenSSL ์•”ํ˜ธํ™” ์—”์ง„ ๐Ÿ”’ ์•”ํ˜ธํ™”๋œ ์•ˆ์ „ํ•œ ๋ฐ์ดํ„ฐ C++ ํ”„๋กœ๊ทธ๋žจ ๋ณด์•ˆ ๊ตฌํ˜„

๐ŸŽฏ ์™œ OpenSSL์ธ๊ฐ€?

๋จผ์ € OpenSSL์ด ๋ญ”์ง€๋ถ€ํ„ฐ ์•Œ์•„์•ผ๊ฒ ์ง€? OpenSSL์€ ์•”ํ˜ธํ™” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ ˆ๋Œ€ ๊ฐ•์ž์•ผ. ์ „ ์„ธ๊ณ„ ์ˆ˜๋งŽ์€ ์„œ๋ฒ„์™€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์–ด. ๋งˆ์น˜ ๋ณด์•ˆ๊ณ„์˜ ์Šค์œ„์Šค ์•„๋ฏธ ๋‚˜์ดํ”„ ๊ฐ™์€ ์กด์žฌ์ง€! ๐Ÿ”ช


๐Ÿ“Œ OpenSSL์˜ ํ•ต์‹ฌ ๊ธฐ๋Šฅ๋“ค โ€ข ๋Œ€์นญํ‚ค ์•”ํ˜ธํ™” (AES, DES, 3DES ๋“ฑ) ๐Ÿ”‘
โ€ข ๋น„๋Œ€์นญํ‚ค ์•”ํ˜ธํ™” (RSA, ECC ๋“ฑ) ๐Ÿ”
โ€ข ํ•ด์‹œ ํ•จ์ˆ˜ (SHA-256, SHA-512, MD5 ๋“ฑ) #๏ธโƒฃ
โ€ข ๋””์ง€ํ„ธ ์„œ๋ช… โœ๏ธ
โ€ข SSL/TLS ํ”„๋กœํ† ์ฝœ ์ง€์› ๐ŸŒ
โ€ข ์ธ์ฆ์„œ ๊ด€๋ฆฌ ๐Ÿ“œ

ํŠนํžˆ C++๊ณผ์˜ ๊ถํ•ฉ์ด ์ •๋ง ์ข‹์•„. ์„ฑ๋Šฅ๋„ ๋›ฐ์–ด๋‚˜๊ณ , ์ €์ˆ˜์ค€ ์ œ์–ด๊ฐ€ ๊ฐ€๋Šฅํ•ด์„œ ์„ธ๋ฐ€ํ•œ ๋ณด์•ˆ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๊ฑฐ๋“ . ๋งŒ์•ฝ ๋„ค๊ฐ€ ์žฌ๋Šฅ๋„ท ๊ฐ™์€ ํ”Œ๋žซํผ์—์„œ ๋ณด์•ˆ ๊ด€๋ จ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค๋ฉด, OpenSSL์€ ํ•„์ˆ˜ ๋„๊ตฌ๊ฐ€ ๋  ๊ฑฐ์•ผ!

๐Ÿ› ๏ธ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์„ธํŒ…ํ•˜๊ธฐ

์ž, ์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ ์‹œ์ž‘ํ•ด๋ณผ๊นŒ? ๋จผ์ € OpenSSL์„ ์„ค์น˜ํ•ด์•ผ ํ•ด. ์šด์˜์ฒด์ œ๋ณ„๋กœ ๋ฐฉ๋ฒ•์ด ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฅด๋‹ˆ๊นŒ ์ž˜ ๋”ฐ๋ผ์™€! ๐Ÿ˜Š


Windows ํ™˜๊ฒฝ

์œˆ๋„์šฐ ์‚ฌ์šฉ์ž๋ผ๋ฉด ์ด๋ ‡๊ฒŒ ํ•ด:


๐Ÿ’ก ์„ค์น˜ ๋ฐฉ๋ฒ• 1. 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);
    }
};

๐Ÿ’ก ์ฝ”๋“œ ์„ค๋ช… ์ด ํด๋ž˜์Šค๋Š” AES-256-CBC ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด. CBC(Cipher Block Chaining)๋Š” ๊ฐ ๋ธ”๋ก์ด ์ด์ „ ๋ธ”๋ก๊ณผ ์—ฐ๊ฒฐ๋˜์–ด ์•”ํ˜ธํ™”๋˜๋Š” ๋ฐฉ์‹์ด์•ผ. ๋” ์•ˆ์ „ํ•˜์ง€๋งŒ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๊ฐ€ ์–ด๋ ค์šด ๋‹จ์ ์ด ์žˆ์–ด. ๐Ÿ”—

์‹ค์ œ ์‚ฌ์šฉ ์˜ˆ์ œ

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;
}

โš ๏ธ ์ฃผ์˜์‚ฌํ•ญ ์‹ค์ œ ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ๋Š” ํ‚ค์™€ IV๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ๊ด€๋ฆฌํ•ด์•ผ ํ•ด! ํ•˜๋“œ์ฝ”๋”ฉํ•˜๊ฑฐ๋‚˜ ์†Œ์Šค์ฝ”๋“œ์— ํฌํ•จ์‹œํ‚ค๋ฉด ์ ˆ๋Œ€ ์•ˆ ๋ผ. ํ‚ค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ(KMS)์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, ํ™˜๊ฒฝ ๋ณ€์ˆ˜, ๋ณด์•ˆ ์ €์žฅ์†Œ ๋“ฑ์„ ํ™œ์šฉํ•ด์•ผ ํ•ด. ๐Ÿ”‘

๐Ÿ”“ ๋น„๋Œ€์นญํ‚ค ์•”ํ˜ธํ™” (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);
        }
    }
};

๐ŸŽ“ RSA ์•”ํ˜ธํ™”์˜ ํŠน์ง• โ€ข ๊ณต๊ฐœํ‚ค๋Š” ๋ˆ„๊ตฌ์—๊ฒŒ๋‚˜ ๊ณต๊ฐœ ๊ฐ€๋Šฅ ๐ŸŒ
โ€ข ๊ฐœ์ธํ‚ค๋Š” ์ ˆ๋Œ€ ๋น„๋ฐ€๋กœ ์œ ์ง€ ๐Ÿคซ
โ€ข ๋Œ€์นญํ‚ค๋ณด๋‹ค ๋А๋ฆฌ์ง€๋งŒ ํ‚ค ๋ฐฐํฌ๊ฐ€ ์•ˆ์ „ํ•จ ๐Ÿข
โ€ข ์ฃผ๋กœ ๋Œ€์นญํ‚ค๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ „๋‹ฌํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ ๐Ÿ“ฆ
โ€ข ๋””์ง€ํ„ธ ์„œ๋ช…์œผ๋กœ ๋ฌด๊ฒฐ์„ฑ๊ณผ ์ธ์ฆ ๋ณด์žฅ โœ…

๐Ÿ”จ ํ•ด์‹œ ํ•จ์ˆ˜ ํ™œ์šฉํ•˜๊ธฐ

ํ•ด์‹œ ํ•จ์ˆ˜๋Š” ์ž„์˜์˜ ๊ธธ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ ์ •๋œ ๊ธธ์ด์˜ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•ด. ๋น„๋ฐ€๋ฒˆํ˜ธ ์ €์žฅ, ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ ๊ฒ€์ฆ ๋“ฑ์— ํ•„์ˆ˜์ ์ด์•ผ! ๐ŸŽฏ


๋‹ค์–‘ํ•œ ํ•ด์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„

#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;
    }
};

๐Ÿ’ก PBKDF2๋ž€? Password-Based Key Derivation Function 2์˜ ์•ฝ์ž์•ผ. ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ํ•ด์‹ฑํ•ด์„œ ๋ฌด์ฐจ๋ณ„ ๋Œ€์ž… ๊ณต๊ฒฉ์„ ์–ด๋ ต๊ฒŒ ๋งŒ๋“œ๋Š” ๊ธฐ๋ฒ•์ด์•ผ. ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ๋†’์ผ์ˆ˜๋ก ์•ˆ์ „ํ•˜์ง€๋งŒ, ๋„ˆ๋ฌด ๋†’์œผ๋ฉด ์„œ๋ฒ„ ๋ถ€ํ•˜๊ฐ€ ์ปค์งˆ ์ˆ˜ ์žˆ์–ด. ๋ณดํ†ต 10๋งŒ~50๋งŒ ํšŒ ์ •๋„๊ฐ€ ์ ๋‹นํ•ด! ๐Ÿ”„
ํ•ด์‹œ ํ•จ์ˆ˜์˜ ์›๋ฆฌ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ "Hello World!" SHA-256 ํ•ด์‹œ ํ•จ์ˆ˜ ๊ณ ์ • ๊ธธ์ด ํ•ด์‹œ๊ฐ’ (256๋น„ํŠธ) a591a6d40bf420404... โœ… ๋‹จ๋ฐฉํ–ฅ ์—ญ๊ณ„์‚ฐ ๋ถˆ๊ฐ€๋Šฅ โœ… ๊ณ ์ • ๊ธธ์ด ํ•ญ์ƒ ๊ฐ™์€ ํฌ๊ธฐ โœ… ๊ฒฐ์ •์  ๊ฐ™์€ ์ž…๋ ฅ = ๊ฐ™์€ ์ถœ๋ ฅ

๐ŸŒ 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;
}

๐Ÿ” SSL/TLS ํ•ธ๋“œ์…ฐ์ดํฌ ๊ณผ์ • 1๏ธโƒฃ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ์—ฐ๊ฒฐ ์š”์ฒญ (Client Hello)
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;
}
ํŒŒ์ผ ์•”ํ˜ธํ™” ํ”„๋กœ์„ธ์Šค ๐Ÿ“„ ์›๋ณธ ํŒŒ์ผ secret.txt ๐Ÿ” AES-256 ์•”ํ˜ธํ™” ๐Ÿ”’ ์•”ํ˜ธํ™”๋œ ํŒŒ์ผ secret.txt.enc ๐Ÿ”“ AES-256 ๋ณตํ˜ธํ™” ๋ณต์› ์™„๋ฃŒ ๐Ÿ”‘ ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ธฐ๋ฐ˜ ํ‚ค ์œ ๋„ PBKDF2 + SHA-256 โœ… ๋ฌด๊ฒฐ์„ฑ ๊ฒ€์ฆ SHA-256 ํ•ด์‹œ ๋น„๊ต

โšก ์„ฑ๋Šฅ ์ตœ์ ํ™” ํŒ

๋ณด์•ˆ๋„ ์ค‘์š”ํ•˜์ง€๋งŒ ์„ฑ๋Šฅ๋„ ๋ฌด์‹œํ•  ์ˆ˜ ์—†์ง€! ์‹ค์ „์—์„œ ์“ธ ์ˆ˜ ์žˆ๋Š” ์ตœ์ ํ™” ๊ธฐ๋ฒ•๋“ค์„ ์•Œ๋ ค์ค„๊ฒŒ. ๐ŸŽ๏ธ


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-128: ์•ฝ 1.5GB/s
โ€ข 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 โญโญโญโญ

๐Ÿ’ก ํ•™์Šต ๋กœ๋“œ๋งต 1๏ธโƒฃ ๊ธฐ๋ณธ ์•”ํ˜ธํ™” ๊ฐœ๋… ์ดํ•ด (๋Œ€์นญํ‚ค, ๋น„๋Œ€์นญํ‚ค, ํ•ด์‹œ)
2๏ธโƒฃ OpenSSL ์„ค์น˜ ๋ฐ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•
3๏ธโƒฃ ๊ฐ„๋‹จํ•œ ์•”ํ˜ธํ™”/๋ณตํ˜ธํ™” ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ
4๏ธโƒฃ SSL/TLS ํ†ต์‹  ๊ตฌํ˜„
5๏ธโƒฃ ์‹ค์ „ ํ”„๋กœ์ ํŠธ (ํŒŒ์ผ ์•”ํ˜ธํ™”, API ๋ณด์•ˆ ๋“ฑ)
6๏ธโƒฃ ์„ฑ๋Šฅ ์ตœ์ ํ™” ๋ฐ ๋ณด์•ˆ ๊ฐ•ํ™”
7๏ธโƒฃ ์ตœ์‹  ๋ณด์•ˆ ํŠธ๋ Œ๋“œ ํ•™์Šต (์–‘์ž ๋‚ด์„ฑ ์•”ํ˜ธ, ๋™ํ˜• ์•”ํ˜ธ ๋“ฑ)

๐ŸŽ“ ๋งˆ๋ฌด๋ฆฌํ•˜๋ฉฐ

์™€, ์—ฌ๊ธฐ๊นŒ์ง€ ์ •๋ง ๊ธด ์—ฌ์ •์ด์—ˆ์–ด! ๐Ÿš€ ์šฐ๋ฆฌ๊ฐ€ ํ•จ๊ป˜ ๋ฐฐ์šด ๋‚ด์šฉ์„ ์ •๋ฆฌํ•ด๋ณด์ž:


๐ŸŽฏ ์˜ค๋Š˜ ๋ฐฐ์šด ํ•ต์‹ฌ ๋‚ด์šฉ
  • โœ… OpenSSL ์„ค์น˜ ๋ฐ ํ™˜๊ฒฝ ์„ค์ •
  • โœ… AES ๋Œ€์นญํ‚ค ์•”ํ˜ธํ™” ๊ตฌํ˜„
  • โœ… RSA ๋น„๋Œ€์นญํ‚ค ์•”ํ˜ธํ™”์™€ ๋””์ง€ํ„ธ ์„œ๋ช…
  • โœ… ํ•ด์‹œ ํ•จ์ˆ˜์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•ˆ์ „ํ•˜๊ฒŒ ์ €์žฅํ•˜๊ธฐ
  • โœ… SSL/TLS ํ†ต์‹  ๊ตฌํ˜„
  • โœ… ์‹ค์ „ ํŒŒ์ผ ์•”ํ˜ธํ™” ๋„๊ตฌ ์ œ์ž‘
  • โœ… ์„ฑ๋Šฅ ์ตœ์ ํ™” ๊ธฐ๋ฒ•
  • โœ… JWT, API ์„œ๋ช… ๋“ฑ ์‹ค๋ฌด ํŒจํ„ด
  • โœ… ๋ณด์•ˆ ํ…Œ์ŠคํŒ…๊ณผ ๋””๋ฒ„๊น…

์ด์ œ ๋„ˆ๋„ C++๋กœ ๋ณด์•ˆ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ์„ ๊ฐ–์ท„์–ด! ๐Ÿ’ช ๋ฌผ๋ก  ์ฒ˜์Œ์—๋Š” ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์ง€๋งŒ, ํ•˜๋‚˜์”ฉ ๋”ฐ๋ผํ•˜๋‹ค ๋ณด๋ฉด ๊ธˆ๋ฐฉ ์ต์ˆ™ํ•ด์งˆ ๊ฑฐ์•ผ.


๋ณด์•ˆ์€ ํ•œ ๋ฒˆ ๋ฐฐ์šฐ๊ณ  ๋๋‚˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ๊ณ„์† ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•˜๋Š” ๋ถ„์•ผ์•ผ. ์ƒˆ๋กœ์šด ์ทจ์•ฝ์ ์ด ๋ฐœ๊ฒฌ๋˜๊ณ , ์ƒˆ๋กœ์šด ๊ณต๊ฒฉ ๊ธฐ๋ฒ•์ด ๋‚˜์˜ค๊ณ , ์ƒˆ๋กœ์šด ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๊ฐœ๋ฐœ๋˜๊ฑฐ๋“ . ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ํ•ญ์ƒ ์ตœ์‹  ์ •๋ณด๋ฅผ ์ฒดํฌํ•˜๊ณ  ๊ณต๋ถ€ํ•˜๋Š” ์Šต๊ด€์„ ๋“ค์ด์ž! ๐Ÿ“–


ํ˜น์‹œ ๋ณด์•ˆ ๊ด€๋ จ ํ”„๋กœ์ ํŠธ๊ฐ€ ํ•„์š”ํ•˜๊ฑฐ๋‚˜, ๋” ๊นŠ์ด ๋ฐฐ์šฐ๊ณ  ์‹ถ๋‹ค๋ฉด ์žฌ๋Šฅ๋„ท ๊ฐ™์€ ํ”Œ๋žซํผ์—์„œ ์ „๋ฌธ๊ฐ€๋“ค๊ณผ ์†Œํ†ตํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ด์•ผ. ์‹ค์ „ ๊ฒฝํ—˜์ด ์Œ“์ด๋ฉด ๋„ˆ๋„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์„ ๋„์™€์ค„ ์ˆ˜ ์žˆ๋Š” ์ „๋ฌธ๊ฐ€๊ฐ€ ๋  ๊ฑฐ์•ผ! ๐ŸŒŸ


โš ๏ธ ๋งˆ์ง€๋ง‰ ๋‹น๋ถ€ ์•”ํ˜ธํ™”๋Š” ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ์ง€๋งŒ, ์ž˜๋ชป ์‚ฌ์šฉํ•˜๋ฉด ์˜คํžˆ๋ ค ์œ„ํ—˜ํ•  ์ˆ˜ ์žˆ์–ด. ํ•ญ์ƒ ๊ฒ€์ฆ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์ž์ฒด ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•˜์ง€ ๋งˆ. ๊ทธ๋ฆฌ๊ณ  ๋ณด์•ˆ์€ ๊ธฐ์ˆ ๋งŒ์˜ ๋ฌธ์ œ๊ฐ€ ์•„๋‹ˆ๋ผ ์‚ฌ๋žŒ๊ณผ ํ”„๋กœ์„ธ์Šค์˜ ๋ฌธ์ œ์ด๊ธฐ๋„ ํ•ด. ์•„๋ฌด๋ฆฌ ๊ฐ•๋ ฅํ•œ ์•”ํ˜ธํ™”๋ฅผ ์จ๋„ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํฌ์ŠคํŠธ์ž‡์— ์ ์–ด๋‘๋ฉด ์†Œ์šฉ์—†์ž–์•„? ๐Ÿ˜…

์ž, ์ด์ œ ์ง์ ‘ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๋ฉด์„œ ์‹ค๋ ฅ์„ ํ‚ค์›Œ๋ด! ๋ง‰ํžˆ๋Š” ๋ถ€๋ถ„์ด ์žˆ์œผ๋ฉด ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜๊ณ , ์ปค๋ฎค๋‹ˆํ‹ฐ์— ์งˆ๋ฌธํ•˜๊ณ , ๊ณ„์† ์‹œ๋„ํ•ด๋ณด๋Š” ๊ฑฐ์•ผ. ์‹คํŒจ๋Š” ๋ฐฐ์›€์˜ ๊ณผ์ •์ด๋‹ˆ๊นŒ ๋‘๋ ค์›Œํ•˜์ง€ ๋งˆ! ๐Ÿ’ช


ํ™”์ดํŒ…! ๋„ˆ๋ผ๋ฉด ์ถฉ๋ถ„ํžˆ ํ•  ์ˆ˜ ์žˆ์–ด! ๐Ÿ”ฅ

๋ณด์•ˆ์˜ ์—ฌ์ •์€ ๊ณ„์†๋ฉ๋‹ˆ๋‹ค Security is not a product, but a process ๐Ÿ” ๐Ÿ’ป ๐Ÿš€ ์•ˆ์ „ํ•œ ์ฝ”๋“œ ์ง€์†์ ์ธ ํ•™์Šต ์‹ค์ „ ๊ฒฝํ—˜
๋Œ“๊ธ€ ์ž‘์„ฑ

์ด ๊ธ€์— ๋Œ€ํ•œ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ƒ๊ฐ์„ ๋“ค๋ ค์ฃผ์„ธ์š”

๋Œ“๊ธ€ 0
๋Œ“๊ธ€์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ค‘์ž…๋‹ˆ๋‹ค.

์•„์ง ๋Œ“๊ธ€์ด ์—†์Šต๋‹ˆ๋‹ค.