How to do padding to input giving to encrypt using AES algorithm if the input is less than 128 bits? I need to encrypt a decimal number such as 10.

  • 4
    $\begingroup$ What research have you done? Have you googled the phrase "AES encryption padding"? $\endgroup$ – pg1989 Jan 23 '16 at 5:04
  • 2
    $\begingroup$ First you have to think about encoding a numeric value into a binary format. For example, if you assume that the number is 128-bit long, then you don't even need padding. $\endgroup$ – Artjom B. Jan 23 '16 at 10:52

First you need to think about what range of numbers you want to encrypt. You need to create a scheme that is secure for at least the numbers within this range. Make sure you don't encrypt any other numbers.

Then you need to specify an encoding of the number to binary. It's probably easiest to make sure you encode to octets instead of bits. Handling odd bits is tricky at best, especially since most crypto API's are defined for bytes instead of bits. A good scheme would be to encode the number as signed or unsigned big endian (network order) value with a static size of 32 bits / 4 bytes.

Now you need to decide on an encryption mode for AES. You may or may not need a mode that uses an IV, and you may or may not need an authenticated scheme. It's hard to tell without any other requirements.

If you've chosen ECB or CBC then you need to choose a padding scheme as well. The most common one is PKCS#7 compatible padding. If you're still working with bits then bit padding is probably better.

Alternatively to choosing a mode and padding you may want to have a look at format preserving encryption or FPE.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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