This repository has been archived on 2024-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
kpcrypt/README.md

121 lines
3.7 KiB
Markdown
Raw Normal View History

2021-02-27 13:52:14 +02:00
## About
The Project is a simple php encryption wrapper, not even a library, the whole point is that it has integrity checks on the encryption part which helps out quite a bit with errors.
## Usage
Simply use the provided functions from the lib.php file, you can simply import it into any project using that file.
2021-02-28 14:13:25 +02:00
#### String Encryption
2021-02-27 13:52:14 +02:00
```php
2021-02-28 11:08:53 +02:00
<?php
require_once("./lib.php");
@require_once("./config.inc.php");
2021-02-27 13:52:14 +02:00
// Check if the config succesfully loaded, or if the mandatory config fields are missing.
if ( empty($config) || empty($config['key']) || empty($config['input']) ) {
echo "Please copy the config.sample.inc.php to config.inc.php and change the configuration to match your needs.";
exit(1);
}
2021-02-27 13:52:14 +02:00
2021-02-28 11:09:42 +02:00
// The memory limit has been set so that we can pass it big strings and test how much it can/will use.
ini_set('memory_limit','1G');
2021-02-27 13:52:14 +02:00
function limitStringSize($str, $amount = 20) {
if (strlen($str) > $amount)
$str = substr($str, 0, $amount) . '...';
return $str;
}
2021-02-27 13:52:14 +02:00
$kpc = new kpcrypt($config['key']);
echo "Key: " . $kpc->getKey() . " \n";
// Echo out the results
$encryptedData = $kpc->encryptData($config['input'], 'AES-256-CBC', TRUE);
echo "Encrypted: " . limitStringSize($encryptedData);
2021-02-27 13:52:14 +02:00
echo "\n";
echo "Decrypted: " . limitStringSize($kpc->decryptData($encryptedData));
2021-02-27 13:52:14 +02:00
echo "\n";
echo "Peak Memory: " . memory_get_peak_usage() / 1024 / 1024 . "Mb"; // Check the memory in kb
echo "\n\n";
2021-02-27 13:52:14 +02:00
// Calculate the string size increse
$inputSize = strlen($config['input']); // Bytes
$outputSize = strlen($encryptedData); // Bytes
// Echo out sizing information
echo "Sizeof input: " . strlen($config['input']) . "\n"; // Bytes
echo "Sizeof output:" . strlen($encryptedData) . "\n"; // Bytes
echo "\n";
2021-02-27 13:52:14 +02:00
```
2021-02-28 14:13:25 +02:00
#### File Encryption
```php
<?php
require("./lib.php");
/**
* This is a simple example of how to encrypt files with the library
* The following code will read the file in chunks instead of loading
* the whole thing in memory, trying to keep the "Peak Memory Usage"
* as low as possible.
*/
define("INPUT_FILE", "./lib.php");
define("OUTPUT_FILE", "./testdir/" . "./example.enc.php");
define("DEC_OUTPUT_FILE", "./testdir/" . "./example.dec.php");
// Make sure the testing folder exists
if ( !file_exists("./testdir") ) {
mkdir("./testdir", 0777, true);
}
// Initialize the class
$lib = new kpcrypt();
// $lib->setEncryptionBlocks(128); // This will take ~10 times more time, but will use half as much memory.
// Really the best value is the default one.
$enc_start = round(microtime(true) * 1000);
// Encrypt the file
$lib->encryptFile(INPUT_FILE, OUTPUT_FILE);
$enc_end = round(microtime(true) * 1000) - $enc_start;
$dec_start = round(microtime(true) * 1000);
// Decrypt the file as well
$lib->decryptFile(OUTPUT_FILE, DEC_OUTPUT_FILE);
$dec_end = round(microtime(true) * 1000) - $dec_start;
/**
* NOTE: You can also compress the output using gzip
*/
echo "\n\n";
echo "Input File Size: " . round(filesize(INPUT_FILE) / 1024 / 1024, 2) . "Mb";
echo "\n";
echo "Output File Size: " . round(filesize(OUTPUT_FILE) / 1024 / 1024, 2) . "Mb";
echo "\n";
echo "Peak Memory: " . memory_get_peak_usage() / 1024 / 1024 . "Mb"; // Check the memory in Mb
echo "\n";
echo "Encryption Time: $enc_end ms\n"; // 9700k = ~210ms ( 4Mb = 100ms )
echo "Decryption Time: $dec_end ms\n";
echo "\n";
$errors = $lib->getErrors();
echo "Errors: " . ( !empty($errors) ? "\n" . json_encode($errors, JSON_PRETTY_PRINT) : "No Errors!");
```
2021-02-27 13:52:14 +02:00
## License
2021-02-27 13:57:32 +02:00
The licensing of the project is the [MIT license](https://mit-license.org/)
#### Credit
2021-02-28 11:10:23 +02:00
Repository icon made by [monkik](https://www.flaticon.com/authors/monkik) from [www.flaticon.com](https://www.flaticon.com/);