Character length expected in Laravel 5 Crypt function

Just a quick question, if I'm using a Laravel 5 feature Crypt::encrypt()

and I would like to store it in the database, how many characters am I expecting? Does the character length depend on the length of my message or will it be a fixed length?

I am currently using varchar 255 in my database and there are missing characters from time to time, causing problems when decrypting.

thank

+3


source to share


3 answers


From the official Laravel documentation:

Laravel provides strong AES encryption capabilities through the Mcrypt PHP Extension.

From the official PHP documentation using mcrypt_generic.

If you want to keep encrypted data in the database, make sure to save the entire string returned by mcrypt_generic, or the string will not fully decrypt properly. If your original string is 10 characters long and the block size is 8 (use mcrypt_enc_get_block_size () to determine the block size), you would need at least 16 characters in the database field. Note the string returned by mdecrypt_generic () will also be 16 characters ... use rtrim ($ str, "\ 0") to remove the padding.



More details here

So, I guess the correct answer is that the size of the characters generated by the encryption function depends on the size of the text that you process through the encryption function.

Assuming you are using MySQL, why not just use TEXT if you are parsing a lot of information? More on MySQL field types here

+7


source


It is difficult to determine the answer because it depends on your input size. But even a fixed input size gives a different size.

I created a simple script to check real world sizes for different line lengths.

Here is a GitHub example



Here's the sample output:

Testing Laravel Crypt::encrypt() result length
Number of passes: 1000000
Minimum input length: 1
Maximum input length: 32
Input length: 1 - Output length 188 - 200
Input length: 2 - Output length 188 - 200
Input length: 3 - Output length 188 - 200
Input length: 4 - Output length 188 - 200
Input length: 5 - Output length 188 - 200
Input length: 6 - Output length 188 - 200
Input length: 7 - Output length 188 - 200
Input length: 8 - Output length 188 - 200
Input length: 9 - Output length 216 - 228
Input length: 10 - Output length 216 - 228
Input length: 11 - Output length 216 - 228
Input length: 12 - Output length 216 - 228
Input length: 13 - Output length 216 - 228
Input length: 14 - Output length 216 - 228
Input length: 15 - Output length 216 - 228
Input length: 16 - Output length 216 - 228
Input length: 17 - Output length 216 - 228
Input length: 18 - Output length 216 - 228
Input length: 19 - Output length 216 - 228
Input length: 20 - Output length 216 - 228
Input length: 21 - Output length 216 - 228
Input length: 22 - Output length 216 - 228
Input length: 23 - Output length 216 - 228
Input length: 24 - Output length 244 - 256
Input length: 25 - Output length 244 - 256
Input length: 26 - Output length 244 - 256
Input length: 27 - Output length 244 - 256
Input length: 28 - Output length 244 - 256
Input length: 29 - Output length 244 - 256
Input length: 30 - Output length 244 - 256
Input length: 31 - Output length 244 - 256
Input length: 32 - Output length 244 - 256

      

Note. If you are using this yourself, you will need to set it to about 1 million passes per line length to get the actual hard minimum and maximum limits. In my testing, 500,000 was not enough. Also, the function get_random_input

outputs a maximum of 32 characters, so it will need to be modified to test longer strings.

+1


source


DOES output depends on the size of the input, so it is safer to use the TEXT datatype for your column instead of VARCHAR. To test it, take the largest possible row in the db column and run it through the encrypt () function to see how big the resulting row is. Note that if you enforce a length limitation on the original text (before encryption), you can avoid using VARCHAR.

+1


source







All Articles