컴퓨터+a/설치, 오류

OpenSSL을 이용한 Triple-DES cbc mode 구현하기/Visual C++ 6.0

hiiambk 2018. 6. 5. 11:38

1. OpenSSL 다운로드

https://slproweb.com/products/Win32OpenSSL.html


컴퓨터가 64bit이더라도 Visual c++에서 windows console 32로 코딩하므로 32bit 설치할 것

기본 디렉토리 C:\OpenSSL-Win32


2. Tools - Option - Directories 

Show directories for : Include files에 추가 - C:\OpenSSL-Win32\include    

Library files에 추가 - C:\OpenSSL-Win32\lib\VC



3. Project - Setting - Link

Object/library modules에 추가 - libcrypto32MD.lib libcrypto32MDd.lib libcrypto32MT.lib libcrypto32MTd.lib libssl32MD.lib libssl32MDd.lib libssl32MT.lib libssl32MTd.lib



4. 예제 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/des.h>
 
/* Triple DES key for Encryption and Decryption */
DES_cblock Key1 = { 0x110x110x110x110x110x110x110x11 };
DES_cblock Key2 = { 0x220x220x220x220x220x220x220x22 };
DES_cblock Key3 = { 0x330x330x330x330x330x330x330x33 };
DES_key_schedule SchKey1,SchKey2,SchKey3;
 
/* Print Encrypted and Decrypted data packets */
void print_data(const char *tittle, const void* data, int len);
 
int main()
{
    /* Input data to encrypt */
    unsigned char input_data[] = {0x010x020x030x040x05};
    
    /* Init vector */
    DES_cblock iv = { 0x000x000x000x000x000x000x000x00 };
    DES_set_odd_parity(&iv);
    
    /* Check for Weak key generation */
    if ( -2 == (DES_set_key_checked(&Key1, &SchKey1) || DES_set_key_checked(&Key2, &SchKey2) || DES_set_key_checked(&Key3, &SchKey3)))
    {
        printf(" Weak key ....\n");
        return 1;
    }
    
    /* Buffers for Encryption and Decryption */
    unsigned char* cipher[sizeof(input_data)];
    unsigned char* text[sizeof(input_data)];
    
    /* Triple-DES CBC Encryption */
    DES_ede3_cbc_encrypt( (unsigned char*)input_data, (unsigned char*)cipher, sizeof(input_data), &SchKey1, &SchKey2, &SchKey3,&iv, DES_ENCRYPT);
    
    /* Triple-DES CBC Decryption */
    memset(iv,0,sizeof(DES_cblock)); // You need to start with the same iv value
    DES_set_odd_parity(&iv);
    DES_ede3_cbc_encrypt( (unsigned char*)cipher, (unsigned char*)text, sizeof(input_data), &SchKey1, &SchKey2, &SchKey3,&iv,DES_DECRYPT);
    
    /* Printing and Verifying */
    print_data("\n Original ",input_data,sizeof(input_data));
    print_data("\n Encrypted",cipher,sizeof(input_data));
    print_data("\n Decrypted",text,sizeof(input_data));
    
    return 0;
}
void print_data(const char *tittle, const void* data, int len)
{
    printf("%s : ",tittle);
    const unsigned char * p = (const unsigned char*)data;
    int i = 0;
    
    for (; i<len;++i)
        printf("%02X "*p++);
    
    printf("\n");
}
cs


728x90