public static String AES(String content, String key) {
if (key == null) {
System.out.print("Key为空null");
return null;
}
if (key.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = key.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = null;
try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
try {
cipher.init(Cipher.ENCRYPT_MODE,skeySpec,iv);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
byte[] encrypted;
String msg=null;
try {
encrypted = cipher.doFinal(content.getBytes());
int totalLength = iv.getIV().length + encrypted.length;
byte[] combine = new byte[totalLength];
System.arraycopy(iv.getIV(), 0, combine, 0, iv.getIV().length);
System.arraycopy(encrypted, 0, combine, iv.getIV().length,encrypted.length);
String baseEncoder = new BASE64Encoder().encode(combine);
try {
msg=URLEncoder.encode(baseEncoder, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return msg;
}
public String SHA1(String decript) {
try {
MessageDigest digest = java.security.MessageDigest
.getInstance("SHA-1");
digest.update(decript.getBytes());
byte messageDigest[] = digest.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
public static String bytetoString(byte[] digest) {
String str = "";
String tempStr = "";
for (int i = 1; i < digest.length; i++) {
tempStr = (Integer.toHexString(digest[i] & 0xff));
if (tempStr.length() == 1) {
str = str + "0" + tempStr;
} else {
str = str + tempStr;
}
}
return str.toLowerCase();
}