This example describes the AES encryption algorithm for PHP. Share to everyone for your reference, specific as follows:
<?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 =) {$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== ');?>
PS: About encryption and decryption interested friends can also refer to the site online tools:
Password Security online detection:
Http://tools.jb51.net/password/my_password_safe
High Strength Password Generator:
Http://tools.jb51.net/password/CreateStrongPassword
MD5 Online Encryption Tool:
Http://tools.jb51.net/password/CreateMD5Password
Thunderbolt, Express, Cyclone URL encryption/decryption tool:
Http://tools.jb51.net/password/urlrethunder
Online hashing/hashing algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt
For more information on PHP related content readers can view the site topics: "PHP Encryption Method Summary", "PHP code and transcoding Operation Skills Summary", "PHP object-oriented Program Design Introductory Course", "PHP Mathematical Arithmetic Skills summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, PHP data structure and algorithm tutorial, PHP programming algorithm Summary, PHP Regular Expression usage summary, and PHP Common database operation Skills Summary
I hope this article will help you with your PHP programming.