"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Lithe Crypt: Simplifying Cryptography in PHP Applications

Lithe Crypt: Simplifying Cryptography in PHP Applications

Published on 2024-11-10
Browse:765

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 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!

Release Statement This article is reproduced at: https://dev.to/lithephp/lithe-crypt-simplificando-a-criptografia-em-aplicacoes-php-27jj?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

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