PHP java AES

Source: Internet
Author: User
Tags chr decrypt

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-&GT;REQUIRE_PKCS5 ();
$encText = $aes->encrypt ($plainText);
$decString = $aes->decrypt ($encText);

Echo $encText, "<br>", $decString;

PHP java AES

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.