openssl:undefined reference to symbol ‘EVP_EncryptUpdate@@libcrypto.so.10’
查看 openssl 版本:
$ openssl version -a
OpenSSL 1.0.2k-fips 26 Jan 2017
built on: reproducible build, date unspecified
platform: linux-x86_64
options: bn(64,64) md2(int) rc4(16x,int) des(idx,cisc,16,int) idea(int) blowfish(idx)
compiler: gcc -I. -I.. -I../include -fPIC -DOPENSSL_PIC -DZLIB -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -DKRB5_MIT -m64 -DL_ENDIAN -Wall -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -Wa,--noexecstack -DPURIFY -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DRC4_ASM -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM -DECP_NISTZ256_ASM
OPENSSLDIR: "/etc/pki/tls"
engines: rdrand dynamic
Code:
$ cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
void encrypt_des_ede_cbc_pkcs(
unsigned char *in, // 待加密數據
unsigned int inLen, // 待加密數據字節數
unsigned char *key, // 密 鑰,長度總是24字節
unsigned char *iv) // 偏移量,長度總是08字節
{
printf("encrypt......\n\n");
unsigned char *outBuf;
unsigned int outBufLen, outLen1, outLen2;
/* 依據PKCS填充規則 */
outBufLen = (inLen/8 + 1) * 8;
outBuf = (unsigned char *)malloc(outBufLen);
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_EncryptInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, key, iv);
EVP_EncryptUpdate(&ctx, outBuf, &outLen1, in, inLen);
EVP_EncryptFinal_ex(&ctx, outBuf+outLen1, &outLen2);
unsigned int i;
for (i = 0; i < outBufLen; i++)
{
printf("%02x ", outBuf[i]);
}
printf("\n");
free(outBuf);
printf("in: %p\n", in);
printf("inLen: %u\n", inLen);
printf("outBufLen: %u\n", outBufLen);
printf("outLen1: %u\n", outLen1);
printf("outLen2: %u\n", outLen2);
printf("\n\n");
}
void decrypt_des_ede_cbc_pkcs(
unsigned char *out, // 待解密數據
unsigned int outLen, // 待解密數據字節數
unsigned char *key, // 密 鑰,長度總是24字節
unsigned char *iv) // 偏移量,長度總是08字節
{
printf("decrypt......\n\n");
unsigned char *inBuf;
unsigned int inBufLen, inLen1, inLen2;
/* 依據PKCS填充規則 */
inBufLen = (outLen/8 - 1) * 8;
inBuf = (unsigned char *)malloc(inBufLen);
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_DecryptInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, key, iv);
EVP_DecryptUpdate(&ctx, inBuf, &inLen1, out, outLen);
EVP_DecryptFinal_ex(&ctx, inBuf+inLen1, &inLen2);
unsigned int i;
for (i = 0; i < inBufLen; i++)
{
printf("%02x ", inBuf[i]);
}
printf("\n");
free(inBuf);
printf("out: %p\n", out);
printf("outLen: %u\n", outLen);
printf("inBufLen: %u\n", inBufLen);
printf("inLen1: %u\n", inLen1);
printf("inLen2: %u\n", inLen2);
printf("\n\n");
}
int main()
{
/* 加載算法 */
OpenSSL_add_all_algorithms();
/* 密鑰長度24字節 */
unsigned char key[] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
/* 偏移量長度8字節 */
unsigned char iv [] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
/* 加密 */
unsigned char in [] = "test1280";
unsigned int inLen = 8; // 不可strlen(in),考慮到in中包含0x00字節會導致獲取待加密數據長度錯誤
encrypt_des_ede_cbc_pkcs(in, inLen, key, iv);
/* 解密 */
unsigned char out[] = { 0x64, 0x5a, 0x6b, 0xd6, 0xbf, 0xf8, 0x36, 0xb2,
0x4f, 0xd1, 0x74, 0xf6, 0xe7, 0xf6, 0xaf, 0xdb };
unsigned int outLen = 16;// 不可strlen(out)
decrypt_des_ede_cbc_pkcs(out, outLen, key, iv);
return 0;
}
編譯:gcc -o main main.c -lssl
$ gcc -o main main.c -lssl
/bin/ld: /tmp/ccMSnaP0.o: undefined reference to symbol 'EVP_EncryptUpdate@@libcrypto.so.10'
/bin/ld: note: 'EVP_EncryptUpdate@@libcrypto.so.10' is defined in DSO /lib64/libcrypto.so.10 so try adding it to the linker command line
/lib64/libcrypto.so.10: could not read symbols: 無效的操作
collect2: 錯誤:ld 返回 1
即:
undefined reference to symbol ‘EVP_EncryptUpdate@@libcrypto.so.10’
解決辦法:
gcc -o main main.c -lssl -lcrypto
重點是添加 -lcrypto 這個動態鏈接庫。
參考:
1.http://blog.sina.com.cn/s/blog_45497dfa0100nxi3.html
2.https://blog.csdn.net/u010587433/article/details/51211383
3.https://blog.51cto.com/chaoyuezhangsan/1745230
4.https://stackoverflow.com/questions/17812344/undefined-reference-to-symbol-bio-ctrllibcrypto-so-10
I think is missing -lcrypto on the command that you put