php和java之间aes加密的互通


以上是php封装好的类,引入即可使用。

例:

";
$decry = Security::decrypt($encry, $key);//解密
echo $decry;
?>

-----------------------------------------------以下是java封装的类---------------------------------------------


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class Security {
	public static String encrypt(String input, String key){
	  byte[] crypted = null;
	  try{
	    SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
	      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
	      cipher.init(Cipher.ENCRYPT_MODE, skey);
	      crypted = cipher.doFinal(input.getBytes());
	    }catch(Exception e){
	    	System.out.println(e.toString());
	    }
	    return new String(Base64.encodeBase64(crypted));
	}

	public static String decrypt(String input, String key){
	    byte[] output = null;
	    try{
	      SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
	      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
	      cipher.init(Cipher.DECRYPT_MODE, skey);
	      output = cipher.doFinal(Base64.decodeBase64(input));
	    }catch(Exception e){
	      System.out.println(e.toString());
	    }
	    return new String(output);
	}
	
	public static void main(String[] args) {
	  String key = "1234567891234567";
	  String data = "example";
	  System.out.println(Security.decrypt(Security.encrypt(data, key), key));
	  System.out.println(Security.encrypt(data, key));	    
	}	
}


以上就是php和java之间的aes加密,亲测已测通。



你可能感兴趣的:(PHP)