public class AES {
static byte [] iv = { 0x38 , 0x37 , 0x36 , 0x35 , 0x34 , 0x33 , 0x32 , 0x31 , 0x38 ,
0x37 , 0x36 , 0x35 , 0x34 , 0x33 , 0x32 , 0x31 };
public AES() {
}
/**
*
* @param kbs
* @return
*/
private static byte[] getKeyBytes(byte[] kbs) {
if (kbs.length >= 32) {
byte[] result = new byte[32];
System.arraycopy(kbs, 0, result, 0, 32);
return result;
} else if (kbs.length >= 24) {
byte[] result = new byte[24];
System.arraycopy(kbs, 0, result, 0, 24);
return result;
} else if(kbs.length>=16){
byte[] result = new byte[16];
System.arraycopy(kbs, 0 , result, 0 , 16);
return result;
} else{
byte[] result = new byte[16];
System.arraycopy(kbs, 0 , result, 0 , kbs.length);
return result;
}
}
/**
*
* @param kbs
* @return
* @throws NoSuchAlgorithmException
*/
private static byte[] getIV(byte[] kbs) throws NoSuchAlgorithmException{
byte[] result = new byte[16];
byte[] md5 = md5(kbs);
System.arraycopy(md5, 0, result, 0, Math.min(md5.length, 16));
for(int i=15;i>-1;i--){
if(result[i]==0){result[i]=(byte) (0x38-i);}
}
return result;
}
/**
* 对字符串进行MD5加密
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
public static byte[] md5(byte[] str) throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(str);
return md5.digest();
}
/**
* 加密
* @param content 需要加密的内容
* @param keyWord 加密密钥
* @return byte[] 加密后的字节数组
*/
public static byte[] encrypt(String content, String keyWord) {
try {
BufferedBlockCipher engine = new PaddedBufferedBlockCipher(
new CBCBlockCipher( new AESFastEngine()));
byte[] contentBytes = content.getBytes("UTF-8");
byte[] kbs = keyWord.getBytes("UTF-8");
engine.init(true , new ParametersWithIV(
new KeyParameter(getKeyBytes(kbs)),getIV(kbs)));
byte [] enc = new byte [engine.getOutputSize(contentBytes.length)];
int size1 = engine.processBytes(contentBytes, 0 , contentBytes.length, enc, 0 );
//System.out.println(size1);
int size2;
size2 = engine.doFinal(enc, size1);
//System.out.println("size2 =" +size2);
byte [] encryptedContent = new byte [size1+size2];
System.arraycopy(enc, 0 , encryptedContent, 0 , encryptedContent.length);
// System.out.println("Encrypted Content:" );
return Hex.encode(encryptedContent);
} catch (DataLengthException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (InvalidCipherTextException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
/**
* @param content 需要加密的内容
* @param password 加密密钥
* @return String 加密后的字符串
*/
public static String encrypttoStr(String content, String password){
try {
return new String(encrypt(content,password),"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
/**解密
* @param content 待解密内容
* @param keyWord 解密密钥
* @return byte[]
*/
public static byte[] decrypt(byte[] content, String keyWord) {
try {
BufferedBlockCipher engine = new PaddedBufferedBlockCipher(
new CBCBlockCipher( new AESFastEngine()));
byte[] kbs = keyWord.getBytes("UTF-8");
engine.init(false , new ParametersWithIV(
new KeyParameter(getKeyBytes(kbs)),getIV(kbs)));
byte [] dec = new byte [engine.getOutputSize(content.length)];
int size1 = engine.processBytes(content, 0 , content.length, dec, 0 );
//System.out.println(size1);
int size2 = engine.doFinal(dec, size1);
//System.out.println("size2 =" +size2);
byte [] decryptedContent = new byte [size1+size2];
System.arraycopy(dec, 0 , decryptedContent, 0 , decryptedContent.length);
//System.out.println("Decrypted Content:" );
return decryptedContent;
} catch (DataLengthException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (InvalidCipherTextException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
/**
* @param content 待解密内容(字符串)
* @param keyWord 解密密钥
* @return byte[]
*/
public static byte[] decrypt(String content, String keyWord) {
try {
return decrypt(hex2byte(content),keyWord);
// return decrypt(content.getBytes("UTF-8"),keyWord);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
*
* @param strhex
* @return
*/
public static byte[] hex2byte(String strhex) {
if (strhex == null) {
return null;
}
int l = strhex.length();
if (l % 2 == 1) {
return null;
}
byte[] b = new byte[l / 2];
for (int i = 0; i != l / 2; i++) {
b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
16);
}
return b;
}
/**
*
* @param b
* @return
*/
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs.toUpperCase();
}
/**
*
* @param args
*/
public static void main(String[] args) {
String content = "/app/RptQueryAction_indexRptQuery.action";
content="/app/rptDataAction_viewRptDataOnPage.action" +
"?dimList=RANGE%3a0%3a%25E5%2585%25A8%25E8%25BE%2596%7cUPINSTCODE%3a810000000012%3a%25E6" +
"%25B7%25B1%25E5%259C%25B3%25E5%25B8%2582%25E5%2588%2586%25E8%25A1%258C%7cCURRKIND%3a1%3a"+
"%25E6%259C%25AC%25E5%25B8%2581" +
"&rptDataVO.sbjcod=005" +
"&rptDataVO.rptcod=CR0116" +
"&rptDataVO.rptdte=20111031&rptDataVO.cfgbsl=20060101" +
"&rptDataVO.rptnam=%25E5%2588%2586%25E6%2594%25AF%25E6%259C%25BA%25E6%259E%2584%25E8" +
"%25B4%25B7%25E6%25AC%25BE%25E8%25B4%25A8%25E9%2587%258F%25E6%2583%2585%25E5%2586" +
"%25B5%25E8%25A1%25A8%25EF%25BC%2588%25E5%258D%2581%25E4%25BA%258C%25E7%25BA%25A7" +
"%25E5%2588%2586%25E7%25B1"+
"%25BB%25EF%25BC%2589";
String Key = "rsp";
//加密
System.err.println("加密前:" + content);
String encryptResult = encrypttoStr(content, Key);
System.err.println("加密后:" + encryptResult);
//解密
byte[] decryptResult = decrypt(encryptResult,"rsp");
System.err.println("解密后:" + new String(decryptResult));
}
}