.env · security🔒 100% client-side — never leaves your browser

Laravel APP_KEY Generator

Generate a Laravel APP_KEY without running artisan: this Laravel APP_KEY generator creates 32 random bytes, base64-encodes them and adds the base64: prefix, the same format php artisan key:generate writes into your .env.

By Aliniyaz Mamat · last reviewed

Be the first to rate
Laravel APP_KEY (base64, 32 bytes)
Identical to what php artisan key:generate produces
Key value
.env line
APP_KEY=
CLI alternative (Artisan)
Generate and write to .env
php artisan key:generate
Print only (no write)
php artisan key:generate --show

Key takeaways

  • ✓Format: base64: + 44 base64 characters = 32 raw bytes, the key size AES-256-CBC (Laravel's default cipher) requires.
  • ✓APP_KEY encrypts cookies, sessions, Crypt::encrypt() values and encrypted Eloquent casts, and signs signed URLs.
  • ✓Passwords are not affected: they are hashed with bcrypt or Argon2, not encrypted with APP_KEY.
  • ✓Laravel 11+ can rotate keys gracefully with APP_PREVIOUS_KEYS, so old encrypted data stays readable.

What it does

  • ▸32-byte key in Laravel's base64: format
  • ▸Ready-to-paste APP_KEY= line for .env
  • ▸Generated with crypto.getRandomValues (OS entropy, like PHP's random_bytes)
  • ▸One-click regenerate
  • ▸Nothing is uploaded or stored

Privacy

Runs 100% in your browser. Your .env never touches our servers.

client-side only

When to use this tool

  • →Setting APP_KEY in a CI pipeline, Docker build or hosting dashboard where you cannot run artisan
  • →A cloned project that fails with "No application encryption key has been specified"
  • →Separate keys for local, staging and production
  • →Rotating a key that was committed to git or shown in an error page

Common mistakes

  • ⚠Running key:generate on production after deploy: it overwrites the key and makes existing encrypted data unreadable
  • ⚠Dropping the base64: prefix: Laravel then uses the 44-character text itself as the key and fails with "Unsupported cipher or incorrect key length"
  • ⚠Sharing one key across environments: a staging leak then exposes production cookies and encrypted columns
  • ⚠Leaving APP_DEBUG=true in production: the error page can show APP_KEY and every other .env value

What Laravel uses APP_KEY for

FeatureEffect of a new APP_KEY
Cookies (EncryptCookies middleware)all cookies unreadable, users logged out
Sessionsexisting sessions invalid
Crypt::encrypt() / encrypt() helperold values throw DecryptException
Eloquent encrypted castscolumns can no longer be decrypted
Signed URLs (URL::signedRoute)old links fail signature check
Queued jobs with ShouldBeEncryptedpending jobs fail
User passwords (Hash::make)no effect, hashes do not use APP_KEY

That table is the reason never to regenerate the key casually: encrypted database columns are lost for good unless you still have the old key.

Rotating APP_KEY in Laravel 11 and later

Since Laravel 11, the encrypter tries the current key first and then each key in APP_PREVIOUS_KEYS. New data is encrypted with the new key; old data stays readable.

.env
APP_KEY=base64:NEW_KEY_FROM_THE_GENERATOR=
# comma-separated if there are several
APP_PREVIOUS_KEYS=base64:OLD_KEY=
  1. Deploy with the new APP_KEY and the old one in APP_PREVIOUS_KEYS, then run php artisan config:cache.
  2. Re-encrypt long-lived data (encrypted casts) by loading and saving the models, for example in a one-off command.
  3. Once nothing depends on the old key, remove it from APP_PREVIOUS_KEYS.

If the old key leaked, still rotate, but treat the data encrypted with it as exposed. On Laravel 10 and older there is no APP_PREVIOUS_KEYS; you have to decrypt with the old key and re-encrypt in a script.

Generating the key in code or CI

# artisan (writes into .env)
php artisan key:generate

# print only, do not touch .env
php artisan key:generate --show

# without PHP/Laravel
echo "base64:$(openssl rand -base64 32)"

The OpenSSL line gives exactly the same format as the generator above. For the rest of the Laravel .env (database, Redis, mail), use the Laravel .env generator. For other frameworks see the Django secret key generator and the Rails secret key generator.

Frequently asked questions

What format does a Laravel APP_KEY have?

base64: followed by the base64 encoding of 32 random bytes, 51 characters in total, for example base64:Zm9v…=. Laravel strips the prefix and decodes the rest into the raw key.

Is this the same as php artisan key:generate?

Yes, the format is identical: 32 bytes from a cryptographically secure generator, base64-encoded with the prefix. The only difference is that artisan writes it into .env for you.

What happens if APP_KEY changes in production?

Users are logged out, signed URLs stop working and data encrypted with the old key cannot be decrypted. Use APP_PREVIOUS_KEYS (Laravel 11+) to keep old data readable during a rotation.

Which cipher does Laravel use?

AES-256-CBC by default (cipher in config/app.php), which needs a 32-byte key. Laravel also supports AES-128-CBC, AES-128-GCM and AES-256-GCM; the 128-bit ciphers need a 16-byte key.

Do I need quotes around APP_KEY in .env?

No. Base64 only uses letters, digits, +, / and =, none of which need quoting in Laravel's dotenv parser.

Related tools

Learn more

Compare

coming soon

Get notified when env syncing launches

We're building a tiny tool to keep .env files in sync across teammates and environments. Leave your email — no spam, just a single launch ping.

We use your address only for this launch email. You can ask us to delete it at any time. Privacy policy