keinos/cypher-string

Simple class to encrypt/decrypt a string.(RSA, SHA-512, 4096bit

dev-master 2020-06-15 22:13 UTC

This package is auto-updated.

Last update: 2022-09-16 03:10:19 UTC


README

Cypher String

Simple PHP class to encrypt/decrypt a string with RSA (SHA-512, 4096 bit).

Install

Copy the source code or use "Composer" to keep the source up-to-date.

composer require keinos/cypher-string

Usage

<?php

require_once __DIR__ . '/../src/CypherString.php';

$path_file_conf = '/path/to/my_key_pair.json';

$cypher = new \KEINOS\lib\CypherString($path_file_conf);

$data_raw = 'Sample data';
$data_enc = $cypher->encrypt($data_raw);
$data_dec = $cypher->decrypt($data_enc);

echo 'Result enc/dec : ', ($data_raw === $data_dec) ? 'SUCCESS' : 'FAIL', PHP_EOL;
<?php

require_once __DIR__ . '/../vendor/autoload.php';

// Use of "try" and "catch" is recommended. Since the error message might contain the
// raw data, the passphrase and/or key pair info when an exception was thrown.
try {
    // File path of the key pair. It will be created if the file doesn't exist.
    $path_file_json = '/path/to/key_pair.json';

    // Creates or loads the key pair info file and instantiates the class object
    $cypher = new KEINOS\lib\CypherString($path_file_json);

    $data_raw = 'Sample data';               // Sample data to encrypt
    $data_enc = $cypher->encrypt($data_raw); // Encrypt data
    $data_dec = $cypher->decrypt($data_enc); // Decrypt data

    // View results
    echo 'Result enc/dec : ', ($data_raw === $data_dec) ? 'SUCCESS' : 'FAIL', PHP_EOL;
    echo 'Public Key     : ', $cypher->getKeyPublic(), PHP_EOL;
    echo 'Private Key    : ', $cypher->getKeyPrivate(), PHP_EOL;
    echo 'Encoded Data   : ', PHP_EOL, $data_enc, PHP_EOL;
} catch (\Exception $e) {
    echo 'Failed to encrypt/decrypt data.', PHP_EOL, $e->getMessage(), PHP_EOL;
}

Advanced usage

  • Create key pair with a pass-phrase.

    $path_file_json = '/path/to/key_pair.json';
    $passphrase = 'my passpharase to use the key pair';
    
    $cypher = new KEINOS\lib\CypherString($path_file_json, $passphrase);

    Important: Note that if the pass-phrase is not set or providing an empty string, then it will use the default password. Due to the bug #73833 of PHP that occur with PHP 7.1.23 test.

Information