通过将数组作为输入数据传递来实现AES-128 CBC

问题描述 投票:0回答:1

我正在研究AES-128/192/256,基本上我从代理获取数据作为字符串,我只需要加密那些数据,我需要验证它。

我已经遇到过这些https://github.com/empyreanx/tiny-AES128-Chttps://github.com/kokke/tiny-AES-c链接。

我的代码是:

static void test_encrypt_cbc(void)
{
    unsigned char input[] = 
"So_letmeknowRuinterested/[email protected]";   
 //64bits

    unsigned char cipher[sizeof input];

    printf("size of in:%lu\n",strlen(input));

    unsigned char key[] = "Gns7AauH3dnaod==";    //16 bits
    unsigned char iv[] = "vhdNaleuTHenaOlL";     //16 bits

    AES128_CBC_encrypt_buffer(cipher, input, 64, key, iv);

    if(0 == memcmp((char*) cipher, (char*) input, 64))
    {
        printf("SUCCESS!\n");
    }
    else
    {
        printf("FAILURE!\n");
    }
}

我还在加密后打印密文,它正在打印一些未定义的字符。

我不知道,但我将“密码”与“输入”进行比较,最终失败了!

请任何人都可以告诉我我做错了什么。

提前致谢。

c encryption cryptography aes
1个回答
0
投票

这对cipher不同于input是合乎逻辑的,不是吗?

要检查加密是否有效,您应该解密编码的消息并检查它们是否相等:

static void test_encrypt_cbc(void)
{
    /* 64 bytes, or 512 bits */
    unsigned char input[] = 
"So_letmeknowRuinterested/[email protected]";   

    unsigned char cipher[sizeof input];
    unsigned char output[sizeof input];

    printf("size of in:%lu\n",strlen(input));

    /* 16 bytes or 128 bits */
    unsigned char key[] = "Gns7AauH3dnaod==";    
    unsigned char iv[] = "vhdNaleuTHenaOlL";     

    /* input --> cipher */
    AES128_CBC_encrypt_buffer(cipher, input, 64, key, iv);

    /* cipher --> output */
    AES128_CBC_decrypt_buffer(output, cipher, 64, key, iv);

    if(0 == memcmp((char*) output, (char*) input, 64))
    {
        printf("SUCCESS!\n");
    }
    else
    {
        int i;
        printf("FAILURE!\nInput and output are different:\n");
        for (i = 0; i < sizeof input; ++i) 
        {
            printf("%02x - %02x\n", input[i], output[i]);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.