2013-08-09 83 views
18

この3つのモードでopenSSLからAESをテストしたいだけです:128,192、解読されたテキストは私の入力とは異なり、私は理由を知りません。また、巨大な入力の長さ(1024バイトと言う)を渡すと、core dumpedと表示されます...私の入力は常に同じですが、少なくとも今のところ問題はありません。 HERESにコード:opensslを使用したAES(aes-cbc-128、aes-cbc-192、aes-cbc-256)暗号化/復号化C

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

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

    /* generate a key with a given length */ 
    unsigned char aes_key[keylength]; 
    memset(aes_key, 0, sizeof(aes_key)); 
    if (!RAND_bytes(aes_key, keylength)) 
    { 
     exit(-1); 
    } 
    aes_key[keylength-1] = '\0'; 

    int inputslength; 
    printf("Give an input's length:\n"); 
    scanf("%d", &inputslength); 

    /* generate input with a given length */ 
    unsigned char aes_input[inputslength+1]; 
    memset(aes_input, '0', sizeof(aes_input)); 
    aes_input[inputslength] = '\0'; 

    /*printf("original:\t"); 
    for(i=0; i<inputslength; i++) 
    { 
     printf("%c ", aes_input[i]); 
    } 
    printf("\n");*/ 

    /* init vector */ 
    unsigned char iv[AES_BLOCK_SIZE]; 
    if (!RAND_bytes(iv, AES_BLOCK_SIZE)) 
    { 
     exit(-1); 
    } 

    //printf("AES_BLOCK_SIZE = %d\n", AES_BLOCK_SIZE); // aes block size is 16 bytes = 128 bits 
    AES_KEY enc_key, dec_key; 
    unsigned char enc_out[AES_BLOCK_SIZE]; 
    unsigned char dec_out[AES_BLOCK_SIZE]; 

    // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256 
    AES_set_encrypt_key(aes_key, keylength, &enc_key); 
    AES_cbc_encrypt(aes_input, enc_out, inputslength, &enc_key, iv, AES_ENCRYPT); 

    AES_set_decrypt_key(aes_key, keylength, &dec_key); 
    AES_decrypt(enc_out, dec_out, &dec_key); 

    printf("original:\t"); 
    for(i=0;*(aes_input+i)!=0x00;i++) 
     printf("%X ",*(aes_input+i)); 
    printf("\nencrypted:\t"); 

    for(i=0;*(enc_out+i)!=0x00;i++) 
     printf("%X ",*(enc_out+i)); 

    printf("\ndecrypted:\t"); 
    for(i=0;*(dec_out+i)!=0x00;i++) 
     printf("%X ",*(dec_out+i)); 
    printf("\n"); 

    /*printf("\n\noriginal:\t"); 
    for(i=0; i<inputslength; i++) 
    { 
     printf("%x ", dec_out[i]); 
    } 
    printf("\n");*/ 


    return 0; 
} 

編集:私は、私は結果だ代わりにAES_BLOCK_SIZEinputslengthに出力サイズを変更し

Give a key length [only 128 or 192 or 256!]: 
128 
Give an input's length: 
5 
original:  30 30 30 30 30 
encrypted:  94 56 50 7E 19 B2 1C CE 20 23 4A E7 10 AF DB E3 30 30 30 30 30 
decrypted:  E1 5F F4 3D E8 8D 91 19 CD 3E 22 1E AF 1C 8F 5A 94 56 50 7E 19 B2 1C CE 20 23 4A E7 10 AF DB E3 30 30 30 30 30 

をだから可能であることoutpusとtheresの問題サイズとivのサイズ? AES-CBC-128、AES-CBC-192、AES-CBC-256の場合、どのようなサイズにする必要がありますか?

+2

サイドノート:AESキーをnullで終わらせる必要はありません。そのバイトのランダムなブロック。それで全部です。 'aes_key [keylength-1] = '\ 0''は無意味です(あなたの鍵の最後のバイト(それは必然的に8倍以上です)をゼロに設定する点を除いては無意味です。 ) – WhozCraig

+0

