*/
public static String encrypt(String value, String key, String initVector) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes(“UTF-8”));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(“UTF-8”), “AES”);
Cipher cipher = Cipher.getInstance(“AES/CBC/NOPADDING”);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(value.getBytes());
return Base64.encodeBase64String(encrypted);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
/**
-
解密
-
@param encrypted
-
@param key
-
@param initVector
-
@return
*/
public static String decrypt(String encrypted, String key, String initVector) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes(“UTF-8”));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(“UTF-8”), “AES”);
Cipher cipher = Cipher.getInstance(“AES/CBC/NOPADDING”);
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
return new String(original);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static void main(String[] args) {
// 秘钥
String key = “0628154244CF368D”;
//偏移量
String initVector = “FA163E4A9E64F0E4”;
String originalStr = “d98a2345#selfservice-instance2#1”;
System.out.println("明文 - " + originalStr);
String encryptedString = encrypt(originalStr,k