mmdm/sim-crypt

A simple yet nice encryption/decryption library

v1.1.2 2021-04-04 05:50 UTC

This package is auto-updated.

Last update: 2024-05-04 12:57:21 UTC


README

A library to encrypt/decrypt your data with two keys and two cipher methods.

Install

composer

composer require mmdm/sim-crypt

Or you can simply download zip file from github and extract it, then put file to your project library and use it like other libraries.

Just add line below to autoload files:

require_once 'path_to_library/autoloader.php';

and you are good to go.

How to use

// to instance a crypt object
$crypt = new Crypt($main_key, $assured_key);

// to crypt data
$encrypted_message = $crypt->encrypt("message to encrypt");

// to encrypt data
$decrypted_message = $crypt->decrypt($encrypted_message);

// "message to encrypt" will be equal to $decrypted_message at the end

Description

To instantiate crypt object, you need two keys. One is the main key and the other is assured key. These keys must be base64 coded strings. You can build them using this website. First generate strong passwords then use base64 encoder/decoder to make them as base64 coded strings and use them for library or you can choose two strings as password and use php function base64_encode() to make them base64.

Available functions

setFirstEncryptionMethod(string $first_method)

You can set first encryption method from valid encryption methods. See openssl_get_cipher_methods() PHP built-in function for supported methods.

getFirstEncryptionMethod(): string

Get first encryption method.

setSecondEncryptionMethod(string $second_method)

You can set second encryption method from valid encryption methods. See openssl_get_cipher_methods() PHP built-in function for supported methods.

getSecondEncryptionMethod(): string

Get second encryption method.

encrypt($data): ?string

This method encrypts a message and return encrypted value or null if $data is an empty string

$encrypted_message = $crypt->encrypt($message_to_encrypt);

decrypt($data): ?string

This method decrypts an encrypted message and return actual message or null if $data is an empty string or an error happened during decode step.

$decrypted_message = $crypt->decrypt($encrypted_message);

hasError()

This function return a boolean indicates operation has error or not.

Note: This method should call after encrypt or decrypt methods to take action on error occurrence.

$bool = $crypt->hasError(); // true on having error or false on OK

License

Under MIT license.