First let me be clear upfront that am not Crypto expert. I'm having a problem where I will have to use NFC card. Now given the limitation of NFC cards memory sizes, I will need to constraint my system to generate encoded data up to X amount of character.

Is there a way to limit character of AES encrypted data to not exceed certain limit? It would be easy with hashes but the data have to be reversible.

  • $\begingroup$ Is there a limit on the amount of data you will have to encrypt? Also, what security guarantees do you require, just confidentiality or confidentiality+integrity? $\endgroup$ – mikeazo Dec 9 '15 at 16:37
  • $\begingroup$ Yes there will be limited maxima for data. While I would like to have integrity checks but I can live without it. Confidentiality is the critical part of it $\endgroup$ – Stefano Mtangoo Dec 9 '15 at 17:04

AES operates on 128 bits of data. We can use modes of operation to turn AES (and any block cipher for that matter) into something that can handle longer (or shorter) data. If you want the ciphertext to be limited to X bits (above you said characters, I'm switching this to bits to make things easy), X must be at least as long as the plaintext data (compression may change that, but you must be very careful).

Using a stream-like mode (which would turn AES into a stream cipher) such as CTR mode, the plaintext text length and ciphertext length will be the same. Using a mode like CBC, the ciphertext will be slightly larger (at most 255 bits longer).

The two modes I listed above only provide confidentiality guarantees. This can be devastating to security, however. Especially if you are using a stream-like mode. To add integrity, you can use something like HMAC or an authenticated encryption scheme. If at all possible, you should do this as it would prevent entire classes of attacks. Authenticated encryption will make the output of the cipher longer than the plaintext, because it adds an authentication tag to the data.

| improve this answer | |
  • $\begingroup$ Thanks for quite detailed help. As I said am not crypto guru so some things are a bit hard to grasp. But this have lighten up. If I encrypt let's say 500 characters with authenticated encryption what is possible maximum size of encoded text? $\endgroup$ – Stefano Mtangoo Dec 9 '15 at 18:21
  • 1
    $\begingroup$ It would be the size of the ciphertext plus the size of the authentication tag. The authentication tag will be anywhere from 128-bits to 256-bits. There will also usually be an initialization vector (IV) that needs to be stored (this is true of just all modes of encryption). It will be around 128-bits. So a maximum of around 48 additional characters (assuming 8 bits per character). So your 500 characters becomes 548. $\endgroup$ – mikeazo Dec 9 '15 at 19:12
  • $\begingroup$ Not so bad. Just to make sure l understand, this is the base 64 encoded text right? $\endgroup$ – Stefano Mtangoo Dec 9 '15 at 19:15
  • 1
    $\begingroup$ I'm assuming raw bits for everything. You would base64 encode after encryption, so that would add some additional expansion. I don't know what typical expansion comes with base64 though. $\endgroup$ – mikeazo Dec 9 '15 at 19:21

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.