研究了一下午,也没找到IV。。。
[Java] 纯文本查看 复制代码
public class AesUtil {
private static String aesKey = "Y5djB0D93sIbeRzZ";
private static String hexStr = "0123456789abcdef";
public static String getKey() {
return aesKey;
}
public static String encryptToHex(String str, String str2) {
try {
return bytesToHexString(aesEncryptToBytes(str, str2));
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
}
public static String decryptHex(String str, String str2) {
try {
return aesDecryptByBytes(hexStringToBytes(str), str2);
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
}
public static String bytesToHexString(byte[] bArr) {
String str = "";
for (byte b : bArr) {
String valueOf = String.valueOf(hexStr.charAt((b & 240) >> 4));
String str2 = valueOf + String.valueOf(hexStr.charAt(bArr[r1] & 15));
str = str + str2;
}
return str;
}
public static byte[] hexStringToBytes(String str) {
int length = str.length() / 2;
byte[] bArr = new byte[length];
for (int i = 0; i < length; i++) {
int i2 = i * 2;
bArr[i] = (byte) (((byte) (hexStr.indexOf(str.charAt(i2)) << 4)) | ((byte) hexStr.indexOf(str.charAt(i2 + 1))));
}
return bArr;
}
private static byte[] aesEncryptToBytes(String str, String str2) throws Exception {
Cipher instance = Cipher.getInstance("AES/CBC/PKCS7Padding");
byte[] bytes = str2.getBytes();
instance.init(1, new SecretKeySpec(bytes, "AES"), new IvParameterSpec(bytes));
return instance.doFinal(str.getBytes());
}
private static String aesDecryptByBytes(byte[] bArr, String str) throws Exception {
Cipher instance = Cipher.getInstance("AES/CBC/PKCS7Padding");
byte[] bytes = str.getBytes();
instance.init(2, new SecretKeySpec(bytes, "AES"), new IvParameterSpec(bytes));
return new String(instance.doFinal(bArr));
}
public static String getSubUtilSimple(String str, String str2) {
Matcher matcher = Pattern.compile(str2).matcher(str);
return matcher.find() ? matcher.group(1) : "";
}
}
[Java] 纯文本查看 复制代码 public class DownloadInfo {
private static String hexStr = "0123456789abcdef";
public static String decryptHex(String str) {
try {
return aesDecryptByBytes(hexStringToBytes(str), "DtXZcHh9XJ5SfPAe");
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
}
private static String aesDecryptByBytes(byte[] bArr, String str) throws Exception {
Cipher instance = Cipher.getInstance("AES/CBC/PKCS7Padding");
byte[] bytes = str.getBytes();
instance.init(2, new SecretKeySpec(bytes, "AES"), new IvParameterSpec(bytes));
return new String(instance.doFinal(bArr));
}
public static byte[] hexStringToBytes(String str) {
int length = str.length() / 2;
byte[] bArr = new byte[length];
for (int i = 0; i < length; i++) {
int i2 = i * 2;
bArr[i] = (byte) (((byte) (hexStr.indexOf(str.charAt(i2)) << 4)) | ((byte) hexStr.indexOf(str.charAt(i2 + 1))));
}
return bArr;
}
|