1. Go to this page and download the library: Download ricwein/crypto library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ricwein / crypto example snippets
use ricwein\Crypto\Symmetric\Crypto;
use ricwein\Crypto\Symmetric\Key;
use ricwein\Crypto\Exceptions\Exception as CryptoException;
try {
$message = 'asecretmessage';
$key = Key::generate();
// actual encryption
$ciphertext = (new Crypto($key))->encrypt($message);
// now we can use the resulting key and ciphertext, e.g. safe them to the filesystem
file_put_contents(__DIR__ . '/key', $key->getKey());
file_put_contents(__DIR__ . '/message', $ciphertext->getString());
} catch (CryptoException $e) {
// something went wrong
}
use ricwein\Crypto\Ciphertext;
use ricwein\Crypto\Symmetric\Crypto;
use ricwein\Crypto\Symmetric\Key;
use ricwein\Crypto\Exceptions\Exception as CryptoException;
use ricwein\Crypto\Exceptions\MacMismatchException;
try {
$ciphertext = Ciphertext::fromString(file_get_contents(__DIR__ . '/message'));
$key = Key::load(file_get_contents(__DIR__ . '/key'));
// actual decryption
$plaintext = (new Crypto($key))->decrypt($ciphertext);
} catch (MacMismatchException $e) {
// unable to decrypt message, invalid HMAC
} catch (CryptoException $e) {
// something else went wrong
}
use ricwein\Crypto\Asymmetric\Crypto;
use ricwein\Crypto\Asymmetric\KeyPair;
use ricwein\Crypto\Exceptions\Exception as CryptoException;
try {
$message = 'asecretmessage';
$keyAlice = KeyPair::generate();
$keyBob = KeyPair::generate();
// send message from alice to bob
$ciphertext = (new Crypto($keyAlice))->encrypt($message, $keyBob->getKey(KeyPair::PUB_KEY));
// it's enough to store the private-keys of our keypairs, public-keys can be derived later if
use ricwein\Crypto\Ciphertext;
use ricwein\Crypto\Asymmetric\Crypto;
use ricwein\Crypto\Asymmetric\KeyPair;
use ricwein\Crypto\Exceptions\Exception as CryptoException;
use ricwein\Crypto\Exceptions\MacMismatchException;
try {
$keyAlice = KeyPair::load([
KeyPair::PRIV_KEY => file_get_contents(__DIR__ . '/alice.key')
]);
$keyBob = KeyPair::load([
KeyPair::PRIV_KEY => file_get_contents(__DIR__ . '/bob.key')
]);
$ciphertext = Ciphertext::fromString(file_get_contents(__DIR__ . '/message'));
// verify and decrypt the ciphertext
// it's enough to pass alice keypair with only a private key here,
// the public key will be dynamically derived to verify the ciphertexts HMAC
// BUT you can also directly pass alice public-key
$plaintext = (new Crypto($keyBob))->decrypt($ciphertext, $keyAlice);
} catch (MacMismatchException $e) {
// unable to decrypt message, invalid HMAC for alice
} catch (CryptoException $e) {
// something else went wrong
}
use ricwein\Crypto\Symmetric\Crypto;
use ricwein\Crypto\Symmetric\Key;
use ricwein\Crypto\Exceptions\Exception as CryptoException;
use ricwein\FileSystem\Exceptions\Exception as FileException;
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
try {
$file = new File(new Storage\Disk(__DIR__, 'file.txt'));
$key = Key::generate();
// actual encryption
$encryptedFile = (new Crypto($key))->encryptFile($file);
file_put_contents(__DIR__ . '/key', $key->getKey());
} catch (FileException $e) {
// unable to open, read or write the file
} catch (CryptoException $e) {
// something went wrong
}
use ricwein\Crypto\Symmetric\Crypto;
use ricwein\Crypto\Symmetric\Key;
use ricwein\Crypto\Exceptions\Exception as CryptoException;
use ricwein\Crypto\Exceptions\MacMismatchException;
use ricwein\FileSystem\Exceptions\Exception as FileException;
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
try {
$encryptedFile = new File(new Storage\Disk(__DIR__, 'file.txt'));
$key = Key::load(file_get_contents(__DIR__ . '/key'));
// actual decryption
$file = (new Crypto($key))->decryptFile($encryptedFile);
} catch (MacMismatchException $e) {
// unable to decrypt message, invalid HMAC
} catch (FileException $e) {
// unable to open, read or write the file
} catch (CryptoException $e) {
// something else went wrong
}
use ricwein\Crypto\Asymmetric\Crypto;
use ricwein\Crypto\Asymmetric\KeyPair;
use ricwein\Crypto\Exceptions\Exception as CryptoException;
use ricwein\FileSystem\Exceptions\Exception as FileException;
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
try {
$file = new File(new Storage\Disk(__DIR__, 'file.txt'));
$keyAlice = KeyPair::generate();
$keyBob = KeyPair::generate();
$plainTextFile = new File(new Storage\Disk(__DIR__, 'file.txt'));
// send message from alice to bob
$encryptedFile = (new Crypto($keyAlice))->encryptFile($plainTextFile, null, $keyBob);
// it's enough to store the private-keys of our keypairs, public-keys can be derived later if
use ricwein\Crypto\Asymmetric\Crypto;
use ricwein\Crypto\Asymmetric\KeyPair;
use ricwein\Crypto\Exceptions\Exception as CryptoException;
use ricwein\Crypto\Exceptions\MacMismatchException;
use ricwein\FileSystem\Exceptions\Exception as FileException;
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
try {
$keyAlice = KeyPair::load([
KeyPair::PRIV_KEY => file_get_contents(__DIR__ . '/alice.key')
]);
$keyBob = KeyPair::load([
KeyPair::PRIV_KEY => file_get_contents(__DIR__ . '/bob.key')
]);
$encryptedFile = new File(new Storage\Disk(__DIR__, 'file.txt'));
$file = (new Crypto($keyBob))->decryptFile($encryptedFile, null, $keyAlice);
} catch (MacMismatchException $e) {
// unable to decrypt message, invalid HMAC for alice
} catch (FileException $e) {
// unable to open, read or write the file
} catch (CryptoException $e) {
// something else went wrong
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.