- Create a Win32 console application. I called my demo application “OpenSSLCryptoSample”. Btw, I am using VS C++ 2010 express in this tutorial.
- Make sure the following settings are setup in the project property pages: [C/C++ -> General -> Additional Include Directories] value: OpenSSL’s include directory in your machine (e.g C:\openssl\include\) [Linker -> General -> Additional Library Directories] value: OpenSSL’s lib directory in your machine (e.g C:\openssl\lib\) [Linker -> Input -> Additional Dependencies] value: libeay32.lib
- Download and add the following header file into the project. This header file will consists of the core function to encrypt and decrypt the given string. Following is the code snippet of the header file.
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83/* osslcrypto.cpp
*
* Copyright (c) 2012 askyb.com
*
* All rights reserved
*/
#include <openssl/evp.h>
/* Please choose an encryption mode in the following defines value.
Only 1 encryption mode is allow at a time. Blowfish CBC mode is
the default encryption in this file.
*/
// BlowFish
#define MY_CIPHER_MODE EVP_bf_cbc() // Blowfish CBC mode
//#define MY_CIPHER_MODE EVP_bf_ecb() // Blowfish ECB mode
// DES
//#define MY_CIPHER_MODE EVP_des_cbc() // DES CBC mode
//#define MY_CIPHER_MODE EVP_des_ecb() // DES ECB mode
//#define MY_CIPHER_MODE EVP_des_ede() // DES EDE mode
//#define MY_CIPHER_MODE EVP_des_ede3() // DES EDE3 mode
// RC2
//#define MY_CIPHER_MODE EVP_rc2_cbc() // RC2 CBC mode
//#define MY_CIPHER_MODE EVP_rc2_ecb() // RC2 ECB mode
// RC4
//#define MY_CIPHER_MODE EVP_rc4() // RC4 mode
//#define MY_CIPHER_MODE EVP_rc4_40() // RC4 40 mode
bool do_encrypt(const char *in, unsigned char *out, int *outlen, unsigned char *key, unsigned char *iv)
{
int buflen, tmplen;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_EncryptInit_ex(&ctx, MY_CIPHER_MODE, NULL, key, iv);
if(!EVP_EncryptUpdate(&ctx, out, &buflen, (unsigned char*)in, strlen(in)))
{
return false;
}
if(!EVP_EncryptFinal_ex(&ctx, out + buflen, &tmplen))
{
return false;
}
buflen += tmplen;
*outlen = buflen;
EVP_CIPHER_CTX_cleanup(&ctx);
return true;
}
bool do_decrypt(const unsigned char *in, unsigned char *out, int inlen, unsigned char *key, unsigned char *iv)
{
int buflen, tmplen;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_DecryptInit_ex(&ctx, MY_CIPHER_MODE, NULL, key, iv);
if(!EVP_DecryptUpdate(&ctx, out, &buflen, in, inlen))
{
return false;
}
if(!EVP_DecryptFinal_ex(&ctx, out + buflen, &tmplen))
{
return false;
}
int decryptedLength = buflen + tmplen;
out[decryptedLength] = '\0';
EVP_CIPHER_CTX_cleanup(&ctx);
return true;
} - Now enter the following code into your project main function:
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/* OpenSSLCryptoSample.cpp
*
* Copyright (c) 2012 askyb.com
*
* All rights reserved
*/
#include "stdafx.h"
#include <iostream>
#include <string>
#include "osslcrypto.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
unsigned char iv[] = {1,2,3,4,5,6,7,8};
cout << "askYB.com Encryption Example" << endl;
cout << "+++++++++++++++++++++++++++\n" << endl;
string mystr;
cout << "Enter string to encrypt: ";
getline (cin, mystr);
int outlen;
unsigned char pEncryptedStr[1024];
unsigned char pDecryptedStr[1024];
memset(pEncryptedStr, 0, sizeof(pEncryptedStr));
memset(pDecryptedStr, 0, sizeof(pDecryptedStr));
do_encrypt(mystr.c_str(), pEncryptedStr, &outlen, key, iv);
cout << "After encrypted: " << pEncryptedStr << endl;
cout << "\n\n--- Press ENTER to decrypt ---" << endl;
getchar();
do_decrypt(pEncryptedStr, pDecryptedStr, outlen, key, iv);
cout << "After decrypted: " << pDecryptedStr << endl;
getchar();
return 0;
} - The project may compile sucessfull now. BUT, if you try to run it, it will complaint that the application can’t start because LIBEAY32.DLL is missing. To address this issue, you may copy LIBEAY.DLL, which you can found it in the OpenSSL bin folder, to the project’s debug/release folder
- Compile and run the project now and you should see the following screenshot:
Man when i compile the code on QT
i get the following error:::
libeay32.lib(cryptlib.obj):-1: error: LNK2019: unresolved external symbol __imp__GetUserObjectInformationW@20 referenced in function _OPENSSL_isservice
libeay32.lib(cryptlib.obj):-1: error: LNK2019: unresolved external symbol __imp__GetProcessWindowStation@0 referenced in function _OPENSSL_isservice
libeay32.lib(cryptlib.obj):-1: error: LNK2019: unresolved external symbol __imp__GetDesktopWindow@0 referenced in function _OPENSSL_isservice
libeay32.lib(cryptlib.obj):-1: error: LNK2019: unresolved external symbol __imp__MessageBoxA@16 referenced in function _OPENSSL_showfatal
libeay32.lib(cryptlib.obj):-1: error: LNK2019: unresolved external symbol __imp__DeregisterEventSource@4 referenced in function _OPENSSL_showfatal
+++++++++++++++
++++++++++ MORE of the same error libeay32.lib(cryptlib.obj):-1: error: LNK2019 until finally ::
debug\OpenSSL.exe:-1: error: LNK1120: 17 unresolved externals
Check Step #2. Make sure that you include necessary files before compile the source code.
How can i convert this pEncryptedStr to base64?
u can convert it by using openssl base64 encode/decode or write your own base 64 encode/decode