php js java aes/esc 对称加密

php js java AES-256-CBC 对称加密 php加密 js java解密

php

js




	
	
	test1
	
	



	

java

package test2;
import java.util.Base64;

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

import test1.test4;
public class test3 {
	private static final String ALGORITHM = "AES/CBC/PKCS5Padding";    
	private static final String KEY = "00000000000000000000000000000000";    
	private static final String IV = "test1test1qweasd";
	public static void main(String[] args) throws Exception {
		//test1();
		test2();
	}
	public static void test2() throws Exception {
		String KEY="qweasdzxc123456QWEASDZXC|}[)8675";
		String IV="test1test1qweasd";
		String s2 = AES_cbc_decrypt("IcrUE7dxuM5B0/IV5qXWwQ==".getBytes(),KEY.getBytes(), IV.getBytes());
		System.out.println(s2);
	}
	public static void test1() throws Exception {        
		 // 加密       
		 String s = AES_cbc_encrypt("aaa".getBytes(), KEY.getBytes(), IV.getBytes()); 
		 System.out.println(s);        
		 //解密       
		 String s2 = AES_cbc_decrypt("db/3myf1YyIZFNLzk9OAJw==".getBytes(), KEY.getBytes(), IV.getBytes());        
		 System.out.println(s2);    
	}
	//加密    
	public static String AES_cbc_encrypt(byte[] srcData, byte[] key, byte[] iv) throws Exception {        
		SecretKeySpec keySpec = new SecretKeySpec(key, "AES");  //AES-256-CBC      
		Cipher cipher = Cipher.getInstance(ALGORITHM);        
		cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv));
		return  Base64.getEncoder().encodeToString(srcData); 
	}
	//解密
	public static String AES_cbc_decrypt(byte[] encData, byte[] key, byte[] iv) throws Exception {        
		 SecretKeySpec keySpec = new SecretKeySpec(key, "AES");        
		 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");        
		 cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));  
		 return new String(cipher.doFinal(Base64.getDecoder().decode(encData)));    
	}
}

你可能感兴趣的:(php,java,php,javascript)