Reduce the encrypted string length in codeigniter Reduce the encrypted string length in codeigniter codeigniter codeigniter

Reduce the encrypted string length in codeigniter


Encryption does not reduce the data length.

AES encryption output length depends on the mode. A streaming mode such as CTR mode will not change the length. A block mode such as ECB or CBC will need to be padded to a multiple of block length but PKCS#7 padding will only increase the length a maximum of one block size, 16-bytes for AES.

There is more going on than just encrypting the bytes. A mode such as CBC may be used and the IV (one block length) may be prepended to the encrypted data. Authentication may be added and that could add perhaps 32-bytes. There may be password derivation and the salt and count may be added. Finally the result may be encoded to Base64 or hexadecimal which would increase the length respectively 33% or 100%.

Potential case: "welcome to ooty" is 15 bytes. padding is 1 byte, authentication 32-bytes, salt 32-bytes, count 2-bytes, version 1-byte = 83-bytes, hex encoded = 166-bytes, close to the 178 bytes you are getting.

All this extra buys security. Depending on you use it may not all be necessary, consult a cryptographic domain expert.


You could use a different combination of cipher, cipher-mode and HMAC algorithm that would add less data overhead, but no - the resulting cipherText won't be reduced to 20 - the HMAC alone will result in at least 28 bytes.

Also, judging by your description ("around 178 characters"), the plainText itself is longer than 20 bytes ... encryption isn't compression, you can't expect the resulting cipherText to have a smaller length than the plainText.


Well you could do substr($encodedString, 0, 20) but this would be a VERY BAD IDEA™

You would be greatly reducing the entropy of the encrypted string, and thus the security of that encryption. It's that long for a reason!