Home  >  Article  >  Backend Development  >  Lithe Crypt: Simplifying Cryptography in PHP Applications

Lithe Crypt: Simplifying Cryptography in PHP Applications

DDD
DDDOriginal
2024-11-05 22:01:03210browse

Lithe Crypt: Simplificando a Criptografia em Aplicações PHP

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.

Installation

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

Requirements

  • PHP 8 or higher
  • OpenSSL extension enabled in your PHP installation

Use

Loading Environment Variables

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

Setting the APP_KEY

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=

Encrypting Data

To encrypt data, use the encrypt method of the Crypt class. You can also specify whether you want 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;

Decrypting Data

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;

Exception Handling

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();
}

Final Considerations

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!

The above is the detailed content of Lithe Crypt: Simplifying Cryptography in PHP Applications. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn