gpt4 book ai didi

c - 带有 BIO API 的 OpenSSL EVP_aes_128_gcm

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:54:55 24 4
gpt4 key购买 nike

我正在尝试将 OpenSSL 的 BIO 接口(interface)与 AES GCM 128 位加密模式结合使用。我几乎是直接从一本书(Network Security with OpenSSL example 4.8)中复制一个示例,只是将加密模式更改为 aes_128_gcm,但事情不起作用(写入了一个空文件)。由于我是 OpenSSL 的新手,所以我可能在做一些愚蠢的事情。你能告诉我下面的代码片段有什么问题吗:

int main()
{
OpenSSL_add_all_algorithms();
char *msg = "hello";
return write_data("hello.out", msg, strlen(msg), "1234567890123456");
}

int write_data(const char *filename, char *out, int len, unsigned char *key)
{
int total, written;
BIO *cipher, *b64, *buffer, *file;
file = BIO_new_file(filename, "w");
buffer = BIO_new(BIO_f_buffer());
b64 = BIO_new(BIO_f_base64());
cipher = BIO_new(BIO_f_cipher());
BIO_set_cipher(cipher, EVP_aes_128_gcm(), key, NULL, 1);
BIO_push(cipher, b64);
BIO_push(b64, buffer);
BIO_push(buffer, file);
for (total = 0; total < len; total += written)
{
if ((written = BIO_write(cipher, out + total, len - total)) <= 0)
{
if (BIO_should_retry(cipher))
{
written = 0;
continue;
}
break;
}
}
BIO_flush(cipher);
BIO_free_all(cipher);
}

我刚刚将示例中的 EVP_des_ede3_cbc() 更改为 EVP_aes_128_gcm() - 并将 key 更改为 16 个字符。

最佳答案

对不起,我想通了。我将 NULL 作为 IV 传递 - 显然这是绝对必需的!

关于c - 带有 BIO API 的 OpenSSL EVP_aes_128_gcm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26971590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com