Skip to content

**** ****

for my bad memories..

Crypto++ 을 이용한 암호화/복호화 예제


출처: https://lyb1495.tistory.com/25

가끔 텍스트 파일을 감추고 싶을때.. 이용하면 좋음.

#include <iostream>
#include <iomanip>

#include "cryptopp/cryptlib.h"
#include "cryptopp/modes.h"
#include "cryptopp/aes.h"
#include "cryptopp/filters.h"
#include "cryptopp/base64.h"

void hex2byte(const char *in, uint len, byte *out)
{
	for (uint i = 0; i < len; i+=2) {
		char c0 = in[i+0];
		char c1 = in[i+1];
		byte c = (
			((c0 & 0x40 ? (c0 & 0x20 ? c0-0x57 : c0-0x37) : c0-0x30)<<4) |
			((c1 & 0x40 ? (c1 & 0x20 ? c1-0x57 : c1-0x37) : c1-0x30))
			);
		out[i/2] = c;
	}
}

int main(int argc, char* argv[]) {
	// 키 할당
	byte key[CryptoPP::AES::DEFAULT_KEYLENGTH];
	memset(key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
	char* rawKey="f4150d4a1ac5708c29e437749045a39a";
	hex2byte(rawKey, strlen(rawKey), key);

	// 초기벡터 할당
	byte iv[CryptoPP::AES::BLOCKSIZE];
	memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE );
	char* rawIv="86afc43868fea6abd40fbf6d5ed50905";
	hex2byte(rawIv, strlen(rawIv), iv);

	// 평문 할당
	std::string plaintext = "http://sopt.org/";
	std::string ciphertext;
	std::string base64encodedciphertext;
	std::string decryptedtext;
	std::string base64decryptedciphertext;

	// 평문 출력
	std::cout << "Plain Text (" << plaintext.size() <<
		" bytes)" << std::endl;
	std::cout << plaintext;
	std::cout << std::endl << std::endl;

	unsigned int plainTextLength = plaintext.length();

	// AES 암호화 수행
	CryptoPP::AES::Encryption 
		aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
	CryptoPP::CBC_Mode_ExternalCipher::Encryption 
		cbcEncryption(aesEncryption, iv);

	CryptoPP::StreamTransformationFilter 
		stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext));
	stfEncryptor.Put(reinterpret_cast<const unsigned="" char*="">
		(plaintext.c_str()), plainTextLength + 1);
	stfEncryptor.MessageEnd();

	// Base64 인코딩
	CryptoPP::StringSource(ciphertext, true,
		new CryptoPP::Base64Encoder(
		new CryptoPP::StringSink(base64encodedciphertext)
		) // Base64Encoder
		); // StringSource

	// Base64 인코딩 문자열 출력
	std::cout << "Cipher Text (" << base64encodedciphertext.size() 
		<< " bytes)" << std::endl;
	std::cout << "cipher : " << base64encodedciphertext << std::endl;
	std::cout << std::endl << std::endl;

	// Base64 디코딩
	CryptoPP::StringSource(base64encodedciphertext, true,
		new CryptoPP::Base64Decoder(
		new CryptoPP::StringSink( base64decryptedciphertext)
		) // Base64Encoder
		); // StringSource

	// AES 복호화
	CryptoPP::AES::Decryption aesDecryption(key, 
		CryptoPP::AES::DEFAULT_KEYLENGTH);
	CryptoPP::CBC_Mode_ExternalCipher::Decryption 
		cbcDecryption(aesDecryption, iv );

	CryptoPP::StreamTransformationFilter 
		stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedtext));
	stfDecryptor.Put( reinterpret_cast<const unsigned="" char*="">
		(base64decryptedciphertext.c_str()), base64decryptedciphertext.size());
	stfDecryptor.MessageEnd();

	// 복호화 문자열 출력
	std::cout << "Decrypted Text: " << std::endl;
	std::cout << decryptedtext;
	std::cout << std::endl << std::endl;
	return 0;
}

Categorized as: Programming


One Comment

  1. 무림고수 댓글:

    이 소스 안되는걸로 나오네요
    Crypto++ 헤드파일 버전 어떤거 쓰나요?
    최신은 8.2.0이네요

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다


이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.