Simple class to encrypt and decrypt something with AES 256 encryption. Support for padding too!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: