# Installation

composer require buckhill/encrypter

OR, via composer.json


```
{    
    "repositories": [
      { "type": "vcs", "url": "git@gitlab02.web:packages/encrypter.git" }
    ],
    "require": {
        "buckhill/encrypter": "dev-master"
    }
}

```

# Encrypting

```php
<?php

use buckhill\encrypter\Encrypter;

// Encryption method
// List of methods: http://php.net/manual/en/function.openssl-get-cipher-methods.php
$method = 'aes-256-cbc'; 
$secret = 'this is random string, it should be strong. You can use openssl_random_pseudo_bytes(128) as well';

$encrypter = new Encrypter($method, $secret);


// Note: output is in binary format. To send it via http, base64_encode it first.
$mangled = $encrypter->encrypt("Encrypt me.");

// Note #2: to decrypt the text on the other side, send the initialization vector too (IV)
// $iv is also in binary format, you can either encode it or pass parameter "true" to the function

// This is binary IV
$iv = $encrypter->getIV();

// This is base64 encoded IV
$iv = $encrypter->getIV(true);

```


# Decrypting 

```php

<?php
use buckhill\encrypter\Encrypter;

// Text to decrypt
$encrypted = base64_decode($_GET['encrypted']);

$method = 'aes-256-cbc';
$secret = 'Remote secret';

// You should read this parameter from the HTTP request
$iv = $_GET['iv'];

$decrypter = new Encrypter($method, $secret);

// 2nd parameter is to base64_decode the $iv into binary string
$decrypter->setIV($iv, true);

try
{
	$original = $decrypter->decrypt($encrypted);
}
catch(\Exception $e)
{
	echo 'Decrypting failed: '. $e->getMessage();
}


