Introduction | Library purposes | Installation | Usage examples | Docs | License | Support
Virgil Crypto Library PHP is a stack of security libraries (ECIES with Crypto Agility wrapped in Virgil Cryptogram) and an open-source high-level cryptographic library that allows you to perform all necessary operations for securely storing and transferring data in your digital solutions. Crypto Library is written in C++ and is suitable for mobile and server platforms.
- Asymmetric Key Generation
- Encryption/Decryption of data and streams
- Generation/Verification of digital signatures
- Double Ratchet algorithm support
- Post-quantum algorithms support: Round5 (encryption) and Falcon (signature)
- Crypto for using Virgil Core SDK
Requirements:
- PHP 8.2, 8.3
composer require virgil/crypto
Generate a key pair using the default algorithm (EC_X25519):
$crypto = new VirgilCrypto();
$keyPair = $crypto->generateKeyPair();
Generate signature and sign data with a private key:
$crypto = new VirgilCrypto();
$senderKeyPair = $crypto->generateKeyPair();
// prepare a message
$messageToSign = "Hello, Bob!";
// generate a signature
$signature = $crypto->generateSignature($messageToSign, $senderKeyPair->getPrivateKey());
Verify a signature with a public key:
$crypto = new VirgilCrypto();
$senderKeyPair = $crypto->generateKeyPair();
// prepare a message
$messageToSign = "Hello, Bob!";
// generate a signature
$signature = $crypto->generateSignature($messageToSign, $senderKeyPair->getPrivateKey());
// verify a signature
$verified = $crypto->verifySignature($signature, $messageToSign, $senderKeyPair->getPublicKey());
Encrypt data with a public key:
$crypto = new VirgilCrypto();
$receiverKeyPair = $crypto->generateKeyPair();
// prepare a message
$messageToEncrypt = "Hello, Bob!";
// encrypt the message
$encryptedData = $crypto->encrypt($messageToEncrypt, new VirgilPublicKeyCollection($receiverKeyPair->getPublicKey()));
Decrypt the encrypted data with a private key:
$crypto = new VirgilCrypto();
$receiverKeyPair = $crypto->generateKeyPair();
// prepare a message
$messageToEncrypt = "Hello, Bob!";
// encrypt the message
$encryptedData = $crypto->encrypt($messageToEncrypt, new VirgilPublicKeyCollection($receiverKeyPair->getPublicKey()));
// prepare data to be decrypted and decrypt the encrypted data using a private key
$decryptedData = $crypto->decrypt($encryptedData, $receiverKeyPair->getPrivateKey());
Export keys:
use Virgil\CryptoImpl\VirgilCrypto;
$crypto = new VirgilCrypto();
$keyPair = $crypto->generateKeys();
// export private key
$privateKeyData = $crypto->exportPrivateKey($keyPair->getPrivateKey(), "YOUR_PASSWORD");
$privateKeyStr = base64_encode($privateKeyData);
// export public key
$publicKeyData = $crypto->exportPublicKey($keyPair->getPrivateKey());
$publicKeyStr = base64_encode($publicKeyData);
Import keys:
use Virgil\CryptoImpl\VirgilCrypto;
$crypto = new VirgilCrypto();
$privateKeyStr = "MIGhMF0GCSqGSIb3DQEFDTBQMC8GCSqGSIb3DQEFDDAiBBBtfBoM7VfmWPlvyHuGWvMSAgIZ6zAKBggqhkiG9w0CCjAdBglghkgBZQMEASoEECwaKJKWFNn3OMVoUXEcmqcEQMZ+WWkmPqzwzJXGFrgS/+bEbr2DvreVgEUiLKrggmXL9ZKugPKG0VhNY0omnCNXDzkXi5dCFp25RLqbbSYsCyw=";
$privateKeyData = base64_decode($privateKeyStr);
// import a Private key
$privateKey = $crypto->importPrivateKey($privateKeyData, "YOUR_PASSWORD");
//-----------------------------------------------------
$publicKeyStr = "MCowBQYDK2VwAyEA9IVUzsQENtRVzhzraTiEZZy7YLq5LDQOXGQG/q0t0kE=";
$publicKeyData = base64_decode($publicKeyStr);
// import a Public key
$publicKey = $crypto->importPublicKey($publicKeyData);
This library is released under the 3-clause BSD License.
Our developer support team is here to help you. Find out more information on our Help Center.
You can find us on Twitter or send us email [email protected].
Also, get extra help from our support team on Slack.