- 配置
- 基本用法
配置在使用流明的加密器之前,您应该将文件APP_KEY选项设置.env为32个字符的随机字符串。如果未正确设置此值,则流明加密的所有值将不安全。基本用法加密值您可以使用Crypt外观对值进行加密。所有加密的值均使用OpenSSL和密码进行加密。此外,所有加密值都用消息认证码(MAC)签名,以检测对加密字符串的任何修改。AES-256-CBC
 例如,我们可以使用该encrypt方法对机密进行加密并将其存储在Eloquent模型中:- <?php
- namespace App\Http\Controllers;
- use App\User;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Crypt;
- class UserController extends Controller
- {
- /**
- * Store a secret message for the user.
- *
- * @param Request $request
- * @param int $id
- * @return Response
- */
- public function storeSecret(Request $request, $id)
- {
- $user = User::findOrFail($id);
- $user->fill([
- 'secret' => Crypt::encrypt($request->secret)
- ])->save();
- }
- }
 解密值当然,您可以使用外观decrypt上的方法解密值Crypt。如果无法正确解密该值(例如,当MAC无效时),将抛出:Illuminate\Contracts\Encryption\DecryptException- use Illuminate\Contracts\Encryption\DecryptException;
- try {
- $decrypted = Crypt::decrypt($encryptedValue);
- } catch (DecryptException $e) {
- //
- }
 
 
                         
                                

