libgcrypt - 如何获取 gcry_cipher_encrypt() 返回的缓冲区中存储的数据长度

libgcrypt - how to get length of data stored in buffer returned by gcry_cipher_encrypt()

我正在编写一个简单的程序来使用 libgcrypt 加密和解密 C 字符串。

在我的程序的一部分中,gcry_cipher_encrypt() 是这样调用的:

gcry_cipher_encrypt(handle, cipher_text, cipher_text_buf_len, plain_text, block_len);

cipher_text_buf_lencipher_text的分配大小,在我的程序中等于plain_text的长度。此处的参考手册也建议了这一点: https://www.gnupg.org/documentation/manuals/gcrypt/Working-with-cipher-handles.html.

问题:我不知道如何获取存储在cipher_text缓冲区中的数据的长度。

如有任何帮助,我们将不胜感激。提前致谢。 瓦拉德

终于找到了。答案是gcry_cipher_encrypt()函数不需要给调用者密文的长度。这是因为在流密码和分组密码中,密文 总是 与明文的长度相同。因此,调用函数本身可以计算 gcry_cipher_encrypt

返回的缓冲区中存储的数据的长度

引用 https://en.wikipedia.org/wiki/Block_size_(cryptography):

In modern cryptography, symmetric key ciphers are generally divided into stream ciphers and block ciphers. Block ciphers operate on a fixed length string of bits. The length of this bit string is the block size. Both the input (plaintext) and output (ciphertext) are the same length; the output cannot be shorter than the input – this follows logically from the pigeonhole principle and the fact that the cipher must be reversible – and it is undesirable for the output to be longer than the input.

但是应该注意,如果您打算存储或传输加密数据,那么您将需要 save/send 初始化向量 (iv) 和明文填充信息。