给定一个适当的key
and iv
,这个 C 程序应该加密stdin
,输出到stdout
。
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit(ctx, EVP_aes_256_gcm(), key, iv);
const size_t block_size = 128;
unsigned char inbuf[block_size];
unsigned char outbuf[block_size + EVP_MAX_BLOCK_LENGTH];
int inlen, outlen;
for (;;)
{
inlen = fread(inbuf, 1, block_size, stdin);
if (inlen <= 0)
break;
EVP_EncryptUpdate(ctx, outbuf, &outlen, inbuf, inlen)
fwrite(outbuf, 1, outlen, stdout);
}
EVP_EncryptFinal_ex(ctx, outbuf, &outlen)
fwrite(outbuf, 1, outlen, stdout);
(为简洁起见,删除了错误检查。)
我正在通过运行验证此代码的输出
openssl aes-256-gcm -in ciphertext.txt -K <key> -iv <iv> -d
例如,这成功且可靠地解密了密文,但随后写入了错误的解密
$ openssl aes-256-gcm ...
Hello World.
bad decrypt
有什么问题会导致它这样说?