Lithe Crypt is a simple encryption and decryption utility in PHP, designed to work with the Lithe framework. It uses the AES-256-CBC algorithm for secure data handling.
To install the Lithe Crypt package, you can use Composer. If you don't already have it installed, make sure Composer is available on your system. Then run the following command in your project directory:
composer require lithemod/crypt
Before using the Crypt class, you need to load your environment variables. Use the following code to load your .env file:
use Lithe\Support\Env; // Carregar variáveis de ambiente Env::load(__DIR__); // Ajuste o caminho conforme necessário
Make sure the APP_KEY environment variable is set. This key must be a 32-byte base64-encoded string. You can configure it in your .env file or directly in the server environment.
Example of a valid base64 key:
YXNkZmFnc2Rhc2RmYWdlcyBhc2RmYWdlcyBhYXNkZmFnc2Q=
To encrypt data, use the encrypt method of the Crypt class. You can also specify whether to use a fixed IV (initialization vector) for encryption:
use Lithe\Support\Security\Crypt; $data = "dados sensíveis"; // Criptografar sem IV fixo $encrypted = Crypt::encrypt($data); echo "Dados Criptografados: " . $encrypted; // Criptografar com IV fixo (útil para valores únicos como e-mails) $encryptedWithSameIV = Crypt::encrypt($data, true); echo "Dados Criptografados com IV Fixo: " . $encryptedWithSameIV;
To decrypt previously encrypted data, use the decrypt method. You must specify the same parameters used during encryption to ensure correct decryption:
use Lithe\Support\Security\Crypt; // Descriptografar sem IV fixo $decrypted = Crypt::decrypt($encrypted); echo "Dados Descriptografados: " . $decrypted; // Descriptografar com IV fixo $decryptedWithSameIV = Crypt::decrypt($encryptedWithSameIV, true, $data); echo "Dados Descriptografados com IV Fixo: " . $decryptedWithSameIV;
If the APP_KEY is not defined or is invalid, the Crypt class will throw a CryptException. It is essential to handle this exception in your code to avoid unexpected errors:
use Lithe\Exceptions\Encryption\CryptException; try { $encrypted = Crypt::encrypt($data); // Descriptografar sem IV fixo $decrypted = Crypt::decrypt($encrypted); } catch (CryptException $e) { echo "Erro de Criptografia: " . $e->getMessage(); }
Lithe Crypt offers a practical and secure way to handle data encryption and decryption in your PHP applications. With the implementation of the AES-256-CBC algorithm and the ease of integration with the Lithe framework, you can protect your data effectively. Try it and see how it can improve the security of your application!
If you have any questions or suggestions, feel free to comment below!
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3