Class Cryptaes
{
protected $cipher = mcrypt_rijndael_128;
protected $mode = MCRYPT_MODE_ECB;
protected $pad _method = NULL;
protected $secret _key = ";
protected $iv = ";
Public Function Set_cipher ($cipher) {
$this->cipher = $cipher;
}
Public Function Set_mode ($mode) {
$this->mode = $mode;
}
Public Function Set_iv ($IV) {
$this->iv = $iv;
}
Public Function Set_key ($key) {
$this->secret_key = $key;
}
Public Function require_pkcs5 () {
$this->pad_method = ' PKCS5 ';
}
protected function Pad_or_unpad ($STR, $ext) {
if (Is_null ($this->pad_method)) {
return $str;
}else{
$func _name = __class__. ‘::‘ . $this->pad_method. ‘_‘ . $ext. ' Pad ';
if (is_callable ($func _name)) {
$size = Mcrypt_get_block_size ($this->cipher, $this->mode);
Return Call_user_func ($func _name, $str, $size);
}
}
return $str;
}
protected function Pad ($STR) {
return $this->pad_or_unpad ($str, ');
}
protected function Unpad ($STR) {
return $this->pad_or_unpad ($str, ' un ');
}
Public function Encrypt ($STR) {
$str = $this->pad ($STR);
$TD = Mcrypt_module_open ($this->cipher, ', $this->mode, ');
if (Empty ($this->iv)) {
$iv = @mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), Mcrypt_rand);
}else{
$iv = $this->iv;
}
Mcrypt_generic_init ($TD, $this->secret_key, $IV);
$cyper _text = Mcrypt_generic ($TD, $STR);
$rt =base64_encode ($cyper _text);
$rt = Bin2Hex ($cyper _text);
Mcrypt_generic_deinit ($TD);
Mcrypt_module_close ($TD);
return $rt;
}
Public function Decrypt ($STR) {
$TD = Mcrypt_module_open ($this->cipher, ', $this->mode, ');
if (Empty ($this->iv)) {
$iv = @mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), Mcrypt_rand);
}else{
$iv = $this->iv;
}
Mcrypt_generic_init ($TD, $this->secret_key, $IV);
$decrypted _text = Mdecrypt_generic ($TD, Self::hex2bin ($STR));
$decrypted _text = Mdecrypt_generic ($TD, Base64_decode ($STR));
$rt = $decrypted _text;
Mcrypt_generic_deinit ($TD);
Mcrypt_module_close ($TD);
return $this->unpad ($RT);
}
public static function Hex2bin ($hexdata) {
$bindata = ";
$length = strlen ($hexdata);
for ($i =0; $i < $length; $i + = 2) {
$bindata. = Chr (Hexdec (substr ($hexdata, $i, 2)));
}
return $bindata;
}
public static function Pkcs5_pad ($text, $blocksize) {
$pad = $blocksize-(strlen ($text)% $blocksize);
Return $text. Str_repeat (Chr ($pad), $pad);
}
public static function Pkcs5_unpad ($text) {
$pad = Ord ($text {strlen ($text)-1});
if ($pad > strlen ($text)) return false;
if (strspn ($text, Chr ($pad), strlen ($text)-$pad)! = $pad) return false;
Return substr ($text, 0,-1 * $pad);
}
}
$keyStr = ' Uitn25lmuqc436im ';
$plainText = ' Love you 2017abc is ';
$aes = new Cryptaes ();
$aes->set_key ($KEYSTR);
$aes->REQUIRE_PKCS5 ();
$encText = $aes->encrypt ($plainText);
$decString = $aes->decrypt ($encText);
Echo $encText, "<br>", $decString;
PHP java AES