AES/CBC 和 AES/ECB 加密后的数据大小 [英] Size of data after AES/CBC and AES/ECB encryption
问题描述
我想知道 AES 加密后的数据大小,这样我就可以避免缓冲我的 AES 后数据(在磁盘或内存上),主要是为了知道大小.
I would like to know the size of data after AES encryption so that I can avoid buffering my post-AES data(on disk or memory) mainly for knowing the size.
我使用 128 位 AES 和 javax.crypto.Cipher
和 javax.crypto.CipherInputStream
进行加密.
I use 128 bit AES and javax.crypto.Cipher
and javax.crypto.CipherInputStream
for encryption.
对不同输入大小进行的一些测试表明,如下计算的后加密大小是正确的:
A few tests performed with various input sizes show that, the post encryption size calculated as below is correct:
long size = input_Size_In_Bytes;
long post_AES_Size = size + (16 - (size % 16));
但我不确定上述公式是否适用于所有可能的输入尺寸.
But I am not sure whether the above formula is applicable for all possible input sizes.
有没有一种方法可以计算应用 AES 加密后的数据大小 - 无需预先缓冲加密数据(在磁盘或内存上)以了解其加密后的大小?
Is there a way to calculate the size of data after applying AES encryption – in advance without having to buffer the encrypted data(on disk or memory) to know its post-encryption size?
推荐答案
AES 具有 16 字节的固定块大小,而不管密钥大小.假设您使用 PKCS 5/7 填充,请使用此公式,
AES has a fixed block size of 16-bytes regardless key size. Assuming you use PKCS 5/7 padding, use this formula,
cipherLen = (clearLen/16 + 1) * 16;
请注意,如果明文是块大小的倍数,则需要一个全新的块进行填充.假设您的明文是 16 个字节.密文将占用 32 个字节.
Please note that if the clear-text is multiple of block size, a whole new block is needed for padding. Say you clear-text is 16 bytes. The cipher-text will take 32 bytes.
您可能想用密文存储 IV(初始向量).在这种情况下,您需要为 IV 添加 16 个字节.
You might want to store IV (Initial Vector) with cipher-text. In that case, you need to add 16 more bytes for IV.
这篇关于AES/CBC 和 AES/ECB 加密后的数据大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!