PHP and Java General AES encryption and decryption algorithm

Source: Internet
Author: User

PHP and Java Universal AES encryption and decryption algorithm AES refers to the Advanced Encryption Standard (encryption), is the most popular one of the current cryptographic algorithm, in Web application development, especially the external interface is often used, Here is a set of PHP and Java common AES encryption and decryption algorithm.

The PHP code is as follows:

<?PHPclasscryptaes{protected$cipher =mcrypt_rijndael_128; protected$mode =MCRYPT_MODE_ECB; protected$pad _method =NULL; protected$secret _key ="'; protected$iv ="';  Publicfunction Set_cipher ($cipher) {$ This->cipher =$cipher; }      Publicfunction Set_mode ($mode) {$ This->mode =$mode; }      Publicfunction Set_iv ($IV) {$ This->iv =$iv; }      Publicfunction Set_key ($key) {$ This->secret_key =$key; }      Publicfunction Require_pkcs5 () {$ This->pad_method ='PKCS5'; }     protectedfunction 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); returnCall_user_func ($func _name, $str, $size); }        }        return$str; }     protectedfunction Pad ($str) {return$ This->pad_or_unpad ($STR,"'); }     protectedfunction Unpad ($str) {return$ This->pad_or_unpad ($STR,'un'); }      Publicfunction 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; }      Publicfunction 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 Staticfunction Hex2bin ($hexdata) {$bindata="'; $length=strlen ($hexdata);  for($i =0; $i amp;< $length; $i + =2) {$bindata.= Chr (Hexdec (substr ($hexdata, $i,2))); }        return$bindata; }      Public Staticfunction Pkcs5_pad ($text, $blocksize) {$pad= $blocksize-(strlen ($text)%$blocksize); return$text. Str_repeat (Chr ($pad), $pad); }      Public Staticfunction 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; returnSUBSTR ($text,0, -1*$pad); }} $keyStr='Uitn25lmuqc436im'; $plainText='This is a string would be aes_encrypt'; $aes=NewCryptaes (); $aes-Set_key ($KEYSTR); $aes-require_pkcs5 (); $encText= $aesEncrypt ($plainText); $decString= $aesdecrypt ($encText); Echo $encText,"N", $decString;?>

Operation Result:
Fhtd0nnizv4juehjuc1htffxj/4s/rl6tdcjpinvj8mvlhwod0hwweuxhynxozf9
This is a string would be aes_encrypt

Java version of the encryption and decryption algorithm:
Import java.security.Key; import javax.crypto.Cipher; import Javax.crypto.spec.SecretKeySpec;  Import org.apache.commons.codec.binary.Base64;  Public classCryptaes {Private StaticFinal String Aestype ="aes/ecb/pkcs5padding";  Public Staticstring Aes_encrypt (String keystr, string plaintext) {byte[] Encrypt =NULL; Try{key key=GenerateKey (KEYSTR); Cipher Cipher=cipher.getinstance (Aestype);             Cipher.init (Cipher.encrypt_mode, key); Encrypt=cipher.dofinal (Plaintext.getbytes ()); }Catch(Exception e) {e.printstacktrace (); }        return NewString (Base64.encodebase64 (encrypt)); }       Public Staticstring Aes_decrypt (String keystr, String encryptData) {byte[] Decrypt =NULL; Try{key key=GenerateKey (KEYSTR); Cipher Cipher=cipher.getinstance (Aestype);             Cipher.init (Cipher.decrypt_mode, key); Decrypt=cipher.dofinal (Base64.decodebase64 (encryptData)); }Catch(Exception e) {e.printstacktrace (); }         return NewString (Decrypt). Trim (); }      Private Statickey GenerateKey (String key) throws exception{Try{secretkeyspec KeySpec=NewSecretkeyspec (Key.getbytes (),"AES"); returnKeySpec; }Catch(Exception e) {e.printstacktrace (); Throwe; }      }       Public Static voidMain (string[] args) {String keystr="Uitn25lmuqc436im"; String plaintext="This is a string would be aes_encrypt"; String Enctext=Aes_encrypt (keystr, plaintext); String decstring=Aes_decrypt (Keystr, Enctext); System. out. println (Enctext); System. out. println (decstring); } }

Operation Result:
Fhtd0nnizv4juehjuc1htffxj/4s/rl6tdcjpinvj8mvlhwod0hwweuxhynxozf9
This is a string would be aes_encrypt

PHP and Java General AES encryption and decryption algorithm

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.