Skip to content
Virgil PHP Crypto Library is a high-level cryptographic library that allows you to perform all necessary operations for secure storing and transferring data and everything required to become HIPAA and GDPR compliant.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Virgil/CryptoImpl Update README.md, add new tests Nov 13, 2018
build Fix travis-ci Nov 8, 2018
tests
.gitignore Update README.md, add new tests Nov 13, 2018
.travis.yml Fix travis-ci Nov 8, 2018
LICENSE upd package files May 14, 2018
README.md
composer.json
phpunit.xml Add Travis CI Nov 5, 2018
phpunit.xml.dist Add Travis CI Nov 5, 2018
virgil_crypto_php.php Update virgil_crypto_php file Nov 6, 2018

README.md

Virgil Security PHP Crypto Library

Build Status GitHub license

Introduction | Library purposes | Usage examples | Installation | Docs | License | Contacts

Introduction

VirgilCrypto 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.

Virgil Security, Inc., guides software developers into the forthcoming security world in which everything will be encrypted (and passwords will be eliminated). In this world, the days of developers having to raise millions of dollars to build a secure chat, secure email, secure file-sharing, or a secure anything have come to an end. Now developers can instead focus on building features that give them a competitive market advantage while end-users can enjoy the privacy and security they increasingly demand.

Library purposes

  • Asymmetric Key Generation
  • Encryption/Decryption of data and streams
  • Generation/Verification of digital signatures
  • PFS (Perfect Forward Secrecy)

Usage examples

Generate a key pair

Generate a Private Key with the default algorithm (EC_X25519):

use Virgil\CryptoImpl\VirgilCrypto;

$crypto = new VirgilCrypto();
$keyPair = $crypto->generateKeys();

Generate and verify a signature

Generate signature and sign data with a private key:

use Virgil\CryptoImpl\VirgilCrypto;

$crypto = new VirgilCrypto();

// prepare a message
$messageToSign = "Hello, Bob!";

// generate a signature
$signature = $crypto->generateSignature($messageToSign, $senderPrivateKey);

Verify a signature with a public key:

use Virgil\CryptoImpl\VirgilCrypto;

$crypto = new VirgilCrypto();

// verify a signature
$crypto->verifySignature($signature, $dataToSign, $senderPublicKey);

Encrypt and decrypt data

Encrypt Data on a Public Key:

use Virgil\CryptoImpl\VirgilCrypto;

$crypto = new VirgilCrypto();

// prepare a message
$messageToEncrypt = "Hello, Bob!";

// encrypt the message
$encryptedData = $crypto->encrypt($messageToEncrypt, $receiverPublicKey);

Decrypt the encrypted data with a Private Key:

use Virgil\CryptoImpl\VirgilCrypto;

$crypto = new VirgilCrypto();

// prepare data to be decrypted
$decryptedData = $crypto->decrypt($encryptedData, $receiverPrivateKey);

Encrypt and decrypt files (size: 2Mb+)

Encrypt file:

use Virgil\CryptoImpl\Cryptography\Cipher\VirgilSeqCipher;
        
$seqCipher = new VirgilSeqCipher();

// add recipient`s identity and public key
$seqCipher->addKeyRecipient($recipientId, $publicKey);

// path to input/output (encrypted) file
$inputFilePath = "/path/to/input.extension";
$outputFilePath = "/path/to/output.enc";

// add input/output handlers
$inputHandler = fopen($inputFilePath, "rb");
$outputHandler = fopen($outputFilePath, "w");

// add encryption header to file
fwrite($outputHandler, $seqCipher->startEncryption());

// encrypt each 1024 byts of the file content
while (!feof($inputHandler)) {
    $inputData = fread($inputHandler, 1024);
    $encryptedData = $seqCipher->process($inputData);
    if(!empty($encryptedData))
        fwrite($outputHandler, $encryptedData);
}

// add last encrypted block to the file
$lastBlock = $seqCipher->finish();
if(!empty($lastBlock))
    fwrite($outputHandler, $lastBlock);

// close input/output handlers
fclose($inputHandler);
fclose($outputHandler);

Decrypt file:

use Virgil\CryptoImpl\Cryptography\Cipher\VirgilSeqCipher;
        
$seqCipher = new VirgilSeqCipher();

// path to input/output (encrypted) file
$inputFilePath = "/path/to/input.enc";
$outputFilePath = "/path/to/output.extension";

// add input/output handlers
$inputHandler = fopen($inputFilePath, "rb");
$outputHandler = fopen($outputFilePath, "w");

// add decryption header to file and recipient`s identity and private key
fwrite($outputHandler, $seqCipher->startDecryptionWithKey($recipientId, $privateKey));

// decrypt each 1024 byts of the file content
while (!feof($inputHandler)) {
    $inputData = fread($inputHandler, 1024);
    $encryptedData = $seqCipher->process($inputData);
    if(!empty($encryptedData))
        fwrite($outputHandler, $encryptedData);
}

// add last decrypted block to the file
$lastBlock = $seqCipher->finish();
if(!empty($lastBlock))
    fwrite($outputHandler, $lastBlock);

// close input/output handlers
fclose($inputHandler);
fclose($outputHandler);

Need more examples? Visit our developer documentation.

Installation

Requirements

  • PHP 5.6 and newer
  • virgil_crypto_php extension

Add virgil_crypto_php extension before install virgil/crypto:

  • Download virgil_crypto_2.6.1 archive from the CDN according to your server operating system and PHP version
  • Place virgil_crypto_php.so file from the archive into the directory with extensions
  • Add string extension=virgil_crypto_php.so to the php.ini file
  • Restart your web-service (apache or nginx): sudo service {apache2 / nginx} restart
Tips:
  • PHP version: phpversion() / php --version
  • OS Version: PHP_OS
  • php.ini and extensions directory: phpinfo() / php -i / php-config --extension_dir

Installation via composer

composer require virgil/crypto

Docs

License

This library is released under the 3-clause BSD License.

Support

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 support@VirgilSecurity.com.

Also, get extra help from our support team on Slack.

You can’t perform that action at this time.