AES解密測試

This commit is contained in:
Martin
2024-09-06 10:20:01 +08:00
parent 528647a0bc
commit 59271ce709
2 changed files with 62 additions and 0 deletions

View 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;
}
}