Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP AES Encryption/Decryption Class with PKCS5 padding
<?php
/**
* Created by PhpStorm.
* User: roshan
* Date: 4/5/15
* Time: 2:46 PM
*/
class Aes
{
private static $key = 'YourKeyxxxxxxxxx';
private static $iv = 'YourIvxxxxxxxxxx';
public static function encrypt($data)
{
return base64_encode(mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
self::$key,
self::pkcs5_pad($data),
MCRYPT_MODE_CBC,
self::$iv
));
}
public static function decrypt($data)
{
$str = mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
self::$key,
base64_decode($data),
MCRYPT_MODE_CBC,
self::$iv
);
return self::pkcs5_unpad($str);
}
private static function pkcs5_pad($text)
{
$blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
private static function pkcs5_unpad($text)
{
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$pad = ord($text[($len = strlen($text)) - 1]);
$len = strlen($text);
$pad = ord($text[$len-1]);
return substr($text, 0, strlen($text) - $pad);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.