错误使用AES库?

时间:2013-09-06 09:39:25

标签: c aes

我想使用节省内存的AES-128实现。我找到了Karl Malbrain的实现。

我正在使用以下代码:

void encryptUboot(void){
    //uint8_t  key[AES_KEY_LENGTH] = {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
    uint8_t  key[AES_KEY_LENGTH] = {0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x21, 0x21};
    uint8_t  keyschedule[AES_KEY_LENGTH * AES_ROUNDS] = {0x00};
    uint8_t message[5] = "test";
    uint8_t cipher[16] = {0x00};
    uint8_t i;

    if(debug)   printf("\n[D] Running AES-128 encryption\n");

    aes_expand_key(key, keyschedule);
    aes_encrypt(message, keyschedule, cipher);
    printf("message: %s | cipher: ", message);
    for(i = 0; i<AES_KEY_LENGTH; i++){
        printf("%02x ", cipher[i]);
    }

}

输出:

[D] Running AES-128 encryption
message: test | cipher: 2d 58 45 71 24 43 f5 cd 69 6d 07 b3 a3 29 de 8f

但是,使用here(zip文件)中的代码和下面的代码......

// AES usage example
// compile as: gcc main.c aes.h aes.c

#include <stdlib.h>
#include "aes.h"
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{

    unsigned char key[KEY_128] = {0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x21, 0x21};
    unsigned char ptext[16] = "test";
    unsigned char ctext[16];
    unsigned char decptext[16];
    unsigned int i = 0;
    aes_ctx_t *ctx;

    init_aes();
    ctx = aes_alloc_ctx(key, sizeof(key));
    if(!ctx) {
            perror("aes_alloc_ctx");
            return EXIT_FAILURE;
    }
    aes_encrypt(ctx, ptext, ctext);
    for(i=0;i<KEY_128;i++)  printf("%02x ", ctext[i]);
    puts("");
    aes_decrypt(ctx, ctext, decptext);
    puts(decptext);

    aes_free_ctx(ctx);
    return EXIT_SUCCESS;
}

..它输出一个不同的密码:

1f 53 3f 60 15 d5 ab 16 69 b6 c6 3b 9e 77 2f 0c
test

你知道我的错吗?显然,我正在以错误的方式检测这些库。

谢谢, -P

0 个答案:

没有答案