6

我正在尝试构建一个 openssl 简单程序。这是完整的代码:

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

我尝试使用以下命令编译它:

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

我明白了:

 /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

我在 Ubuntu 10.04 下。我怎样才能让它工作?

4

3 回答 3

8

您可能正在尝试静态链接,但该-L选项-lcrypto正在寻找要动态链接的文件。要静态链接到特定库,只需.a在所有源文件之后在编译器命令行上指定您的文件。

例如,

gcc -I/home/aleksei/openSSL0.9.8/include -o app tema1.c ./libcrypto.a
于 2012-06-12T22:09:52.097 回答
3

对于那些有同样问题但正在使用 Windows、Mingw 和这个OpenSSL for Windows 的人(目前:Win32 OpenSSL v1.0.2a)。您需要链接到libeay32.a位于C:\OpenSSL-Win32\lib\MinGW\(安装 OpenSSL 之后)的那个。

就我而言,我使用的是 CMake 和强大的 CLion IDE,所以我不得不将库重命名为,libeay32.dll.a因为 CMake 没有找到库。这是我的 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;
}
于 2015-05-15T19:32:00.227 回答
0

我认为参数的顺序应该重置如下:

gcc -I/home/aleksei/openSSL0.9.8/include -o app  tema1.c -L . -lssl -lcrypto
于 2022-02-26T10:00:41.270 回答