How can I dynamically change the keys that Crypt uses in Laravel?

前端 未结 1 1278
走了就别回头了
走了就别回头了 2021-01-14 00:34

I have been researching how to use Laravel Encryption as building a homestead encryption platform is frowned upon and rightfully so.

Illuminate\\Support\\Fac         


        
相关标签:
1条回答
  • 2021-01-14 01:01

    I would recommend against using the Crypt facade directly and would instead recommend using the Laravel Illuminate\Encryption\Encrypter which is the class that is used for the Crypt facade (I'm on Laravel 5.6).

    Here is a little code snippet that I hope will help:

    use Illuminate\Encryption\Encrypter;
    
    //Keys and cipher used by encrypter(s)
    $fromKey = base64_decode("from_key_as_a_base_64_encoded_string");
    $toKey = base64_decode("to_key_as_a_base_64_encoded_string");
    $cipher = "AES-256-CBC"; //or AES-128-CBC if you prefer
    
    //Create two encrypters using different keys for each
    $encrypterFrom = new Encrypter($fromKey, $cipher);
    $encrypterTo = new Encrypter($toKey, $cipher);
    
    //Decrypt a string that was encrypted using the "from" key
    $decryptedFromString = $encrypterFrom->decryptString("gobbledygook=that=is=a=from=key=encrypted=string==");
    
    //Now encrypt the decrypted string using the "to" key
    $encryptedToString = $encrypterTo->encryptString($decryptedFromString);
    

    If you would like to see the facade loading code it is in vendor\laravel\framework\src\Illuminate\Encryption\EncryptionServiceProvider.

    0 讨论(0)
提交回复
热议问题