packagecom.xx.ws.util;importjavax.crypto.Cipher;importjavax.crypto.spec.IvParameterSpec;importjavax.crypto.spec.SecretKeySpec;importjavax.xml.bind.DatatypeConverter;public classAESOperator {private String sKey = "abcdef0123456789";private String ivParameter = "9iuj87y2hbi5wxlk";private final String CIPHER = "AES/CBC/PKCS5Padding";private final String AES = "AES";private final String CHARSET = "UTF-8";publicAESOperator() {
}publicAESOperator(String key) {this.sKey =key;
}public String encrypt(String sSrc) throwsException {try{
Cipher cipher= Cipher.getInstance("AES/CBC/PKCS5Padding");byte[] raw = this.sKey.getBytes();
SecretKeySpec skeySpec= new SecretKeySpec(raw, "AES");
IvParameterSpec iv= new IvParameterSpec(this.ivParameter.getBytes());
cipher.init(1, skeySpec, iv);byte[] encrypted = cipher.doFinal(sSrc.getBytes("UTF-8"));returnDatatypeConverter.printBase64Binary(encrypted);
}catch(Exception var7) {throwvar7;
}
}public String encrypt(String sSrc, String key) throwsException {try{
Cipher cipher= Cipher.getInstance("AES/CBC/PKCS5Padding");byte[] raw =key.getBytes();
SecretKeySpec skeySpec= new SecretKeySpec(raw, "AES");
IvParameterSpec iv= new IvParameterSpec(this.ivParameter.getBytes());
cipher.init(1, skeySpec, iv);byte[] encrypted = cipher.doFinal(sSrc.getBytes("UTF-8"));returnDatatypeConverter.printBase64Binary(encrypted);
}catch(Exception var8) {throwvar8;
}
}public String decrypt(String sSrc) throwsException {try{byte[] raw = this.sKey.getBytes("ASCII");
SecretKeySpec skeySpec= new SecretKeySpec(raw, "AES");
Cipher cipher= Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv= new IvParameterSpec(this.ivParameter.getBytes());
cipher.init(2, skeySpec, iv);byte[] encrypted1 =DatatypeConverter.parseBase64Binary(sSrc);byte[] original =cipher.doFinal(encrypted1);return new String(original, "UTF-8");
}catch(Exception var8) {throwvar8;
}
}public String decrypt(String sSrc, String key) throwsException {try{byte[] raw = key.getBytes("ASCII");
SecretKeySpec skeySpec= new SecretKeySpec(raw, "AES");
Cipher cipher= Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv= new IvParameterSpec(this.ivParameter.getBytes());
cipher.init(2, skeySpec, iv);byte[] encrypted1 =DatatypeConverter.parseBase64Binary(sSrc);byte[] original =cipher.doFinal(encrypted1);return new String(original, "UTF-8");
}catch(Exception var9) {throwvar9;
}
}public static void main(String[] args) throwsException {
AESOperator aesOper= new AESOperator("abcdef0123456789");
String cSrc= "test123";
System.out.println(cSrc+ " 长度为" +cSrc.length());long lStart =System.currentTimeMillis();
String enString=aesOper.encrypt(cSrc);
System.out.println("加密后的字串是:" + enString + "长度为" +enString.length());long lUseTime = System.currentTimeMillis() -lStart;
System.out.println("加密耗时:" + lUseTime + "毫秒");
lStart=System.currentTimeMillis();
String DeString=aesOper.decrypt(enString);
System.out.println("解密后的字串是:" + DeString + " 长度为" +DeString.length());
lUseTime= System.currentTimeMillis() -lStart;
System.out.println("解密耗时:" + lUseTime + "毫秒");
}
}