Search on the Internet more than n code, are you copy me, I copied you, really let people without words to heaven. After a lot of data search, PHP and Java CBC encryption, decryption results are finally consistent, the code is as follows:
Java Cryptographic decryption class:
Packagemain;ImportJavax.crypto.Cipher;ImportJavax.crypto.spec.IvParameterSpec;ImportJavax.crypto.spec.SecretKeySpec;Importorg.apache.commons.codec.binary.Base64; Public classAesutil {//Encrypt Public StaticString Encrypt (String sSrc, String SKey)throwsException {if(SKey = =NULL) {System.out.print ("Key is null NULL"); return NULL; } //determine if key is 16-bit if(Skey.length ()! = 16) {System.out.print ("Key length is not 16-bit"); return NULL; } byte[] Raw =skey.getbytes (); Secretkeyspec Skeyspec=NewSecretkeyspec (Raw, "AES"); Cipher Cipher= Cipher.getinstance ("aes/cbc/pkcs5padding");//"algorithm/Mode/complement Method"//Cipher Cipher = cipher.getinstance ("aes/ecb/pkcs5padding");//"algorithm/Mode/complement Method"Ivparameterspec IV =NewIvparameterspec ("1234567890123456". GetBytes ());//using CBC mode, a vector IV is required to increase the strength of the encryption algorithmCipher.init (Cipher.encrypt_mode, Skeyspec, iv); //Cipher.init (Cipher.encrypt_mode, skeyspec); byte[] encrypted = Cipher.dofinal (Ssrc.getbytes ("UTF-8")); return NewString (Base64.encodebase64 (encrypted)); } //decryption Public StaticString Decrypt (String sSrc, String SKey)throwsException {Try { //determine if key is correct if(SKey = =NULL) {System.out.print ("Key is null NULL"); return NULL; } //determine if key is 16-bit if(Skey.length ()! = 16) {System.out.print ("Key length is not 16-bit"); return NULL; } byte[] raw = Skey.getbytes ("UTF-8"); Secretkeyspec Skeyspec=NewSecretkeyspec (Raw, "AES"); Cipher Cipher= Cipher.getinstance ("aes/cbc/pkcs5padding"); //Cipher Cipher = cipher.getinstance ("aes/ecb/pkcs5padding");Ivparameterspec IV =NewIvparameterspec ("1234567890123456". GetBytes ()); Cipher.init (Cipher.decrypt_mode, Skeyspec, iv); //Cipher.init (Cipher.decrypt_mode, skeyspec);base64.decodebase64 (Ssrc.getbytes ()); byte[] encrypted1 =base64.decodebase64 (Ssrc.getbytes ()); Try { byte[] Original =cipher.dofinal (encrypted1); String originalstring=NewString (original); returnoriginalstring; } Catch(Exception e) {System.out.println (e.tostring ()); return NULL; } } Catch(Exception ex) {System.out.println (ex.tostring ()); return NULL; } }}
Java calls:
Public Static voidMain (string[] args) {//TODO auto-generated Method StubString Ckey= "1234567890123456"; //strings that need to be encryptedString = "Test String"; System.out.println ; String enstring; String destring; Try{enstring=Aesutil.encrypt ; System.out.println ("The encrypted string is:" +enstring); Destring=Aesutil.decrypt (enstring, Ckey); System.out.println ("The decrypted string is:" +destring); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }
Note that in the Aesutil class, there is an IV variable that corresponds to the IV in PHP.
PHP Cryptographic Decryption class:
classMyaes { Public functionEncryptString ($input,$key,$iv) { $size= Mcrypt_get_block_size (mcrypt_rijndael_128,MCRYPT_MODE_CBC); $input= Myaes::p kcs5_pad ($input,$size); $TD= Mcrypt_module_open (mcrypt_rijndael_128, ", MCRYPT_MODE_CBC,"); //$iv = Mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), mcrypt_rand);Mcrypt_generic_init ($TD,$key,$iv); $data= Mcrypt_generic ($TD,$input); Mcrypt_generic_deinit ($TD); Mcrypt_module_close ($TD); $data=Base64_encode($data); return $data; } Private Static functionPkcs5_pad ($text,$blocksize) { $pad=$blocksize- (strlen($text) %$blocksize); return $text.str_repeat(CHR($pad),$pad); } Public functionDecryptstring ($sStr,$sKey,$iv) { $TD= Mcrypt_module_open (mcrypt_rijndael_128, ", MCRYPT_MODE_CBC,"); //$iv = Mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), mcrypt_rand); $decrypted= @mcrypt_decrypt (mcrypt_rijndael_128,$sKey,Base64_decode($sStr), MCRYPT_MODE_CBC,$iv); $dec _s=strlen($decrypted); $padding=Ord($decrypted[$dec _s-1]); $decrypted=substr($decrypted, 0,-$padding); return $decrypted; } }
PHP Calls:
$msg= ' Test string ';$key= ' 1234567890123456 ';$iv=$key;$x=NewMyaes ();$en=$x->encryptstring ($msg,$key,$iv);Echo $en, ' <br/> ';Echo $x->decryptstring ($en,$key,$iv);
Reference: http://blog.163.com/fan_xy_qingyuan/blog/static/188987748201391124728740/
CBC PHP Java compatible version