AES / CBC和AES / ECB加密后的数据大小 [英] Size of data after AES/CBC and AES/ECB encryption

查看:1343
本文介绍了AES / CBC和AES / ECB加密后的数据大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道AES加密后的数据大小,以避免缓冲我的后AES数据(磁盘或内存),主要是为了了解大小。



我使用128位AES和 javax.crypto.Cipher javax.crypto.CipherInputStream 进行加密。 / p>

使用各种输入大小执行的一些测试显示,以下所示的邮政加密大小是正确的:

  long size = input_Size_In_Bytes; 
long post_AES_Size = size +(16 - (size%16));

但我不知道上述公式是否适用于所有可能的输入大小。



是否有一种计算应用AES加密后的数据大小的方法 - 提前不必缓存加密的数据(在磁盘或内存中)来了解其加密后的大小? / p>

解决方案

无论密钥大小,AES都有16字节的固定块大小。假设您使用PKCS 5/7填充,请使用此公式,

  cipherLen =(clearLen / 16 + 1)* 16; 

请注意,如果明文是块大小的倍数,则需要一个全新的块填充。说你的文本是16个字节。密文将需要32个字节。



您可能想要使用密文存储IV(初始向量)。在这种情况下,您需要为IV添加16个字节。


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.

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.

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 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;

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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