class aes_mcrypt { private $hex _iv = ' 00000000000000000000000000000000 '; # converted java byte code in to Hex and placed it here function __construct ($key) { $this->key = hash (' sha256 ', $key, true); } 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,&NBSP;MCRYPT_MODE_CBC); $ Block = mcrypt_enc_get_block_size ($TD); $str = $this->addpadding ($str, $block); $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, ", &NBSP;MCRYPT_MODE_CBC, mcrypt_generic_init ($td, $this->key, $ This->hextostr ($this->hex_iv)); $str = mdecrypt _generic ($TD, base64_decode ($code)); mcrypt_generic_deinit ($TD); &nBsp; 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; } } public function Hextostr ($hex) { $string = '; for ($i =0; $i &NBSP;<&NBSp;strlen ($hex) -1; $i +=2) { $string .= chr (Hexdec ($hex [$i]. $hex [$i +1]); } return $string; }}
PHP AES CBC