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

freddyheppell's avatar

Encrypt with specific key

I want to encrypt a string with a specific key, different for each encryption. I found a stackoverflow answer saying that this would work:

Crypt::setKey($encryption_key);
$encrypted_message =  Crypt::encrypt( $message );

It seems that the setKey method was removed in 5.0 however. How can I achieve this in 5.2?

0 likes
1 reply
skliche's avatar
skliche
Best Answer
Level 42

@freddinator This should still work:

$newEncrypter = new \Illuminate\Encryption\Encrypter( $theOtherKey, Config::get( 'app.cipher' ) );
$encrypted = $newEncrypter->encrypt( $plainTextToEncrypt );
$decrypted = $newEncrypter->decrypt( $encrypted );
14 likes

Please or to participate in this conversation.