2013-12-22 38 views
2

我想在我的程序中只使用aes。我複製文件只能從Polarssl運行aes的文件

  1. 的config.h
  2. aes.h
  3. havege.h

文件夾polarssl。但是,當我運行程序

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include "polarssl/aes.h" 
#include "polarssl/havege.h" 
int main() 
{ 
    char buff[2][64] = {"ABCDEFGHIJKLMN", ""}; 
    havege_state hs; 
    int retval; 
    unsigned char IV[16]; 
    unsigned char IV2[16]; 
    unsigned char key[32]; 

    aes_context enc_ctx; 
    aes_context dec_ctx; 

    havege_init(&hs); 
    havege_random(&hs, IV, 16); 
    havege_random(&hs, key, 32); 
    strncpy(IV, IV2, 16);   //copy IV 

    aes_setkey_enc(&enc_ctx, key, 256); 
    aes_setkey_dec(&dec_ctx, key, 256); 


    //encrypt 
    aes_crypt_cbc(&enc_ctx, AES_ENCRYPT, 64, IV, buff[0], buff[1]); 
    printf("Before encrypt:%s\n", buff[0]); 

    //decrypt 
    aes_crypt_cbc(&dec_ctx, AES_DECRYPT, 64, IV2, buff[1],buff[0]); 
    printf("After decrypt:%s\n", buff[0]); 
    return 0; 
} 

我收到錯誤

In function `main': 
ex.c:(.text+0x68): undefined reference to `havege_init' 
ex.c:(.text+0x86): undefined reference to `havege_random' 
ex.c:(.text+0xa4): undefined reference to `havege_random' 
ex.c:(.text+0xe0): undefined reference to `aes_setkey_enc' 
ex.c:(.text+0xfe): undefined reference to `aes_setkey_dec' 
ex.c:(.text+0x133): undefined reference to `aes_crypt_cbc' 
ex.c:(.text+0x17e): undefined reference to `aes_crypt_cbc' 
collect2: error: ld returned 1 exit status 

回答

1

旁邊的頭文件,你還需要.c文件! (aes.c,havege.c)並在你的代碼中進行編譯。

執行方: *您確定要使用HAVEGE嗎?有很多疑問(取決於你運行的系統),標準化的CTR-DRBG似乎是一個更好的選擇。

0

我認爲你的錯誤與鏈接到Aes和Havege文件有關。你的編譯器不能識別它們! 它們與你的主文件夾在同一個文件夾中嗎?如果它們位於相同的文件夾中,則從頂部的頭文件名稱中刪除「polarssl /」。

或者,也許編譯時一定要包含aes.c和aes.h。我發現由於這個原因我得到了同樣的錯誤。我只在編譯時加入了aes.h。 示例

$terminal: gcc main.c aes.h aes.c -o encrypt 

想知道嗎? 如果你只想使用aes,你爲什麼試圖使用havege.h?