2014-02-28 64 views
1

我想使用openssl使用AES 128對數據進行加密/解密。使用openssl進行數據加密/ ecryption

void main(void) 
{ 
    unsigned char key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; 
    AES_KEY enc_key, dec_Key; 
    unsigned char text = "Data encryption/ecryption with openssl"; 
    unsigned char encrtext[64], decrptext[64]; 

    AES_set_encrypt_key(key, 128, &enc_Key); 
    AES_encrypt(text, encrtext, &enc_Key); 

    AES_set_decrypt_key(key,128,&dec_key); 
    AES_decrypt(encrtext, decrptext, &dec_Key); 

    printf("Data = %s",decrptext); 
} 

此程序的執行使

Data = Data encryption/ 

我看到,只有16個字符被加密和解密。你能幫忙解決這個問題嗎? 謝謝

+0

[如何在OpenSSL中使用AES進行加密](http://stackoverflow.com/questions/9889492/how-to-do-encryption-using-aes-in-openssl) – jww

+0

您應該*不*使用'AES_encrypt'和朋友。您應該使用'EVP_ *'功能。請參閱OpenSSL wiki上的[EVP Symmetric Encryption and Decryption](https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption)。事實上,您應該使用經過身份驗證的加密,因爲它提供了*機密性和真實性。請參閱OpenSSL wiki上的[EVP Authenticated Encryption and Decryption](https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption)。 – jww

回答

1

OpenSSL支持單次加密,只要你設置一個有效的密鑰,提供一個合適的IV,並調用相應的函數(其中一個似乎很奇怪,調用加密函數來解密,但實際上它是一個對稱算法所以不要因此而感到震驚):

下面的例子。注意加密區塊是補齊最後的塊加密之前得當,和解密過程中填充被拋出(這是你想要的):

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <limits.h 
#include <openssl/aes.h> 
#include <openssl/rand.h> 

/* a simple hex-print routine. could be modified to print 16 bytes-per-line */ 
static void hex_print(const void* pv, size_t len) 
{ 
    const unsigned char * p = (const unsigned char*)pv; 
    if (NULL == pv) 
     printf("NULL"); 
    else 
    { 
     size_t i = 0; 
     for (; i<len;++i) 
      printf("%02X ", *p++); 
    } 
    printf("\n"); 
} 

/* main entrypoint */ 
int main(int argc, char **argv) 
{ 
    int keylength; 
    printf("Give a key length [only 128 or 192 or 256!]: "); 
    scanf("%d", &keylength); 

    /* generate a key with a given length */ 
    unsigned char aes_key[keylength/8]; 
    if (!RAND_bytes(aes_key, keylength/8)) 
     exit(-1); 

    size_t inputslength = 0; 
    printf("Give an input's length:\n"); 
    scanf("%lu", &inputslength); 

    /* generate input with a given length */ 
    unsigned char aes_input[inputslength]; 
    memset(aes_input, 'X', inputslength); 

    /* init vector */ 
    unsigned char iv_enc[AES_BLOCK_SIZE], iv_dec[AES_BLOCK_SIZE]; 
    RAND_bytes(iv_enc, AES_BLOCK_SIZE); 
    memcpy(iv_dec, iv_enc, AES_BLOCK_SIZE); 

    /* buffers for encryption and decryption */ 
    const size_t encslength = ((inputslength + AES_BLOCK_SIZE)/AES_BLOCK_SIZE) * AES_BLOCK_SIZE; 
    unsigned char enc_out[encslength]; 
    unsigned char dec_out[inputslength]; 
    memset(enc_out, 0, sizeof(enc_out)); 
    memset(dec_out, 0, sizeof(dec_out)); 

    /* initialize encryption key, encrypt */ 
    AES_KEY enc_key, dec_key; 
    AES_set_encrypt_key(aes_key, keylength, &enc_key); 
    AES_cbc_encrypt(aes_input, enc_out, inputslength, &enc_key, iv_enc, AES_ENCRYPT); 

    /* same key and if for decrypt */ 
    AES_set_decrypt_key(aes_key, keylength, &dec_key); 
    AES_cbc_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT); 

    printf("original:\n"); 
    hex_print(aes_input, sizeof(aes_input)); 

    printf("encrypt:\n"); 
    hex_print(enc_out, sizeof(enc_out)); 

    printf("decrypt:\n"); 
    hex_print(dec_out, sizeof(dec_out)); 

    return 0; 
} 

輸出(顯然你的會有所不同)

Give a key length [only 128 or 192 or 256!]: 192 
Give an input's length: 
27 
original: 
58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 
encrypt: 
5F F1 57 AA 3C BC C3 10 49 34 E7 E8 CB 6D 4D B0 AE BB 14 04 C0 26 D6 B7 A4 69 0B 3F 92 84 97 A0 
decrypt: 
58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 
Program ended with exit code: 0 
0

這是應該的,AES是一個block cipher塊大小爲128位,即16個字節。

您需要通過加密功能手動輸入所有輸入塊。

+0

我應該將輸入數據分成16個字節的塊,然後逐塊加密/解密嗎?有沒有辦法用openssl庫來做到這一點? – ARM

相關問題