package com.qxueyou.scc.base.util;
|
|
import javax.crypto.Cipher;
|
import javax.crypto.KeyGenerator;
|
import javax.crypto.SecretKey;
|
import javax.crypto.spec.IvParameterSpec;
|
import javax.crypto.spec.SecretKeySpec;
|
import java.security.SecureRandom;
|
|
public class AESUtil {
|
|
public static final String charset = "UTF-8";
|
private static final String IV = "1234567812345678";
|
private static final String defaultPasswd = "0120302323012324";
|
|
public static String encrypt(String cleartext) {
|
return encrypt(defaultPasswd,cleartext);
|
}
|
|
public static String encrypt(String dataPassword, String cleartext) {
|
try {
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
SecretKeySpec key = new SecretKeySpec(dataPassword.getBytes(), "AES");
|
IvParameterSpec iv = new IvParameterSpec(IV.getBytes());
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
byte[] encryptedData = cipher.doFinal(cleartext.getBytes(charset));
|
return new String(parseByte2HexStr(encryptedData));
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
public static String decrypt(String cleartext) {
|
return decrypt(defaultPasswd,cleartext);
|
}
|
|
public static String decrypt(String dataPassword, String encrypted) {
|
try {
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
SecretKeySpec key = new SecretKeySpec(dataPassword.getBytes(), "AES");
|
IvParameterSpec iv = new IvParameterSpec(IV.getBytes());
|
byte[] byteMi = parseHexStr2Byte(encrypted);
|
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
byte[] decryptedData = cipher.doFinal(byteMi);
|
return new String(decryptedData, charset);
|
} catch (Exception e) {
|
return null;
|
}
|
}
|
|
/**
|
* 将16进制转换为二进制
|
*
|
* @param hexStr
|
* @return
|
*/
|
public static byte[] parseHexStr2Byte(String hexStr) {
|
if (hexStr.length() < 1) {
|
return null;
|
}
|
byte[] result = new byte[hexStr.length() / 2];
|
for (int i = 0; i < hexStr.length() / 2; i++) {
|
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
|
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
|
result[i] = (byte) (high * 16 + low);
|
}
|
return result;
|
}
|
|
/**
|
* 将二进制转换成16进制
|
*
|
* @param buf
|
* @return
|
*/
|
public static String parseByte2HexStr(byte buf[]) {
|
StringBuilder sb = new StringBuilder();
|
for (int i = 0; i < buf.length; i++) {
|
String hex = Integer.toHexString(buf[i] & 0xFF);
|
if (hex.length() == 1) {
|
sb.append('0');
|
}
|
sb.append(hex.toUpperCase());
|
}
|
return sb.toString();
|
}
|
|
/**
|
* AES解密
|
*/
|
public static String decode(String aesKey, String encryptText) {
|
try {
|
KeyGenerator _generator = KeyGenerator.getInstance("AES");
|
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
|
secureRandom.setSeed(aesKey.getBytes());
|
_generator.init(128, secureRandom);
|
SecretKey secretKey = _generator.generateKey();
|
byte[] enCodeFormat = secretKey.getEncoded();
|
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
|
Cipher cipher = Cipher.getInstance("AES");
|
cipher.init(Cipher.DECRYPT_MODE, key);
|
byte[] result = cipher.doFinal(AESUtil.parseHexStr2Byte(encryptText));
|
return new String(result);
|
} catch (Exception e) {
|
return "";
|
}
|
}
|
|
public static void main(String[] args) {
|
|
String publicKey = "30820122300d06092a864886f70d01010105000382010f00303032301006\n" +
|
"072a8648ce3d02002EC311215SHA512withECDSA106052b81040006031e0\n" +
|
"0042cac6fdede70a68027b03be32f81a2c3aca6a715e6937abcc6277e0fG\n" +
|
"82010a028201010097beb94bc6a412279fdcf7b9696279a2dad43e4c0589\n" +
|
"5ce1d14057fa4e60da48e38c0bed81cc668465213dec624fbde46c379468\n" +
|
"1fa1a9fe05ae4595cd82c09cf0a4cea909efd43b8993dffcb2b86d8cdccf\n" +
|
"1986392d60bcbf8aaf0c03RSA4204813SHA512withRSA1b5a1f66abe2be9\n" +
|
"dd9c23b1efbaa48dc9727d1899765d37901d37403af214e45fbdf6831845\n" +
|
"e762cd227c3fa0d0960346b23e30da429881ca3a26a20dcc3c08bba66927\n" +
|
"e1e742184dc92ed0dc61ec97313b5239d47b1099f154928dd06c149c0ae2\n" +
|
"9e6a04674454f89bbf466aa2527138e061a1d85f08bbd4dc0d9f3b3494d0\n" +
|
"c8a406830fe981ee905719082c23767f6e0c59df143549786f5e28587eb4\n" +
|
"f7ba4f8549aff0203010001\n";
|
|
|
|
System.out.println(decrypt(publicKey));
|
}
|
|
}
|