Vigyata.AI
Is this your channel?

New in Laravel 11 - Encryption Key Rotation

1.9K views· 59 likes· 2:16· Mar 17, 2024

🛍️ Products Mentioned (2)

If you change your application's encryption key, all authenticated user sessions will be logged out of your application. This is because every cookie, including session cookies, are encrypted by Laravel. In addition, it will no longer be possible to decrypt any data that was encrypted with your previous encryption key. To mitigate this issue, Laravel allows you to list your previous encryption keys in your application's APP_PREVIOUS_KEYS environment variable. This variable may contain a comma-delimited list of all of your previous encryption keys. When you set this environment variable, Laravel will always use the "current" encryption key when encrypting values. However, when decrypting values, Laravel will first try the current key, and if decryption fails using the current key, Laravel will try all previous keys until one of the keys is able to decrypt the value. This approach to graceful decryption allows users to keep using your application uninterrupted even if your encryption key is rotated. === Offer === GET 30% DISCOUNT ON ALL PLANS OF CLOUDWAYS HOSTING FOR 3 MONTHS https://unified.cloudways.com/signup?id=1029148&coupon=CW30FOR3 === Important Links === Fonts, extensions I use, and Support Laratips links: https://bit.ly/m/laratips === Video Related Links === === Other Videos === What's New In Laravel 11 Playlist https://www.youtube.com/playlist?list=PL2DahmvUpeutmODuAUlZW5xnxIn6NvI19 What's New In Laravel 10 Playlist https://www.youtube.com/playlist?list=PL2DahmvUpeut2-6VlOlGRDyBx9BfzCeA5 What's New In Laravel 9 Playlist https://www.youtube.com/playlist?list=PL2DahmvUpeuvkNJk9rIu87kgqIEUzt0bS What's New In Laravel 8 Playlist https://www.youtube.com/playlist?list=PL2DahmvUpeuu4UVWrcxlvVkMiI0SeS2OZ Laravel Package Development Playlist https://www.youtube.com/playlist?list=PL2DahmvUpeuu8HkpbNT-cKernolVdqZq_ Password Validation In Laravel with at least one Letters, Numbers, Upper and Lowercase or Symbols: https://youtu.be/f-g3P9imPZs Multiple Apis With ForwardsCalls Trait And Code Refactoring Tip - Laravel Hidden Feature: https://youtu.be/vzgNm86ezWE Laravel Chunking - You Might Be Doing This Wrong - Problem and Solution: https://youtu.be/aQCHmB4Uh0Q Laravel Tip - Properly Get Data Between Two Dates https://youtu.be/bJkEBTg1NGA Get Data From Multiple Databases In One Project | Laravel Tip https://youtu.be/KJAXLf78QPs #laravel #php #laraveltip #laratips

About This Video

In this video I’m showing you a new Laravel 11 feature: encryption key rotation. I start by demoing a super simple setup in web.php with two routes—one to encrypt whatever you pass in a data query param, and another to decrypt it. You’ll see how Laravel can encrypt a string, then successfully decrypt the encrypted value back to the original text, all using the APP_KEY behind the scenes. Then I explain the real-world problem: sometimes you need to change your APP_KEY (for example, after a security issue). But if you rotate the key the old way, anything encrypted with the previous key—cookies, sessions, and even encrypted database values—can break, and you’ll hit errors like “MAC is invalid.” Laravel 11 solves this with APP_PREVIOUS_KEYS. I duplicate the old key into APP_PREVIOUS_KEYS, generate a new APP_KEY, and show that Laravel will encrypt with the new key but still decrypt older values by trying previous keys. The takeaway is simple: you can rotate keys more safely without immediately breaking existing encrypted data.

Frequently Asked Questions

🎬 More from Laratips