1
$\begingroup$

I'm currently developing a AES implementation in a graduation project at university.

As far as I know, AES uses a 128 bit block length as input .

if the entered string to encrypt is not 128 bits such as 32 bits .

what should be done for data that is less than 128 bits ? What's the best solution ?

$\endgroup$
1
  • $\begingroup$ What are you implementing? The blockcipher itself or encryption based on it? $\endgroup$
    – Maeher
    Apr 11, 2021 at 11:03

2 Answers 2

2
$\begingroup$

It really depends on the block mode that you are going to use. If you want to use something like CBC, have a look into PKCS#7. If you were to use CTR mode, then you do not require any padding, as the input will always be in chunks of 16 bytes, and you use as much of it as you require.

$\endgroup$
1
$\begingroup$

JoJoTheCodeDude's answer is correct. I just want to add some detail which may clear up some possible misunderstandings and followup questions.

AES is a block cipher. Contrary to the name Advanced Encryption Standard, it's not actually useful for practical encryption of anything. That's because, like any block cipher, it can safely encrypt exactly one block of data with any given key before the key must be changed. Since changing the key is slow and requires some way to get the next key to the recipient this isn't particularly practical. So block ciphers (including AES) MUST be used in a "Mode of Operation" for practical secure encryption.

Speaking of security, there are several notions of security. There's security against chosen-plaintext attacks (IND-CPA), security against chosen-ciphertext attacks of various sorts (IND-CCA1, IND-CCA2, IND-CCA3 AKA AE-Secure), and a few more niche notions. Different modes of operation provide different levels of security.

CBC mode and CTR mode (mentioned before) provide IND-CPA security only. If an attacker can modify ciphertexts, they're not secure. To make them secure, ciphertext authentication is needed.

To make CBC secure, it's common to MAC the ciphertext using HMAC-SHA256. If the tag from the MAC doesn't verify successfully, the recipient discards the ciphertext instead of decrypting. This construction is IND-CCA2 secure.

CTR is typically used as part of a more advanced Authenticated Encryption mode. These modes are IND-CCA3 secure. Galois/Counter Mode is common. GCM-SIV is more secure against accidental nonce reuse, but less commonly available. OCB mode recently went out of patent, and is substantially faster than GCM.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.