/**
*
*/
package com.hlmedicals.app.util;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import com.itextpdf.xmp.impl.Base64;
public class CryptographyTool {
private static CryptographyTool instance = new CryptographyTool();
private CryptographyTool() {
}
public static CryptographyTool getInstance() {
return instance;
}
public static void main(String[] args) {
String testStr = "ISoGK6h4zdhAk1bKiFpogT+zmza5I9q97hyJmVd+2Sg=";
CryptographyTool ct = new CryptographyTool();
try {
//System.out.println(ct.encrypt(testStr));
System.out.println(ct.decrypt(testStr));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加密方法
*
* @return
* @throws Exception
*/
public String encrypt(String encryptString) throws Exception {
// 判断Key是否为16位
byte[] raw = IConst.KEY.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(encryptString.getBytes("utf-8"));
return new String(new Base64().encode(encrypted));
}
/**
* 解密方法
*
* @param decryptString
* @return
* @throws Exception
*/
public String decrypt(String decryptString) throws Exception {
byte[] raw = IConst.KEY.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] encrypted1 = new Base64().decode(decryptString.getBytes());// 先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
}
}
*
*/
package com.hlmedicals.app.util;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import com.itextpdf.xmp.impl.Base64;
public class CryptographyTool {
private static CryptographyTool instance = new CryptographyTool();
private CryptographyTool() {
}
public static CryptographyTool getInstance() {
return instance;
}
public static void main(String[] args) {
String testStr = "ISoGK6h4zdhAk1bKiFpogT+zmza5I9q97hyJmVd+2Sg=";
CryptographyTool ct = new CryptographyTool();
try {
//System.out.println(ct.encrypt(testStr));
System.out.println(ct.decrypt(testStr));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加密方法
*
* @return
* @throws Exception
*/
public String encrypt(String encryptString) throws Exception {
// 判断Key是否为16位
byte[] raw = IConst.KEY.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(encryptString.getBytes("utf-8"));
return new String(new Base64().encode(encrypted));
}
/**
* 解密方法
*
* @param decryptString
* @return
* @throws Exception
*/
public String decrypt(String decryptString) throws Exception {
byte[] raw = IConst.KEY.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] encrypted1 = new Base64().decode(decryptString.getBytes());// 先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
}
}