AES解密測試
This commit is contained in:
31
common/src/main/java/com/yunbao/common/utils/AesUtils.java
Normal file
31
common/src/main/java/com/yunbao/common/utils/AesUtils.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.yunbao.common.utils;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
public class AesUtils {
|
||||
|
||||
public static String decrypt(String encrypted) {
|
||||
try {
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec("76b60dd0a5d7cafdqqew".getBytes(), "AES");
|
||||
Cipher cipher = Cipher.getInstance("AES");
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
|
||||
byte[] original = cipher.doFinal(hexToByteArray(encrypted));
|
||||
return new String(original);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] hexToByteArray(String hex) {
|
||||
if (hex == null || hex.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
byte[] ba = new byte[hex.length() / 2];
|
||||
for (int i = 0; i < ba.length; i++) {
|
||||
ba[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
|
||||
}
|
||||
return ba;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user