87 lines
2.8 KiB
Java
87 lines
2.8 KiB
Java
package com.actionsoft.apps.coe.pal.datamigration;
|
|
|
|
import sun.misc.BASE64Decoder;
|
|
import sun.misc.BASE64Encoder;
|
|
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.KeyGenerator;
|
|
import java.net.URLEncoder;
|
|
import java.security.Key;
|
|
import java.security.SecureRandom;
|
|
|
|
/**
|
|
* AES加密解密工具类
|
|
*
|
|
* @author M-Y
|
|
*/
|
|
public class AesUtil {
|
|
|
|
public static String DES = "AES"; // optional value AES/DES/DESede
|
|
|
|
public static String CIPHER_ALGORITHM = "AES"; // optional value
|
|
// AES/DES/DESede
|
|
|
|
public Key getKey(String strKey) {
|
|
try {
|
|
if (strKey == null) {
|
|
strKey = "";
|
|
}
|
|
KeyGenerator _generator = KeyGenerator.getInstance("AES");
|
|
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
|
|
secureRandom.setSeed(strKey.getBytes());
|
|
_generator.init(128, secureRandom);
|
|
return _generator.generateKey();
|
|
} catch (Exception e) {
|
|
throw new RuntimeException(" 初始化密钥出现异常 ");
|
|
}
|
|
}
|
|
|
|
public String encrypt(String data, String key) throws Exception {
|
|
SecureRandom sr = new SecureRandom();
|
|
Key secureKey = getKey(key);
|
|
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
|
|
cipher.init(Cipher.ENCRYPT_MODE, secureKey, sr);
|
|
byte[] bt = cipher.doFinal(data.getBytes());
|
|
String strS = new BASE64Encoder().encode(bt);
|
|
return strS;
|
|
}
|
|
|
|
public String decrypt(String message, String key) {
|
|
try {
|
|
SecureRandom sr = new SecureRandom();
|
|
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
|
|
Key secureKey = getKey(key);
|
|
cipher.init(Cipher.DECRYPT_MODE, secureKey, sr);
|
|
byte[] res = new BASE64Decoder().decodeBuffer(message);
|
|
res = cipher.doFinal(res);
|
|
return new String(res);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public String DecryptedMsg(String message, String key) throws Exception, Exception {
|
|
String Msg1 = encrypt(message, key);
|
|
System.out.println("message is :" + Msg1);
|
|
String encryptMsg1 = URLEncoder.encode(encrypt(message, key), "UTF-8");
|
|
System.out.println("encrypted message is :" + encryptMsg1);
|
|
return encryptMsg1;
|
|
|
|
}
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
String data = "AUDIT##202210181611";
|
|
String key = "auditY809kUih23";
|
|
String url = "http://10.60.143.183:8088/portal/r/df?groupValue=7d3ca852-a0bd-42e6-80b1-3dcea6f55083&fileValue=d1135309-e376-4ec6-bd27-51947abe26ea&sid=null&repositoryName=output&appId=com.actionsoft.apps.coe.pal.output.pr&attachment=true&fileName=%E5%B9%BF%E5%91%8A%E5%88%9B%E6%84%8F%E7%94%9F%E6%88%90%E6%B5%81%E7%A8%8B_1.0.doc&lastModified=1666352134000";
|
|
String str1 = url.substring(0, url.indexOf("/df"));
|
|
|
|
System.out.println("str1>"+str1);
|
|
//String encryptMsg1 = AesUtil.encrypt(data, key);
|
|
//System.out.println("加密后:" + encryptMsg1);
|
|
/*message = "YPS5F%2F%2BVmdbVj0iuxrSINw%3D%3D";
|
|
System.out.println("解密后:" + AesUtil.decrypt(URLDecoder.decode(message), key));*/
|
|
}
|
|
|
|
}
|