1

I am encrypting data using AES 256 encryption, want to know how can i restrict length of the encrypted text. if i encrypt 6 character string using AES 256 it gets encrypted around 100 character. I want to add this data in table so wanted to know the maximum length so that i can set constraint in table in php codeigniter.

$this->encryption->initialize(
                array(
                'cipher' => 'aes-256',
                'mode' => 'ECB',
                'key' => $key1
                )
            ); 

$this->encryption->encrypt($_POST['uname']);
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Shaikh Azhar Alam
  • 173
  • 1
  • 3
  • 16

1 Answers1

1

Best guess given minimal information is that you are treating encrypted data as a null terminated character string and since the encryption does not null terminate the encrypted data the null terminator is something that just happens to be in memory falling the encrypted data.

The following assume AES in ECB mode.

  1. AES does not encrypt characters, it encrypts data bytes. If you are trying to handle the encrypted output as characters that is incorrect and will generally produce errors. It character output is required the encrypted data needs to be encoded, generally with Base64 or hexadecimal.
  2. AES is a block cipher and as such the output will always be a multiple of the block length, 16-bytes for AES.
  3. If the input is not an exact multiple of the block size it needs to e padded, PKCS#7 is the generally used padding.
  4. Data encrypted with AES will be at most one block length larger than the input due to padding.
zaph
  • 111,848
  • 21
  • 189
  • 228
  • please help here https://stackoverflow.com/questions/66757704/aes256-encryption-in-swift-string-not-matching – Amit Mar 23 '21 at 05:29