c - 带有 BIO API 的 OpenSSL EVP_aes_128_gcm

标签 c linux encryption openssl

我正在尝试将 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/

相关文章:

android - OpenSSL Android NDK 的包装类

c - C 中的数组内存分配取决于命名约定?

c++ - 每个版本更新的代码更改

c - numactl --membind

c - Linux,UDP数据报和内核时间戳: Lots of examples and stackoversflow entries later,,仍然根本无法获得时间戳

linux - 如何在 Linux UEFI 中使用 memmap 内核选项启动?

linux - 为什么我的 bash 脚本在空白处中断?

c# - .net Cryptography - 有没有办法告诉某些东西被解密错误?

php - 在哪里保存数字内容并防止共享?

Delphi 10.2 Coinbase Pro API 签名