Failed to link OpenSSL code

I am trying to create a simple openssl 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 am trying to compile it with this command:

gcc -I/home/aleksei/openSSL0.9.8/include -o app -L . -lssl -lcrypto tema1.c

and I get the following:

 /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'm on Ubuntu 10.04. How can I make this work?

+5
source share
2 answers

Perhaps you are trying to statically link, but the parameter is -Lalso -lcryptolooking for a file for dynamic linking. To statically refer to a specific library, simply specify your file .aon the compiler command line after all the source files.

eg.

gcc -I/home/aleksei/openSSL0.9.8/include -o app tema1.c ./libcrypto.a
+5
source

, , Windows, Mingw OpenSSL Windows ( : Win32 OpenSSL v1.0.2a ). libeay32.a, C:\OpenSSL-Win32\lib\MinGW\ ( OpenSSL).

CMake CLION IDE, libeay32.dll.a, CMake wasn . 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)

( ):

#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;
}
+2

All Articles