C AES库的错误使用?

C AES库的错误使用?,c,aes,C,Aes,我想使用一个节省内存的AES-128实现。我发现实现了 我使用它的代码如下: 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, 0x6

我想使用一个节省内存的AES-128实现。我发现实现了

我使用它的代码如下:

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]);
    }

}
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消息[5]=“测试”;
uint8_t密码[16]={0x00};
uint8_t i;
if(debug)printf(“\n[D]运行AES-128加密\n”);
aes_expand_key(key,keyschedule);
aes_加密(消息、密钥表、密码);
printf(“消息:%s |密码:,消息”);
对于(i=0;i(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;
}
//AES使用示例
//编译为:gcc main.c aes.h aes.c
#包括
#包括“aes.h”
#包括
#包括
int main(int argc,char*argv[])
{
无符号字符键[key_128]={0x54、0x68、0x69、0x73、0x20、0x69、0x73、0x20、0x61、0x20、0x74、0x65、0x73、0x74、0x21、0x21};
无符号字符ptext[16]=“测试”;
无符号字符ctext[16];
无符号字符decptext[16];
无符号整数i=0;
aes_ctx_t*ctx;
init_aes();
ctx=aes_alloc_ctx(键,sizeof(键));
如果(!ctx){
perror(“aes_alloc_ctx”);
返回退出失败;
}
aes_加密(ctx、ptext、ctext);

对于(i=0;i虽然我找不到您在Malbrains代码中使用的确切函数,但我相信您的问题在于
消息
的数组长度不同。该算法加密128位(16字节)的块,但您只分配了5个字节

uint8_t message[5] = "test";
vs

尝试使用完全相同的数据初始化它

uint8_t message[16];
memset(message, 0, sizeof(message));
memcpy(message, "test", 5);

虽然我找不到您在Malbrains代码中使用的确切函数,但我相信您的问题在于
消息
的数组长度不同。该算法加密128位(16字节)的块,但您只分配了5个字节

uint8_t message[5] = "test";
vs

尝试使用完全相同的数据初始化它

uint8_t message[16];
memset(message, 0, sizeof(message));
memcpy(message, "test", 5);

你应该看看关键进度表:第二个示例的构建情况如何?你应该看看关键进度表:第二个示例的构建情况如何?非常感谢。这正是原因。现在一切正常!非常感谢。这正是原因。现在一切正常!