Skip to content

Instantly share code, notes, and snippets.

@krisanalfa
Created July 24, 2015 07:20
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save krisanalfa/ae2b1444b47aab2b2570 to your computer and use it in GitHub Desktop.
Simple class to encrypt and decrypt something with AES 256 encryption. Support for padding too!
<?php
/**
* Simple class to encrypt and decrypt something with AES 256 encryption.
*
* @author Krisan Alfa Timur <krisan47@gmail.com>
*/
class Encrypt
{
/**
* The encryption key.
*
* @var string
*/
protected $key;
/**
* The encryption iv.
*
* @var string
*/
protected $iv;
/**
* Create a new encrypter instance.
*
* @param string $key
* @param string $iv
*
* @throws \Exception If key size is not 16, 24, 32 bytes. Or if IV length is not 32 bytes.
*/
public function __construct($key, $iv)
{
if (!in_array($keyLength = strlen($this->key = $key), [16, 24, 32])) {
throw new Exception("Key of size {$keyLength} not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported.");
}
if (strlen($this->iv = $iv) !== 32) {
throw new Exception('IV size must be 32 bytes long.');
}
}
/**
* Encrypt the given value.
*
* @param string $value
*
* @return string
*/
public function encrypt($value)
{
return base64_encode($this->padAndMcrypt($value, $this->iv));
}
/**
* Decrypt the given value.
*
* @param string $payload
*
* @return string
*/
public function decrypt($payload)
{
return $this->stripPadding($this->mcryptDecrypt(base64_decode($payload)));
}
/**
* Pad and use mcrypt on the given value and input vector.
*
* @param string $value
*
* @return string
*/
protected function padAndMcrypt($value)
{
return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->key, $this->addPadding($value), MCRYPT_MODE_CBC, $this->iv);
}
/**
* Run the mcrypt decryption routine for the value.
*
* @param string $value
*
* @return string
*
* @throws \Exception
*/
protected function mcryptDecrypt($value)
{
return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->key, $value, MCRYPT_MODE_CBC, $this->iv);
}
/**
* Add PKCS7 padding to a given value.
*
* @param string $value
*
* @return string
*/
protected function addPadding($value)
{
$pad = 32 - (strlen($value) % 32);
return $value.str_repeat(chr($pad), $pad);
}
/**
* Remove the padding from the given value.
*
* @param string $value
*
* @return string
*/
protected function stripPadding($value)
{
$pad = ord($value[($paddedLength = strlen($value)) - 1]);
return $this->paddingIsValid($pad, $value) ? substr($value, 0, $paddedLength - $pad) : $value;
}
/**
* Determine if the given padding for a value is valid.
*
* @param string $pad
* @param string $value
*
* @return bool
*/
protected function paddingIsValid($pad, $value)
{
return substr($value, strlen($value) - $pad) == str_repeat(substr($value, -1), $pad);
}
}
@krisanalfa
Copy link
Author

Example usage:

$publicKey = 'M8vB6rsFMpvGWwlhTFXmsUtFlUwcnqLh';
$iv = 'fdcZHEt0EZqUUThWZICSgos631STQAhI';
$plain = 'Meyta';

$client = new Encrypt($publicKey, $iv);
$encrypted = $client->encrypt($plain);

$anotherClient = new Encrypt($publicKey, $iv);
$decrypted = $anotherClient->decrypt($encrypted);

var_dump($decrypted === $plain, $decrypted, $encrypted);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment