Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce the encrypted string length in codeigniter

When i try to encrypt a string using encryption library by CI, the returned string is very big,around 178 chars long. Is there is any method to reduce the length of the string. default cipher is: AES-128.

Suppose: $data=$this->encryption->encrypt("welcome to ooty"); it returns 178 length string value.. i need it to be reduced under 20

Update: When I encrypt a number, say 6 , it returns 178 long string.

like image 346
Tibin Avatar asked Mar 29 '16 08:03

Tibin


People also ask

How to encrypt decrypt string in CodeIgniter?

Show activity on this post. $this->load->library('encrypt');//load this library. $config['encryption_key'] = "YOUR KEY"; // application/config/config. php $this->encrypt->encode();//Data encryption and returns it as a string $this->encrypt->decode();//Decrypts an encoded string.

How to use encrypt in CodeIgniter?

Encrypting and decrypting data with the already configured library settings is simple. As simple as just passing the string to the encrypt() and/or decrypt() methods: $plain_text = 'This is a plain-text message! '; $ciphertext = $this->encryption->encrypt($plain_text); // Outputs: This is a plain-text message!

How many characters can AES encrypt in block?

Plain AES is a block cipher, which can only encrypt 128-bit blocks (i.e. 16 bytes at once).

How to create encryption key in CodeIgniter?

// $key will be assigned a 16-byte (128-bit) random key $key = $this->encryption->create_key(16); The key can be either stored in your application/config/config. php, or you can design your own storage mechanism and pass the key dynamically when encrypting/decrypting.


2 Answers

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.

like image 109
zaph Avatar answered Sep 17 '22 17:09

zaph


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.

like image 41
Narf Avatar answered Sep 20 '22 17:09

Narf