未定义对 `encrypt' 的引用 & 未定义对 `decrypt' 的引用

4
我正在使用CodeBlocks IDE测试以下OpenSLL的知识示例。
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>


#include <string.h>

int main(int arc, char *argv[])
{
  /* Set up the key and iv. Do I need to say to not hard code these in a
   * real application? :-)
   */

  /* A 256 bit key */
  unsigned char *key = "01234567890123456789012345678901";

  /* A 128 bit IV */
  unsigned char *iv = "01234567890123456";

  /* Message to be encrypted */
  unsigned char *plaintext =
    "The quick brown fox jumps over the lazy dog";

  /* Buffer for ciphertext. Ensure the buffer is long enough for the
   * ciphertext which may be longer than the plaintext, dependant on the
   * algorithm and mode
   */
  unsigned char ciphertext[128];

  /* Buffer for the decrypted text */
  unsigned char decryptedtext[128];

  int decryptedtext_len, ciphertext_len;

 /* Initialise the library */
  ERR_load_crypto_strings();
  OpenSSL_add_all_algorithms();
  OPENSSL_config(NULL);

  /* Encrypt the plaintext */
  ciphertext_len = encrypt(plaintext, strlen(plaintext), key, iv,ciphertext);

  /* Do something useful with the ciphertext here */
  printf("Ciphertext is:\n");
  BIO_dump_fp(stdout, ciphertext, ciphertext_len);

  /* Decrypt the ciphertext */
  decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,decryptedtext);

  /* Add a NULL terminator. We are expecting printable text */
  decryptedtext[decryptedtext_len] = '\0';

  /* Show the decrypted text */
  printf("Decrypted text is:\n");
  printf("%s\n", decryptedtext);

  /* Clean up */
  EVP_cleanup();
  ERR_free_strings();

  return 0;
}

我已经编译并安装了最新的Openssl库并将其链接到我的项目中。
/usr/local/ssl/lib/libcrypto.a /usr/local/ssl/lib/libssl.a /lib/x86_64-linux-gnu/libdl-2.19.so
然而,当我编译我的项目时,总是会收到以下错误:
||=== Build: Release in CryptoProject (compiler: GNU GCC Compiler) ===|
obj/Release/main.o||In function `main':|
main.c:(.text.startup+0x46)||undefined reference to `encrypt'|
main.c:(.text.startup+0x81)||undefined reference to `decrypt'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

我在项目中是否缺少一些库?请帮忙!


1
你缺少了 encryptdecrypt 函数。请仔细阅读示例,参考链接:http://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption - polslinux
在编译时,你是否链接了加密和SSL库?例如:gcc filename.c -lcrypto -lssl - Sathish
请查看使用Code::Blocks解决DLL Hell问题。它会告诉你应该把OpenSSL库放在哪里。忽略FIPS相关内容。而且,不要使用/usr/.../libcrypto.so,而是使用/usr/.../libcrypto.a。这样,你就可以避免我遇到的那个DLL问题(Basile声称没有出现过)。 - jww
@polslinux 你是正确的!对不起,大家看到了这篇帖子...感谢你的帮助。 - Sérgio Almeida
3个回答

0
在我的情况下,通过添加LDFLAGS '-lcrypt'来解决错误。请注意,没有'o',不是'-lcrypto'。

错误,libcrypt 函数具有完全不同的签名。 - n. m.

0

当我尝试从提供的GPL源代码构建D-Link WBR-1310路由器固件时,遇到了这个错误。问题是Makefile在寻找crypt.h和libcrypt.so*时会在/usr下查找,而不是tarball中包含的工具链。我不得不修改Makefile将$(wildcard /usr/lib/libcrypt.*)更改为$(shell find ../.. -name libcrypt.*)

修改后,它定位了库并相应地设置了LIBS


错误,libcrypt函数与OpenSSL无关。 - n. m.
没有说他们是。 - jcomeau_ictx

0

encryptdecrypt不是OpenSSL函数。您的编译器应该警告您有关其隐式声明的问题。如果没有,请升级到最新稳定版本的gcc,并将其设置为将所有警告视为错误。

libcrypt链接不是正确的解决方案。此库确实包含名为encryptdecrypt的函数,但这些不是您想要的函数(它们与openssl无关)。

我不知道您在哪里找到了这个确切的示例,但OpenSSL维基上的示例通常包含它们自己的encryptdecrypt实现。类似于您的示例的示例可以在此处找到,它还包含encryptdecrypt实现。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接