Skip to content

Instantly share code, notes, and snippets.

@taka-wang
Last active January 26, 2018 19:17
Show Gist options
  • Save taka-wang/9d899c76f8d126a06d574ec2e2f8b272 to your computer and use it in GitHub Desktop.
Save taka-wang/9d899c76f8d126a06d574ec2e2f8b272 to your computer and use it in GitHub Desktop.
PHP AES
<?php
class MCrypt {
private $hex_iv = '00000000000000000000000000000000'; # converted JAVA byte code in to HEX and placed it here
private $key = 'U1MjU1M0FDOUZ.Qz'; #Same as in JAVA
function __construct() {
$this->key = hash('sha256', $this->key, true);
//echo $this->key.'<br/>';
}
function encrypt($str) {
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$pad = $block - (strlen($str) % $block);
$str .= str_repeat(chr($pad), $pad);
$encrypted = mcrypt_generic($td, $str);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return base64_encode($encrypted);
}
function decrypt($code) {
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
$str = mdecrypt_generic($td, base64_decode($code));
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $this->strippadding($str);
}
/*
For PKCS7 padding
*/
private function addpadding($string, $blocksize = 16) {
$len = strlen($string);
$pad = $blocksize - ($len % $blocksize);
$string .= str_repeat(chr($pad), $pad);
return $string;
}
private function strippadding($string) {
$slast = ord(substr($string, -1));
$slastc = chr($slast);
$pcheck = substr($string, -$slast);
if (preg_match("/$slastc{" . $slast . "}/", $string)) {
$string = substr($string, 0, strlen($string) - $slast);
return $string;
} else {
return false;
}
}
function hexToStr($hex)
{
$string='';
for ($i=0; $i < strlen($hex)-1; $i+=2)
{
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}
}
$encryption = new MCrypt();
echo $encryption->encrypt('123456') . "<br/>";
echo $encryption->decrypt('tpyxISJ83dqEs3uw8bN/+w==');
?>
//https://gist.github.com/tlarevo/b65c94b02993fd50fc68
<?php
// Credit should be given to;
// https://gist.github.com/bradbernard/7789c2dd8d17c2d8304b#file-php-encyrption
// https://gist.github.com/krzyzanowskim/043be69ab3ba9fd5ba58#file-encryptaeswithphp
$data = '{"verification_code":"123456"}';
$key = '1234567890123456';
function aesEncrypt($data, $key) {
$data = addPadding($data);
$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
$cipherText = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv);
$result = base64_encode($iv.$cipherText);
return $result;
}
function aesDecrypt($base64encodedCipherText, $key) {
$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
$cipherText = base64_decode($base64encodedCipherText);
if (strlen($cipherText) < $ivSize) {
throw new Exception('Missing initialization vector');
}
$iv = substr($cipherText, 0, $ivSize);
$cipherText = substr($cipherText, $ivSize);
$result = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $cipherText, MCRYPT_MODE_CBC, $iv);
$result = stripPadding(rtrim($result, chr(0x0b)));
return $result;
}
function addPadding($value){
$pad = 16 - (strlen($value) % 16);
return $value.str_repeat(chr($pad), $pad);
}
function stripPadding($value){
$pad = ord($value[($len = strlen($value)) - 1]);
return paddingIsValid($pad, $value) ? substr($value, 0, $len - $pad) : $value;
}
function paddingIsValid($pad, $value){
$beforePad = strlen($value) - $pad;
return substr($value, $beforePad) == str_repeat(substr($value, -1), $pad);
}
$base64encodedCipherText = aesEncrypt($data, $key);
print($base64encodedCipherText);
print("<br />");
$decryptedText = aesDecrypt($base64encodedCipherText, $key);
print($decryptedText);
print("<br />");
$base64encodedCipherText2 = "aAQ7PWP50FtUv37VxItEIVl6kPXdFm/xiA1ROxAuKthmCj4+L4CWix4pyzbq+I/q"; // Encoded and Ciphered string generated by Swift
$decryptedText2 = stripPadding(aesDecrypt($base64encodedCipherText2, $key));
print($decryptedText2);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment