AES前端加密后端解密

欢迎阅读AES前端加密后端解密

目录

  1. 前端js加密






                
                



  1. 后端java解密
package cn.com.bankit.bwp.common.util;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

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

public class EncryptUtils {
	   private static final String KEY = "abcdefgabcdefg12";  
	    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";  
	    public static String base64Encode(byte[] bytes){  
	        return Base64.encodeBase64String(bytes);  
	    }  
	    public static byte[] base64Decode(String base64Code) throws Exception{  
	        //return new BASE64Decoder().decodeBuffer(base64Code);  
	        return Base64.decodeBase64(base64Code.getBytes("UTF-8")); 
	    }  
	    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {  
	        KeyGenerator kgen = KeyGenerator.getInstance("AES");  
	        kgen.init(128);  
	        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);  
	        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));  

	        return cipher.doFinal(content.getBytes("utf-8"));  
	    }  
	    public static String aesEncrypt(String content, String encryptKey) throws Exception {  
	        return base64Encode(aesEncryptToBytes(content, encryptKey));  
	    }  
	    public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) {  
	    	  try {
			        KeyGenerator kgen = KeyGenerator.getInstance("AES");  
			        kgen.init(128);  
			        
			        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);  
			        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));  
			        byte[] decryptBytes = cipher.doFinal(encryptBytes);  
			        return new String(decryptBytes);  
	    	  } catch (NoSuchAlgorithmException e) {
		            e.printStackTrace();
		        } catch (NoSuchPaddingException e) {
		            e.printStackTrace();
		        } catch (InvalidKeyException e) {
		            e.printStackTrace();
		        } catch (IllegalBlockSizeException e) {
		            e.printStackTrace();
		        } catch (BadPaddingException e) {
		            e.printStackTrace();
		        }
	    	  return null;
	    }  
	    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {  
	        return aesDecryptByBytes(base64Decode(encryptStr), decryptKey);  
	    }  


	    /**
	     * 测试
	     * 
	     */
	    public static void main(String[] args) throws Exception {

	        String content = "Test String么么哒";  //0gqIDaFNAAmwvv3tKsFOFf9P9m/6MWlmtB8SspgxqpWKYnELb/lXkyXm7P4sMf3e
	        System.out.println("加密前:" + content);  

	        System.out.println("加密密钥和解密密钥:" + KEY);  

	        String encrypt = aesEncrypt(content, KEY);  
	        System.out.println(encrypt.length()+":加密后:" + encrypt);  

	        String decrypt = aesDecrypt(encrypt, KEY);  
	        System.out.println("解密后:" + decrypt);  
	    }
}

你可能感兴趣的:(java,安全)