Post List

2015년 1월 23일 금요일

OpenSSL을 Visual Stidio 2010에 적용하기

먼저 OpenSSL을 Visual Studio 2010을 이용하여 빌드하는 것은 아래 포스팅을 참조하면 된다.

OpenSSL 빌드 (Visual Studio 2010 기준)


그럼 Visual Studio 2010에 OpenSSL을 적용시켜 보자.

1. Project 생성

  간단하게 Console Empty Project로 생성하였다.



2. 해당 Poject 폴더 아래 OpenSSL 이라는 폴더로 빌드된 OpenSSL 파일들을 복사한다. 
   폴더구조는 빌드된 그대로 옮겼다.
   해당 Project 뿐만 아니라 계속해서 쓸려면 다른 폴더에 저장한뒤 Proejct 속성에서 경로만 달리하면 된다.

3. Project 속성을 설정한다.

    필자의 경우는 Project에서 직접 수정하지 않고 Property Sheet를 생성하여서 거기에 설정하였다. 그럴 경우 다른 Project 생성시 해당 Sheet만 적용시켜 주면 되므로 편리하다.



이름은 OpenSSL.props 로 저장하였다.
그런뒤 아래와 같이 속성을 수정하였다.

  - C/C++ -> General -> Additional Include Directories : OpenSSL/include
  - Linker -> General -> Additional Library Directories : OpenSSL/lib
  - Linker -> Input -> Additional Dependencies : libeay32.lib

4. 예제 소스 작성

    아무 이름으로 cpp파일을 하나 생성하여 project에 추가 한 뒤 아래 code를 실행해보면 된다.

  - SHA256 예제

#include <iostream> 
#include <sstream> 
#include <string> 
#include <iomanip> 
#include "openssl/sha.h"

#define _CRT_SECURE_NO_WARNINGS 

std::string sha256( const std::string str
        unsigned char hash[SHA256_DIGEST_LENGTH]; 
        SHA256_CTX sha256; 
        SHA256_Init(&sha256); 
        SHA256_Update(&sha256, str.c_str(), str.size());

        SHA256_Final(hash, &sha256);

        std::stringstream ss;
        for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
               ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; 

        return ss.str(); 

void sha256_hash_string (unsigned char hash[SHA256_DIGEST_LENGTH], char outputBuffer[65]) 
        for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) 
               sprintf(outputBuffer + (i * 2), "%02x", hash[i]); 

        outputBuffer[64] = 0; 

int sha256_file(char* path, char output[65]) 
        FILE* file = fopen(path, "rb"); 
        if(!file) return -1; 

        unsigned  char hash[SHA256_DIGEST_LENGTH]; 
        SHA256_CTX sha256; 
        SHA256_Init(&sha256); 
        const int bufSize = 32768; 
        char* buffer = new char[ bufSize ]; 
        int bytesRead = 0; 
        if(!buffer) return -1; 

        while((bytesRead = fread(buffer, 1, bufSize, file))) 
               SHA256_Update(&sha256, buffer, bytesRead); 

        SHA256_Final(hash, &sha256); 

        sha256_hash_string(hash, output); 
        fclose(file); 
        delete [] buffer; 
        return 0; 
}

int main() 
        using namespace std;

        // hash a string 
        string str = "Test Hash String = \"LUNA STAR\"";
        cout << "Palin Text : " << str << endl;
        cout << sha256(str) << endl << endl;

        // hash a file 
        cout << "File Hash : " << endl; 
        char calc_hash[65]; 
        sha256_file( "Test.txt", calc_hash );
        cout << calc_hash << endl;

        getchar();
        return 0; 
}


  - Triple DES 예제

#include <iostream> 
#include <string> 
#include "openssl/des.h"

#define _CRT_SECURE_NO_WARNINGS 

DES_key_schedule K1, K2, K3;

void encryptDES(std::string &arg, int enc)
{
        char out_[128];
        memset(out_, 0, sizeof(out_));
        for (int i = 0; i < ((arg.length() + 7) / 8 * 8); i +=8)
               DES_ecb3_encrypt((C_Block *)(arg.c_str() + i),(C_Block *)(out_ + i), &K1, &K2, &K3, enc);

        arg = out_;
}

void setDES()
{
        DES_set_key((C_Block*)"12345678", &K1);
        DES_set_key((C_Block*)"QWERTYUI", &K1);
        DES_set_key((C_Block*)"qwertyui", &K1);
}

void main() 
        using namespace std;
        setDES();

        std::string test("testQWERTYU");

        encryptDES(test, DES_ENCRYPT);
        cout << "ENCRYPT : "<< test << endl;

        encryptDES(test, DES_DECRYPT);
        cout << "DECRYPT : "<< test << endl;

        getchar();
}

댓글 없음:

댓글 쓰기