I am trying to build an openssl simple program. Here is the complete code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/aes.h"
int main(int argc, char* argv[])
{
AES_KEY aesKey_;
unsigned char userKey_[16];
unsigned char in_[16];
unsigned char out_[16];
strcpy(userKey_,"0123456789123456");
strcpy(in_,"0123456789123456");
fprintf(stdout,"Original message: %s", in_);
AES_set_encrypt_key(userKey_, 128, &aesKey_);
AES_encrypt(in_, out_, &aesKey_);
AES_set_decrypt_key(userKey_, 128, &aesKey_);
AES_decrypt(out_, in_,&aesKey_);
fprintf(stdout,"Recovered Original message: %s", in_);
return 0;
}
I try to compile it using this command:
gcc -I/home/aleksei/openSSL0.9.8/include -o app -L . -lssl -lcrypto tema1.c
and I get this:
/tmp/ccT1XMid.o: In function `main':
tema1.c:(.text+0x8d): undefined reference to `AES_set_encrypt_key'
tema1.c:(.text+0xa7): undefined reference to `AES_encrypt'
tema1.c:(.text+0xbf): undefined reference to `AES_set_decrypt_key'
tema1.c:(.text+0xd9): undefined reference to `AES_decrypt'
collect2: ld returned 1 exit status
I am under Ubuntu 10.04. How can I get this to work ?
You may be trying to statically link, but the -L
option and -lcrypto
are looking for a file to link with dynamically. To statically link to a specific library, just specify your .a
file on the compiler command line after all your source files.
E.g.,
gcc -I/home/aleksei/openSSL0.9.8/include -o app tema1.c ./libcrypto.a
For those of you who have this same problem but are using Windows, Mingw and this OpenSSL for Windows (at this time: Win32 OpenSSL v1.0.2a). You need to link to libeay32.a
that is located in C:\OpenSSL-Win32\lib\MinGW\
(after installing OpenSSL).
In my case I am using CMake and the powerful CLion IDE, so I had to rename the library to libeay32.dll.a
because CMake wasn't locating the library. This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.1)
project(openssl_1_0_2a)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(C:\\OpenSSL-Win32\\include)
set(SOURCE_FILES main.cpp)
link_directories(C:\\OpenSSL-Win32\\lib\\MinGW)
add_executable(openssl_1_0_2a ${SOURCE_FILES})
target_link_libraries(openssl_1_0_2a eay32)
I made the test with this example (which is borrowed from this answer):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/aes.h"
int main(int argc, char* argv[])
{
AES_KEY aesKey_;
unsigned char userKey_[16];
unsigned char in_[16] = {0};
unsigned char out_[16] = {0};
strcpy((char *) userKey_,"0123456789123456");
strcpy((char *) in_,"0123456789123456");
fprintf(stdout,"Original message: %s\n", in_);
AES_set_encrypt_key(userKey_, 128, &aesKey_);
AES_encrypt(in_, out_, &aesKey_);
AES_set_decrypt_key(userKey_, 128, &aesKey_);
AES_decrypt(out_, in_,&aesKey_);
fprintf(stdout,"Recovered Original message: %s XXX \n", in_);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With