Using Crypto++ for AES CBC encryption for binary data of unknown length

1,678 views
Skip to first unread message

Chirag Shah

unread,
Feb 1, 2012, 2:14:16 PM2/1/12
to Crypto++ Users
Hi,
I want to use AES encryption with CBC (Cipher Block Chain) mode with
Crypto++. My question is i want to use this encryption on binary data
of unknown length. So, how can Crypto++ behaves in this case of
variable length.
I have following questions if i use AES CBC mode on binary data

1) Can Crypto++ work on binary data stored in .dat file or any buffer.
I do not know the input data size. It can be different every time.
2) What would be the size of Encrypted data if my binary data length
is 63 bytes, 64 bytes and 1021 bytes (basically i want to ask if size
if of multiple of 16 and what if not.
3) What is the max size Crypto++ can handle for encryption at a time.
4) What is the option NO_PADDING is for in AES CBC mode while creating
a encryption object?

Thanks in advance.

Jeffrey Walton

unread,
Feb 2, 2012, 10:46:02 AM2/2/12
to Crypto++ Users


On Feb 1, 2:14 pm, Chirag Shah <chiragatc...@gmail.com> wrote:
> Hi,
> I want to use AES encryption with CBC (Cipher Block Chain) mode with
> Crypto++. My question is i want to use this encryption on binary data
> of unknown length. So, how can Crypto++ behaves in this case of
> variable length.
You can stream data into a CBC_Mode<AES> object using multiple Put()
calls. After you are done streaming, call MessageEnd().

> I have following questions if i use AES CBC mode on binary data
>
> 1) Can Crypto++ work on binary data stored in .dat file or any buffer.
> I do not know the input data size. It can be different every time.
Yes - Use a FileSource (on disk) or ArraySource (in memoery) or
StringSource (std::string)

> 2) What would be the size of Encrypted data if my binary data length
> is 63 bytes, 64 bytes and 1021 bytes (basically i want to ask if size
> if of multiple of 16 and what if not.
Standard PKCS padding rules apply. At most, the ciphertext will grow
by 15 bytes. You can change from PKCS to no padding if you'd like.

> 3) What is the max size Crypto++ can handle for encryption at a time.
Its platform and cipher dependent. Is you data over 2^32-1?

> 4) What is the option NO_PADDING is for in AES CBC mode while creating
> a encryption object?
If you use no padding, your data length will need to be a multiple of
the block cipher's BLOCKSIZE (16 bytes).

Be sure to authenticate your data to detect tampering. You might want
to abandon CBC in favor of an authenticated encryption mode: EAX, CCM,
or GCM.

The following might also be helpful: http://www.cryptopp.com/wiki/Cbc_mode.

Jeff

Chirag Shah

unread,
Feb 2, 2012, 5:47:41 PM2/2/12
to Crypto++ Users
Hi Jeffery,

Thanks for your reply but i want to clarify one more thing.
If i have binary data buffer / file length is of i.e. 50 bytes, then
do i need to take care of chopping my data in 16 bytes for first three
Put() calls with NO_PADDING and last Put() call with padding enabled
OR i can simply give 50 byte buffer into Put() call with padding
enabled as its(50) not multiple of 16.

Chirag

Jeffrey Walton

unread,
Feb 2, 2012, 8:58:57 PM2/2/12
to Crypto++ Users


On Feb 2, 5:47 pm, Chirag Shah <chiragatc...@gmail.com> wrote:
> Hi Jeffery,
>
> Thanks for your reply but i want to clarify one more thing.
> If i have binary data buffer / file length is of i.e. 50 bytes, then
> do i need to take care of chopping my data in 16 bytes for first three
> Put() calls with NO_PADDING and last Put() call with padding enabled
> OR i can simply give 50 byte buffer into Put() call with padding
> enabled as its(50) not multiple of 16.
// const size_t size = 16 * 4 + 1;
const size_t size = 16 * 4;
string plain(size, 0x00);

for(size_t i = 0; i < size; i++)
plain[i] = 'A' + (i%26);

// cout << plain << endl;

byte key[AES::DEFAULT_KEYLENGTH], iv[AES::BLOCKSIZE];
memset(key, 0x00, sizeof(key));
memset(iv, 0x00, sizeof(iv));

CBC_Mode < AES >::Encryption aes(key, sizeof(key), iv);
StreamTransformationFilter encryptor(
aes, NULL, BlockPaddingSchemeDef::NO_PADDING
);

for(size_t j = 0; j < size; j++)
encryptor.Put((byte)plain[j]);

encryptor.MessageEnd();
size_t ready = encryptor.MaxRetrievable();

// cout << ready << endl;

string cipher(ready, 0x00);
encryptor.Get((byte*) &cipher[0], cipher.size());

Jeffrey Walton

unread,
Feb 2, 2012, 9:17:25 PM2/2/12
to Crypto++ Users
This might also be useful to you:

size_t ready = encryptor.MaxRetrievable();
for(size_t j = 0; j < size; j++)
{
encryptor.Put((byte)plain[j]);
cout << plain[j] << ", ";
ready = encryptor.MaxRetrievable();
cout << ready << endl;
}
Reply all
Reply to author
Forward
0 new messages