@WhozCraig:ありがとう、それを知ってよかった。どのように主要な問題については、どのようなアイデアがありますか? – ivy

+1

たくさん。暗号化と復号化のためのバッファーサイズは十分に大きい*どこにも*ありません。このデータを文字データとして扱うのではなく、一般的な16進数の印刷機能が必要です。 *バイナリ*。正式な回答を今作成するが、そこを見始める。 – WhozCraig

答えて

25

この修正されたコードを見てください。次の点に注意してください

  1. を追加しましたhex_print(マイナー)
  2. キーバッファ(メディア)の適切なサイズを追加しました。
  3. 出力暗号化バッファーの適切なサイズを追加しました(ブロックサイズの倍数でなければなりません。元のソースバッファーが正確なブロックサイズの倍数である場合でも、完全な1ブロックのパディングが必要です) 。
  4. 同じIVの両方の暗号化に使用して復号化する。
  5. を最後に、奇妙なことがAES_cbc_encrypt()は(呼び出しの最後のパラメータを参照)暗号化と復号化の両方に使用されて見えるかもしれませんよう。

ソースコード

#include <stdio.h> 
#include <stdlib.h> 
#include <string.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!]:\n"); 
    scanf("%d", &keylength); 

    /* generate a key with a given length */ 
    unsigned char aes_key[keylength/8]; 
    memset(aes_key, 0, 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)); 

    // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256 
    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); 

    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:\t"); 
    hex_print(aes_input, sizeof(aes_input)); 

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

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

    return 0; 
} 

テスト出力

Give a key length [only 128 or 192 or 256!]: 
128 
Give an input's length: 
10 
original: 58 58 58 58 58 58 58 58 58 58 
encrypt: A9 66 C5 24 A4 02 AB 96 08 65 F7 22 A5 FB BE 26 
decrypt: 58 58 58 58 58 58 58 58 58 58 

セカンドテスト出力

Give a key length [only 128 or 192 or 256!]: 
128 
Give an input's length: 
10 
original: 58 58 58 58 58 58 58 58 58 58 
encrypt: C2 47 6D B1 A1 68 29 53 55 74 C5 CC 3F 27 0A 3F 
decrypt: 58 58 58 58 58 58 58 58 58 58 

私は心からこのことができます願っています。

+0

あなたを傷つけて申し訳ありませんが、あなたは正しいです、今はすべてうまくあります:) – ivy

3

@WhozCraig:ありがとうございました!それは私にたくさん説明しました!しかし、もう一つ問題があります。静的配列を動的配列に変更しました。私がそれをしたときに、いくつかのエラーが発生しました。しかし、私は巨大なインプットのサイズを与えたときにのみ、valgrindの出力を見てください:http://pastie.org/private/bzofrrtgrlzr0doyb3g。エラーは、小さなサイズ(例のように10)を渡したときに大量の入力を渡したときにのみ発生します。他のすべては完全に動作しています。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.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) 
{ 
    size_t inputslength = 0; 
    printf("Give an input's length:\n"); 
    scanf("%lu", &inputslength); 

    int keylength; 
    printf("Give a key length [only 128 or 192 or 256!]:\n"); 
    scanf("%d", &keylength); 


    // generate a key with a given length 
    unsigned char *aes_key = (unsigned char*)malloc(sizeof(unsigned char) * (keylength/8)); 
    memset(aes_key, 0, keylength/8); 
    RAND_bytes(aes_key, keylength/8); 

    // generate input with a given length 
    unsigned char *aes_input = (unsigned char*)malloc(sizeof(unsigned char) * (inputslength)); 
    memset(aes_input, 'X', sizeof(aes_input)); 

    // init vectors 
    unsigned char *iv_enc = (unsigned char*)malloc(sizeof(unsigned char) * (AES_BLOCK_SIZE)); 
    unsigned char *iv_dec = (unsigned char*)malloc(sizeof(unsigned char) * (AES_BLOCK_SIZE)); 
    // iv_dec == iv_enc 
    RAND_bytes(iv_enc, AES_BLOCK_SIZE); 
    memcpy(iv_dec, iv_enc, AES_BLOCK_SIZE); 

    // buffers for encryption and decryption 
    const size_t length = (((inputslength + AES_BLOCK_SIZE)/AES_BLOCK_SIZE) * AES_BLOCK_SIZE); 
    unsigned char *enc_out = (unsigned char*)malloc(sizeof(unsigned char) * (length)); 
    unsigned char *dec_out = (unsigned char*)malloc(sizeof(unsigned char) * (inputslength)); 

    // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256 
    AES_KEY encKey, decKey; 
    AES_set_encrypt_key(aes_key, keylength, &encKey); 
    AES_cbc_encrypt(aes_input, enc_out, inputslength, &encKey, iv_enc, AES_ENCRYPT); 

    AES_set_decrypt_key(aes_key, keylength, &decKey); 
    AES_cbc_encrypt(enc_out, dec_out, length, &decKey, iv_dec, AES_DECRYPT); 

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

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

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

    free(aes_key); 
    aes_key = NULL; 
    free(aes_input); 
    aes_input = NULL; 
    free(iv_enc); 
    iv_enc = NULL; 
    free(iv_dec); 
    iv_dec = NULL; 
    free(enc_out); 
    enc_out = NULL; 
    free(dec_out); 
    dec_out = NULL; 

    return 0; 
} 

