Readouble

Laravel 11.x 暗号化

イントロダクションIntroduction

Laravelの暗号化サービスは、AES-256およびAES-128暗号化を使用してOpenSSLを介してテキストを暗号化および復号化するためのシンプルで便利なインターフェイスを提供します。Laravelの暗号化された値はすべて、メッセージ認証コード(MAC)を使用して署名されているため、暗号化された値の基になる値を変更したり改ざんしたりすることはできません。Laravel's encryption services provide a simple, convenient interface for encrypting and decrypting text via OpenSSL using AES-256 and AES-128 encryption. All of Laravel's encrypted values are signed using a message authentication code (MAC) so that their underlying value can not be modified or tampered with once encrypted.

設定Configuration

Laravelの暗号化を使用する前に、config/app.php設定ファイルでkey設定オプションを設定する必要があります。この設定値は、APP_KEY環境変数が反映されます。php artisan key:generateコマンドを使用してこの変数の値を生成する必要があります。これは、key:generateコマンドがPHPの安全なランダムバイトジェネレーターを使用して、アプリケーションの暗号的に安全なキーを構築するためです。通常、APP_KEY環境変数の値は、Laravelのインストール中に生成されます。Before using Laravel's encrypter, you must set the key configuration option in your config/app.php configuration file. This configuration value is driven by the APP_KEY environment variable. You should use the php artisan key:generate command to generate this variable's value since the key:generate command will use PHP's secure random bytes generator to build a cryptographically secure key for your application. Typically, the value of the APP_KEY environment variable will be generated for you during Laravel's installation[/docs/{{version}}/installation].

優しい暗号化キーのローテーションGracefully Rotating Encryption Keys

アプリケーションの暗号化キーを変更すると、認証済みユーザーセッションをアプリケーションから全てログアウトします。これは、セッションクッキーを含む全てのクッキーがLaravelによって暗号化されているためです。さらに、以前の暗号化キーで暗号化されたデータを復号することもできなくなります。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.

Laravelはこの問題を軽減するため.、アプリケーションのAPP_PREVIOUS_KEYS環境変数へ、以前の暗号化キーをリストアップしておくことができます。この変数に以前の暗号化キーをカンマ区切りで列挙してください。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:

APP_KEY="base64:J63qRTDLub5NuZvP kb8YIorGS6qFYHKVo6u7179stY="
APP_PREVIOUS_KEYS="base64:2nLsGFGzyoae2ax3EF2Lyq/hH6QghBGLIq5uL Gp8/w="

この環境変数を設定すると、Laravelは値を暗号化する時、常に「現在の」暗号化キーを使用します。しかし、値を復号化する場合、Laravelはまず現在のキーを試し、復号化に失敗すると、いずれかのキーで復号化できるまで、Laravelは以前のキーをすべて試します。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.

エンクリプタの使用Using the Encrypter

値の暗号化Encrypting a Value

Cryptファサードが提供するencryptStringメソッドを使用して値を暗号化できます。暗号化された値はすべて、OpenSSLとAES-256-CBC暗号を使用して暗号化されます。さらに、暗号化されたすべての値は、メッセージ認証コード(MAC)で署名されます。統合されたメッセージ認証コードは、悪意のあるユーザーにより改ざんされた値の復号化を防ぎます。You may encrypt a value using the encryptString method provided by the Crypt facade. All encrypted values are encrypted using OpenSSL and the AES-256-CBC cipher. Furthermore, all encrypted values are signed with a message authentication code (MAC). The integrated message authentication code will prevent the decryption of any values that have been tampered with by malicious users:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;

class DigitalOceanTokenController extends Controller
{
    /**
     * ユーザーのDigitalOceanAPIトークンを保存
     */
    public function store(Request $request): RedirectResponse
    {
        $request->user()->fill([
            'token' => Crypt::encryptString($request->token),
        ])->save();

        return redirect('/secrets');
    }
}

値の復号Decrypting a Value

Cryptファサードが提供するdecryptStringメソッドを使用して値を復号化できます。メッセージ認証コードが無効な場合など、値を適切に復号化できない場合、Illuminate\Contracts\Encryption\DecryptExceptionを投げます。You may decrypt values using the decryptString method provided by the Crypt facade. If the value can not be properly decrypted, such as when the message authentication code is invalid, an Illuminate\Contracts\Encryption\DecryptException will be thrown:

use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Support\Facades\Crypt;

try {
    $decrypted = Crypt::decryptString($encryptedValue);
} catch (DecryptException $e) {
    // ...
}

章選択

設定

明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

テストコード表示
両コード表示
Pestのみ表示
PHPUnitのみ表示
和文変換

対象文字列と置換文字列を半角スペースで区切ってください。(最大5組各10文字まで)

本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作