【问题标题】:Undefined reference to `encrypt' & undefined reference to `decrypt'未定义的“加密”引用和未定义的“解密”引用
【发布时间】:2014-08-07 09:22:20
【问题描述】:

我正在使用 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)) ===|

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

【问题讨论】:

  • 您缺少encryptdecrypt 函数。更好地阅读示例wiki.openssl.org/index.php/…
  • 在编译时是否链接了加密和 ssl 库?喜欢gcc filename.c -lcrypto -lssl
  • Overcome DLL Hell with Code::Blocks。它向您展示了放置 OpenSSL 库的位置。忽略 FIPS 的东西。并且 ratehr 比使用/usr/.../libcrypto.so,使用/usr/.../libcrypto.a 那样,您将避免我遇到的那个DLL(并且Basile 声称没有发生)。
  • @polslinux 你是对的!对不起,这个帖子..感谢您的帮助。

标签: c ubuntu encryption openssl codeblocks


【解决方案1】:

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

【讨论】:

  • 错了,libcrypt 函数的签名完全不同。
【解决方案2】:

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

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

我不知道您在哪里找到了这个确切的示例,但 OpenSSL wiki 上的示例通常包含 their own implementations of encrypt and decrypthere 可以找到一个与您更相似的示例,它还包含 encryptdecrypt 实现。

【讨论】:

    【解决方案3】:

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

    修改后找到库,并相应设置LIBS

    【讨论】:

    • 错误,libcrypt 函数与 OpenSSL 无关。
    • 没说是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多