EDIT:

[OK]を、何かがさえ巨大な入力のために、完璧に働いて、私は投稿前のコード、HERESに新しいものと間違っていました。もう一度私を助けてくれてうれしい!:)

#include <stdio.h> 
#include <stdlib.h> 
#include <string.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!]:\n"); 
    scanf("%d", &keylength); 

    /* generate a key with a given length */ 
    unsigned char *aes_key = (unsigned char*)malloc(sizeof(unsigned char) * (keylength/8)); 
    memset(aes_key, 0, 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 = (unsigned char*)malloc(sizeof(unsigned char) *inputslength); 
    memset(aes_input, 'X', inputslength); 

    /* init vector */ 
    unsigned char *iv_enc = (unsigned char*)malloc(sizeof(unsigned char) *AES_BLOCK_SIZE), *iv_dec = (unsigned char*)malloc(sizeof(unsigned char) *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 = (unsigned char*)malloc(sizeof(unsigned char) *encslength); 
    unsigned char *dec_out = (unsigned char*)malloc(sizeof(unsigned char) *inputslength); 
    memset(enc_out, 0, sizeof(enc_out)); 
    memset(dec_out, 0, sizeof(dec_out)); 

    // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256 
    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); 

    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:\t"); 
    hex_print(aes_input, inputslength); 

    printf("encrypt:\t"); 
    hex_print(enc_out, encslength); 

    printf("decrypt:\t"); 
    hex_print(dec_out, inputslength); 

    // free memory here 

    return 0; 
} 
+6

もう一度見てください。 'sizeof()'は、固定配列または可変配列ではなくポインタで使用すると、まったく新しい意味を持ちます。それは、*ポインタのサイズになります。***は***に割り当てられたデータのサイズではありません。サイズはすべて変数でなければなりません。 ** sizeof(何か)ではない**。それを修正すれば、あなたの最終的な問題はなくなります。 (sidenote:通常のC言語でプログラミングするときに 'malloc()'をキャストしないでください。これは特に移植性の問題を引き起こす可能性があります。最初は。)= P – WhozCraig

+1

もう一度soooに感謝します!ここに実例があります:http://pastie.org/private/dtvmftozmru02webkzzq私は 'malloc'ビーコンをキャストしました。私のgccが私に言った - ' malloc'は私が思い出したように空を返します、そうですか?私がC++で書くときは、 'new'と' delete'を使います。私はC++で 'malloc'を使うべきではないことを知っています;) – ivy

+2

C言語のプログラミングで' malloc() 'をキャストすること自体には潜在的な落とし穴があります。明らかにC++では必須ですが、あなたが言ったように、あなたは 'new'と' delete'を使います。このサイトで "malloc [c]をキャスト"してください。それはかなり議論された問題です。それとは別に、あなたが立ち上がって走っていることをとてもうれしく思います。 – WhozCraig

関連する問題