암호화 : RSA, AES, BASE64

C++ 2012. 5. 20. 15:51




crypt_wrapper.cpp


crypt_wrapper.h


CryptoAPIHelper.cpp


CryptoAPIHelper.h





//  Base64 인코딩

string base64(const string& str)

{

vector<BYTE> binary(str.begin(), str.end());


// get size

DWORD base64_size = 0;

if (!CryptBinaryToStringA(&binary.at(0), binary.size(), CRYPT_STRING_BASE64, NULL, &base64_size))

return "fail to get base64 size";


// encode base64

vector<char> base64(base64_size, 0x00);

if (!CryptBinaryToStringA(&binary.at(0), binary.size(), CRYPT_STRING_BASE64, &base64.at(0), &base64_size))

return "fail to convert base64";


auto it=base64.begin();

while (it!=base64.end()) {

if (*it == 0x0A || *it == 0x0D) { // LFCR(\r\n) 제거

it = base64.erase(it);

} else {

++it;

}

}

string base64_string(&base64.at(0), &base64.at(0) + base64_size);

return base64_string;

}


//  Base64 디코딩

string unbase64(const string& str)

{

DWORD unbase64_flag = 0;

DWORD unbase64_size = 0;


// get size & flag

if (!CryptStringToBinaryA(&str.at(0), str.size(),

CRYPT_STRING_BASE64, NULL, &unbase64_size, NULL, &unbase64_flag))

return "fail to get unbase64 size";


// decode base64

vector<BYTE> unbase64_vec(unbase64_size, 0x00);

if (!CryptStringToBinaryA(&str.at(0), str.size(),

unbase64_flag, &unbase64_vec.at(0), &unbase64_size, NULL, &unbase64_flag))

return "fail to convert unbase64";


string unbase64_string(&unbase64_vec.at(0), &unbase64_vec.at(0) + unbase64_size);

return unbase64_string;

}


'C++' 카테고리의 다른 글

log4cxx 코딩으로 로거 생성 샘플  (0) 2012.05.20
C#의 tick 으로 변환  (0) 2012.05.20
URL encoding  (0) 2012.05.20
endian 변환민 serialize  (0) 2012.05.20
간단한 sync 소켓 사용  (0) 2012.05.20
WinINet, WinHTTP, post 전송  (0) 2012.05.20
간단한 유틸(util) 함수들  (0) 2012.05.20
간단한 file 조작  (0) 2012.05.20