MEncoderDecoder
Overview
MEncoderDecoder is a PHP class that provides simple methods for encrypting and decrypting strings. It relies on PHP's OpenSSL extension to provide secure encryption features, both symmetric and asymmetric.
Features
- Symmetric encryption with AES-256-CBC
- Asymmetric encryption with RSA key pairs
- Simplified management of encryption/decryption operations
- Secure initialization vector (IV) generation
- Custom exception handling for errors
Prerequisites
- OpenSSL extension enabled
- For asymmetric encryption: valid RSA key pairs
Make sure the OpenSSL extension is installed and enabled in your PHP configuration. You can check this with the following command:
php -m | grep openssl
If the extension is not enabled, modify your php.ini file to uncomment or add the line:
extension=openssl
Usage
Symmetric Encryption (AES-256-CBC)
Symmetric encryption uses the same key for both encryption and decryption.
use mlib\utils\crypto\MEncoderDecoder;
use mlib\utils\crypto\MEncoderDecoderException;
try {
// Create an instance with a secret key
$key = 'MyVeryLongAndSecureSecretKey123!';
$encoder = new MEncoderDecoder($key);
// Encrypt a message
$message = 'This is a confidential message';
$encrypted = $encoder->encode($message);
echo "Encrypted message: " . $encrypted . "\n";
// Decrypt the message
$decrypted = $encoder->decode($encrypted);
echo "Decrypted message: " . $decrypted . "\n";
} catch (MEncoderDecoderException $e) {
echo "Encryption error: " . $e->getMessage() . "\n";
}
Asymmetric Encryption (RSA)
Asymmetric encryption uses a key pair: a public key for encryption and a private key for decryption.
Key Generation
First generate a key pair with OpenSSL:
# Generate a private key
openssl genrsa -out private_key.pem 2048
# Extract the public key
openssl rsa -in private_key.pem -pubout -out public_key.pem
Usage with MEncoderDecoder
use mlib\utils\crypto\MEncoderDecoder;
use mlib\utils\crypto\MEncoderDecoderException;
try {
// Read keys from files
$publicKey = file_get_contents('path/to/public_key.pem');
$privateKey = file_get_contents('path/to/private_key.pem');
// Create an instance for encryption (with public key)
$encoder = new MEncoderDecoder($publicKey, false);
// Encrypt a message (with public key)
$message = 'This is a confidential message';
$encrypted = $encoder->encode($message);
echo "Encrypted message: " . base64_encode($encrypted) . "\n";
// Create an instance for decryption (with private key)
$decoder = new MEncoderDecoder(null, false, $privateKey);
// Decrypt the message (with private key)
$decrypted = $decoder->decode($encrypted);
echo "Decrypted message: " . $decrypted . "\n";
} catch (MEncoderDecoderException $e) {
echo "Encryption error: " . $e->getMessage() . "\n";
}
Method Reference
__construct($key = null, $symetricEncoding = true, $privateKey = null)
Constructor of the MEncoderDecoder class.
$key: Key for symmetric encryption or public key for asymmetric encryption$symetricEncoding:truefor symmetric encryption (default),falsefor asymmetric encryption$privateKey: Private key for asymmetric decryption
Throws an MEncoderDecoderException if OpenSSL extension is not available or if parameters are invalid.
encode($string)
Encrypts a string.
$string: The string to encrypt- Returns: The encrypted string
Throws an MEncoderDecoderException in case of encryption error.
decode($string)
Decrypts a previously encrypted string.
$string: The string to decrypt- Returns: The decrypted string
Throws an MEncoderDecoderException in case of decryption error or if private key is missing for asymmetric decryption.
Error Handling
All errors are signaled by throwing exceptions of type MEncoderDecoderException which extends PHP's standard \Exception class. You can catch these exceptions to handle errors appropriately.
try {
$encoder = new MEncoderDecoder('my_key');
$encrypted = $encoder->encode('message');
// ...
} catch (\mlib\utils\crypto\MEncoderDecoderException $e) {
// Log the error
error_log('Encryption error: ' . $e->getMessage());
// Display a generic message to the user
echo 'An error occurred. Please try again later.';
}