| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package com.idea.util;
- import javax.crypto.Cipher;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import java.nio.charset.StandardCharsets;
- import java.security.MessageDigest;
- import java.util.Base64;
- public class AESUtil {
- public static String decryptKey(String tokenId, String pw) throws Exception {
- // MD5加密
- byte[] md5Byte =MessageDigest.getInstance("MD5").digest(tokenId.getBytes());
- // 转为16进制字符串
- String hexStr = bytesToHex(md5Byte);
- // 截取
- String aesKey = hexStr.substring(8, 24);
- // AES解密,返回结果
- return decrypt(pw, aesKey, aesKey);
- }
- private static String bytesToHex(byte[] bytes) {
- StringBuilder sb = new StringBuilder(bytes.length * 2);
- for (int i = 0; i < bytes.length; i++) {
- sb.append(Character.toUpperCase(Character.forDigit((bytes[i] >> 4) & 0x0F, 16)));
- sb.append(Character.toUpperCase(Character.forDigit(bytes[i] & 0x0F, 16)));
- }
- return sb.toString();
- }
- private static String decrypt(String pw, String aesKey, String viPara) throws Exception {
- IvParameterSpec zeroIv = new IvParameterSpec(viPara.getBytes());
- SecretKeySpec key = new SecretKeySpec(aesKey.getBytes(), "AES");
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
- cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
- return new String(cipher.doFinal(Base64.getDecoder().decode(pw)), StandardCharsets.UTF_8);
- }
- }
|