phicorp/cipher

Cipher: A tool for easier working with cryptography for PHP language

1.0.1 2023-10-05 22:06 UTC

This package is auto-updated.

Last update: 2024-06-06 13:05:09 UTC


README

A tool for easier working with cryptography for PHP language

Installation

Install Cipher with composer

composer require phicorp/cipher

Features

  • Easy to use
  • Symmetric encryption and decryption with powerful AES algorithm
  • Asymmetric encryption and decryption with RSA algorithm
  • A variety of hash algorithms such as SHA and Bcrypt
  • And more features

Usage/Examples

  • Symmetric encryption and decryption:
$key = Cipher::AESKEY();
$iv = Cipher::AESIV();

$plaintext = 'Hello World!';

$ciphertext = encrypt($plaintext, $key, $iv);

$decryptedText = decrypt($ciphertext, $key, $iv);

echo "Original: $plaintext\n";
echo "Encrypted: $ciphertext\n";
echo "Decrypted: $decryptedText\n";

Or

$key = Cipher::AESKEY();
$iv = Cipher::AESIV();

$plaintext = 'Hello World!';

$ciphertext = Cipher::AES()->key($key)->iv($iv)->encrypt($plaintext);

$decryptedText = Cipher::AES()->key($key)->iv($iv)->decrypt($ciphertext);

echo "Original: $plaintext\n";
echo "Encrypted: $ciphertext\n";
echo "Decrypted: $decryptedText\n";
  • GCM Mode symmetric encryption and decryption
$key = Cipher::AESKEY("AES-256-GCM");
$iv = Cipher::AESIV();
$tag = null;

$plaintext = 'Hello World!';

$ciphertext = Cipher::AES('GCM')->key($key)->iv($iv)->encrypt($plaintext, 0, $tag);

$decryptedText = Cipher::AES('GCM')->key($key)->iv($iv)->decrypt($ciphertext, 0, $tag);

echo "Original: $plaintext\n";
echo "Encrypted: $ciphertext\n";
echo "Decrypted: $decryptedText\n";
  • Examples for asymmetric encryption and decryption
[$publicKey, $privateKey] = Cipher::RSAKEY();

$plaintext = 'hello world!';

$ciphertext = RSA($publicKey, $privateKey)->encryptPublic($plaintext);
$decryptedText = RSA($publicKey, $privateKey)->decryptPrivate($ciphertext);

echo "Original: $plaintext\n";
echo "Encrypted: $ciphertext\n";
echo "Decrypted: $decryptedText\n";

// You can also use it in this way

[$publicKey, $privateKey] = Cipher::RSAKEY();

$plaintext = 'hello world!';

$ciphertext = Cipher::RSA()->privateKey($privateKey)->encryptPrivate($plaintext);
$decryptedText = RSA()->publicKey($publicKey)->decryptPublic($ciphertext);

echo "Original: $plaintext\n";
echo "Encrypted: $ciphertext\n";
echo "Decrypted: $decryptedText\n";
  • Example of using hash functions
sha512('Hello World!');
//output: 861844d6704e...
sha256('Hello World!');
//output: 7f83b1657ff1...
$hash = bcrypt('password', 15);

var_dump(bcrypt_verify('password', $hash));
// output: true

Authors

License

MIT