Lithe Crypt: Simplifying Encryption in PHP Applications

Lithe Crypt is a simple encryption and decryption utility for PHP, designed to work with the Lithe framework. It utilizes the AES-256-CBC algorithm for secure data handling.

Installation

To install the Lithe Crypt package, you can use Compo…


This content originally appeared on DEV Community and was authored by Lithe

Lithe Crypt is a simple encryption and decryption utility for PHP, designed to work with the Lithe framework. It utilizes the AES-256-CBC algorithm for secure data handling.

Installation

To install the Lithe Crypt package, you can use Composer. If you haven't installed it yet, 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

Usage

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;

// Load environment variables
Env::load(__DIR__); // Adjust the path as necessary

Setting the APP_KEY

Ensure that the APP_KEY environment variable is set. This key should be a base64 encoded string of 32 bytes. You can configure it in your .env file or directly in your 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 = "sensitive data";

// Encrypt without fixed IV
$encrypted = Crypt::encrypt($data);
echo "Encrypted Data: " . $encrypted;

// Encrypt with fixed IV (useful for unique values like emails)
$encryptedWithSameIV = Crypt::encrypt($data, true);
echo "Encrypted Data with Fixed IV: " . $encryptedWithSameIV;

Decrypting Data

To decrypt the previously encrypted data, use the decrypt method. You must specify the same parameters used during encryption to ensure proper decryption:

use Lithe\Support\Security\Crypt;

// Decrypt without fixed IV
$decrypted = Crypt::decrypt($encrypted);
echo "Decrypted Data: " . $decrypted;

// Decrypt with fixed IV
$decryptedWithSameIV = Crypt::decrypt($encryptedWithSameIV, true, $data);
echo "Decrypted Data with Fixed IV: " . $decryptedWithSameIV;

Exception Handling

If the APP_KEY is not set or is invalid, the Crypt class will throw a CryptException. It's essential to handle this exception in your code to avoid unexpected errors:

use Lithe\Exceptions\Encryption\CryptException;

try {
    $encrypted = Crypt::encrypt($data);
    // Decrypt without fixed IV
    $decrypted = Crypt::decrypt($encrypted);
} catch (CryptException $e) {
    echo "Encryption Error: " . $e->getMessage();
}

Final Considerations

Lithe Crypt provides a practical and secure way to handle encryption and decryption of data in your PHP applications. With the implementation of the AES-256-CBC algorithm and easy integration with the Lithe framework, you can effectively protect your data. Try it out and see how it can enhance your application's security!

If you have any questions or suggestions, feel free to comment below!


This content originally appeared on DEV Community and was authored by Lithe


Print Share Comment Cite Upload Translate Updates
APA

Lithe | Sciencx (2024-11-04T23:39:10+00:00) Lithe Crypt: Simplifying Encryption in PHP Applications. Retrieved from https://www.scien.cx/2024/11/04/lithe-crypt-simplifying-encryption-in-php-applications/

MLA
" » Lithe Crypt: Simplifying Encryption in PHP Applications." Lithe | Sciencx - Monday November 4, 2024, https://www.scien.cx/2024/11/04/lithe-crypt-simplifying-encryption-in-php-applications/
HARVARD
Lithe | Sciencx Monday November 4, 2024 » Lithe Crypt: Simplifying Encryption in PHP Applications., viewed ,<https://www.scien.cx/2024/11/04/lithe-crypt-simplifying-encryption-in-php-applications/>
VANCOUVER
Lithe | Sciencx - » Lithe Crypt: Simplifying Encryption in PHP Applications. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/04/lithe-crypt-simplifying-encryption-in-php-applications/
CHICAGO
" » Lithe Crypt: Simplifying Encryption in PHP Applications." Lithe | Sciencx - Accessed . https://www.scien.cx/2024/11/04/lithe-crypt-simplifying-encryption-in-php-applications/
IEEE
" » Lithe Crypt: Simplifying Encryption in PHP Applications." Lithe | Sciencx [Online]. Available: https://www.scien.cx/2024/11/04/lithe-crypt-simplifying-encryption-in-php-applications/. [Accessed: ]
rf:citation
» Lithe Crypt: Simplifying Encryption in PHP Applications | Lithe | Sciencx | https://www.scien.cx/2024/11/04/lithe-crypt-simplifying-encryption-in-php-applications/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.