Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

untymage's avatar

How to shorten laravel encryption string?

I'm not familiar in hash/encryption world, So i wanna know if there is a way to reduce number of laravel encryption string ? this is so long...

eyJpdiI6IlhaZVpoVTBqTVNaRkNnMytNWTdwR3c9PSIsInZhbHVlIjoiL1Nmam1wUmFyUnVBQXNTTlU1OEp1TTJNY2c5Z1Yw5QlRteHpZbXlDREdIdz0iLCJtYWMiOiI0YTVmMzRiYjZjY2I5NDVjMTA5MWZhYmFhZGZmYTMwYTFmZWM4NDZlNzg0NDU4MWYxZjdhZTY0OTUwNjhmMTc2In0

As i want to pass it for referral query string i wanna something user friendlier and shorter,

i tried base64_encode() on the laravel enctryption string in order to produce fewer character but it also generate long string, Any idea ?

0 likes
10 replies
martinbean's avatar

@untymage That’s not how encryption works. Encryption is based on algorithms, and algorithms tend to generate cipher text (encrypted values) of a certain length.

Use something more appropriate if you want to use it as a referral ID.

1 like
untymage's avatar

I dont wanna modify laravel encryption string, Just looking for some method in php to pass the laravel encryption string into it then decrypt it (and give me shorten string than laravel). something like base64_encode() and base64_decode()

martinbean's avatar

@untymage Again, use something more appropriate. You can’t shorten a string of encrypted text without losing data.

2 likes
jlrdw's avatar

When you say any idea, first of all is it required to use standard encryption, for example some government entities you may work for require a certain type of encryption so no you cannot shorten it.

If this is in house and you using your own encryption simply write some sort of special class and use your own encryption.

Or if encryption isn't even required, Implement real good authentication and authorization.

But if you are coding and having to follow a spec, you need to adhere strictly to the spec.

1 like
codonist's avatar

There are all philosophers, no one replied a "to the point" answer.

Snapey's avatar

@codonist you signed up to leave that comment on a two year old thread ?

Where is your solution ?

AddWebContribution's avatar

@untymage

Laravel uses the encrypt() function to encrypt a string. The encrypted string is typically longer than the original string because encryption involves adding additional characters to ensure the string is not easily reversible. However, you can use the substr() function to shorten the encrypted string to a specified length. Here's an example of how you can shorten a Laravel encrypted string:

use Illuminate\Support\Facades\Crypt;

$string = 'Hello World!';

$encrypted = Crypt::encrypt($string);

$shortened = substr($encrypted, 0, 10); // Get the first 10 characters of the encrypted string

In the example above, the substr() function is used to get the first 10 characters of the encrypted string. You can change the second parameter of substr() to specify the number of characters you want to extract. Note that shortening the encrypted string may reduce the security of the encryption, so you should be cautious about how much you shorten the string.

Snapey's avatar

@untymage the answer given (to a two-year old question) is too inaccurate for chatGPT

Even Lary knows you cannot just shorten an encryption string and have it still have any validity.

1 like

Please or to participate in this conversation.