Laravel APP_KEY generator

This Laravel APP_KEY generator creates a random Base64-encoded encryption key, in a line ready to paste into the .env file of your application.
Encryption key for the .env file

The key is generated in the browser. Please enable JavaScript.

Everything runs in your browser; nothing is sent or stored.

  • In the "Cipher" section, select the cipher that your application uses. AES-256-CBC is selected by default.
  • The "APP_KEY line to paste" field shows a line with a new key, which is generated when the page opens and every time you change the cipher.
  • Click "Generate a new key" to replace the key in the field, and click "Copy" to copy the whole line.
  • In the .env file, replace the existing APP_KEY line with the copied line. Changing APP_KEY on a live site logs out all logged-in users, and data encrypted with the previous key can no longer be decrypted.
  • With AES-128-CBC, also set the cipher in the config/app.php file, as described under the field.

What APP_KEY is used for

Laravel uses the APP_KEY value as the encryption key of the application. The key encrypts the cookies that the application sends to the browser, including the session cookie, and the data that your code encrypts with the encrypt() function or with the encrypted cast in Eloquent models. Laravel also uses the key to create signed URLs, such as email verification links. User passwords are stored as hashes that do not depend on the key, so changing the key does not affect them.

Key format and the cipher

The key consists of the base64: prefix followed by random bytes encoded in Base64. The prefix tells Laravel to decode the Base64 value before it uses the key. The key length must match the cipher set in the config/app.php file: AES-256-CBC requires a 32-byte key, and AES-128-CBC requires a 16-byte key. When the length does not match the cipher, the application shows the error Unsupported cipher or incorrect key length.

Keeping the key secret

Anyone who has the key can decrypt the encrypted data of the application and forge cookies and signed URLs. That is why the .env file should never be stored in a Git repository, and each environment, such as development and production, needs its own key. On the server, restrict the permissions of the .env file and make sure that the file is not accessible from the internet, for example by setting the public folder of the application as the document root of the site.

  • php artisan key:generategenerates a new key and writes it to the APP_KEY line in the .env file.
  • php artisan key:generate --showdisplays a new key without changing the file.
  • php artisan config:clearclears the cached configuration, so that the application reads the new key from the file.
Did you know?

With a Laravel VPS, FrankenPHP, Laravel Octane, and Caddy with a WAF module are installed and configured when the server is set up.

Laravel VPS

Laravel APP_KEY generator FAQ

What does the error "No application encryption key has been specified" mean?

The error appears when the APP_KEY value in the .env file is empty or missing. Paste the line that the generator created into the file, or run the command php artisan key:generate from the root folder of the application.

Why does the application not pick up the new key?

When the configuration of the application is cached with the php artisan config:cache command, the application does not read the .env file, so after you update the key, run the command again. In an application that runs on Laravel Octane, also reload the worker processes, because they keep the configuration in memory.

What is the difference between AES-256-CBC and AES-128-CBC?

AES-256-CBC uses a 32-byte (256-bit) key, and AES-128-CBC uses a 16-byte (128-bit) key. In Base64, a 32-byte key is 44 characters long, and a 16-byte key is 24 characters long. AES-256-CBC is the default cipher in the config/app.php file of a new Laravel application, so in a new application you do not need to change the cipher setting.

Can I change the key without logging out users?

In recent versions of Laravel, you can move the previous key to the APP_PREVIOUS_KEYS variable in the .env file, and separate several previous keys with commas. Laravel uses the previous keys to decrypt cookies and data that were encrypted before the change, and encrypts new data with the new key, so users stay logged in.

Is the base64: prefix part of the key?

The prefix is part of the APP_KEY value, so copy the whole line, including the prefix. Without the prefix, Laravel uses the Base64 characters themselves as the key, and the number of characters is greater than the number of bytes that the cipher requires.

Do I need to generate a new key when I move an application to another server?

When you move an existing application, copy the same key to the new server, so that the encrypted data and the cookies of the users keep working. A new key is needed for a new application, or when the existing key has been exposed.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement