1. Java端
package com.inphase.demo.utils;
importjava.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
importjavax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
importorg.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AESUtils {
privatestatic final Logger logger = (Logger) LoggerFactory.getLogger(AESUtils.class);
privatestatic final String CHARSET = "UTF-8";
/**
* @param args
*/
publicstatic void main(String[] args) {
Stringsource = "888888";
Stringdata ="y/VcohRI9k+5NYPWL9w4ZFlPbuwGNvEv6E3ue7uniXQGIkSMYLaQ4wMP4VR6GNa0";
StringaesKey="o7H8uIM2O5qv65l2";
try{
Stringdes = aesDecrypt(data, aesKey);
Stringenc = aesEncrypt(source, aesKey);
System.out.println("解密後:\t"+des);
System.out.println("\n加密後:"+enc);
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加密
*
* @param content
* 需要加密的內容
* @param key
* 加密密碼
* @param md5Key
* 是否對key進行md5加密
* @param iv
* 加密向量
* @return 加密後的字節數據
*/
publicstatic byte[] encrypt(byte[] content, byte[] key, boolean md5Key,
byte[]iv) {
try{
if(md5Key) {
MessageDigestmd = MessageDigest.getInstance("MD5");
key= md.digest(key);
}
SecretKeySpecskeySpec = new SecretKeySpec(key, "AES");
Ciphercipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // "算法/模式/補碼方式"
IvParameterSpecivps = new IvParameterSpec(iv);// 使用CBC模式,需要一個向量iv,可增加加密算法的強度
cipher.init(Cipher.ENCRYPT_MODE,skeySpec, ivps);
returncipher.doFinal(content);
}catch (NoSuchAlgorithmException | NoSuchPaddingException
|InvalidKeyException | InvalidAlgorithmParameterException
|IllegalBlockSizeException | BadPaddingException ex) {
ex.printStackTrace();
logger.error(ex.getLocalizedMessage());
}
returnnull;
}
publicstatic byte[] decrypt(byte[] content, byte[] key, boolean md5Key,
byte[]iv) {
try{
if(md5Key) {
MessageDigestmd = MessageDigest.getInstance("MD5");
key= md.digest(key);
}
SecretKeySpecskeySpec = new SecretKeySpec(key, "AES");
Ciphercipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // "算法/模式/補碼方式"
IvParameterSpecivps = new IvParameterSpec(iv);// 使用CBC模式,需要一個向量iv,可增加加密算法的強度
cipher.init(Cipher.DECRYPT_MODE,skeySpec, ivps);
returncipher.doFinal(content);
}catch (NoSuchAlgorithmException | NoSuchPaddingException
|InvalidKeyException | InvalidAlgorithmParameterException
|IllegalBlockSizeException | BadPaddingException ex) {
ex.printStackTrace();
logger.error(ex.getLocalizedMessage());
}
returnnull;
}
publicstatic String aesEncrypt (String data,String aesKey) throws Exception {
StringsendStr = "";
byte[]bytes = data.getBytes(CHARSET);
//以下是對數據進行加密
bytes= AESUtils.encrypt(bytes, aesKey.getBytes(CHARSET), false,
aesKey.getBytes("UTF-8"));
sendStr= Base64.encodeBase64String(bytes);
//以上是對數據進行加密
returnsendStr;
}
publicstatic String aesDecrypt(String data,String aesKey) throws Exception{
if(!data.isEmpty()){
if(data.indexOf("%3D")>-1){
data= data.replaceAll("%3D","==");
}
byte[] bytes = data.getBytes(CHARSET);
//以下是對數據進行解密
bytes = Base64.decodeBase64(bytes);
bytes = AESUtils.decrypt(bytes,aesKey.getBytes(CHARSET), false, aesKey.getBytes(CHARSET));
//以上是對數據進行解密
return new String(bytes, CHARSET);
}else{
return"";
}
}
}
/*
* 01算法/模式/填充 16字節加密後數據長度 不滿16字節加密後長度 02 AES/CBC/NoPadding 16 不支持 03
*AES/CBC/PKCS5Padding 32 16 04 AES/CBC/ISO10126Padding 32 16 05
*AES/CFB/NoPadding 16 原始數據長度 06 AES/CFB/PKCS5Padding 3216 07
* AES/CFB/ISO10126Padding32 16 08 AES/ECB/NoPadding 16 不支持 09
*AES/ECB/PKCS5Padding 32 16 10 AES/ECB/ISO10126Padding 32 16 11
*AES/OFB/NoPadding 16 原始數據長度 12 AES/OFB/PKCS5Padding 3216 13
*AES/OFB/ISO10126Padding 32 16 14 AES/PCBC/NoPadding 16 不支持 15
*AES/PCBC/PKCS5Padding 32 16 16 AES/PCBC/ISO10126Padding 32 16
*
*
*
*CryptoJS supports the following padding schemes:
*
*Pkcs7 (the default) Iso97971 AnsiX923 Iso10126 ZeroPadding NoPadding
*/
2. Jsp頁面
<scripttype="text/javascript"src="<%=basePath%>js/core.js"></script>
<scripttype="text/javascript"src="<%=basePath%>js/aes.js"></script>
<script type="text/javascript" src="<%=basePath%>js/enc-base64.js"></script>