keinos/cypher-string

用于加密/解密字符串的简单类。(RSA, SHA-512, 4096位

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

This package is auto-updated.

Last update: 2024-09-16 07:43:34 UTC


README

Cypher String

一个简单的PHP类,用于使用RSA (SHA-512, 4096位) 加密/解密字符串。

安装

复制源代码或使用"Composer"来保持源代码更新。

composer require keinos/cypher-string

使用方法

<?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;
}

高级使用

  • 使用密码短语创建密钥对。

    $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);

    重要:请注意,如果没有设置密码短语或提供空字符串,则将使用默认密码。由于PHPbug #73833,在PHP 7.1.23测试中出现。

信息