使用具有不同密钥的 AES 的相同短加密结果

Same short encryption result using AES with different keys

我有一个纯文本

unsigned char plaintext[] = "Hi, this is trial number one";

对于键,而不是使用类似的东西:

unsigned char key[16] = "azertyuiopqsdfg";

我决定大量使用 "dog", "azkier", "jfieifdragon", ...

到目前为止,我的代码如下所示:

unsigned char *aes_encrypt(unsigned char *plaintext, unsigned char *key)
{
    EVP_CIPHER_CTX *ctx;
    ctx = EVP_CIPHER_CTX_new();
    unsigned char iv[16] = "0000000000000000";

    int c_len = strlen(plaintext) + AES_BLOCK_SIZE;
    int f_len = 0;
    unsigned char *ciphertext = malloc(c_len);

    EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);

    EVP_EncryptUpdate(ctx, ciphertext, &c_len, plaintext, strlen(plaintext));

    EVP_EncryptFinal_ex(ctx, ciphertext+c_len, &f_len);

    EVP_CIPHER_CTX_free(ctx);

    return ciphertext;
}

当我编译 运行 时,输出看起来像这样:

the key: dog
the plain: Hi, this is trial number one
ciphertext: 157a320

the key: azkier
the plain: Hi, this is trial number one
ciphertext: 157a320

.....

我的问题是:

为什么我使用不同的密钥却总是得到相同的密文?

还有,为什么密文真的很短?不过我的明文很长。

谢谢。

更新 --> 我调用 aes_encrypt 的方式是这样的:

unsigned char plaintext[] = "Hi, this is trial number one";
unsigned char *cipher;
cipher = aes_encrypt(plaintext, "dog");
printf("The cipher is: %x\n", cipher);
free(cipher);

unsigned char *cipher;
cipher = aes_encrypt(plaintext, "azkier");
printf("The cipher is: %x\n", cipher);
free(cipher);

在你的测试代码中:

printf("The cipher is: %x\n", cipher);

嗯,这当然行不通——%xcipher 地址 打印为十六进制,而不是其内容。如果你想转储 cipher 的内容,你需要自己循环遍历每个字节。

此外,EVP_EncryptInit_exkey 参数是一个固定长度的缓冲区,其大小根据您使用的密码设置。 它不是字符串。传递短字符串可能会导致不可预知的行为,因为在字符串结束后存储的任何数据都可能用作密钥的一部分。