| | |
| | | package com.vci.web.util; |
| | | |
| | | import com.vci.corba.common.PLException; |
| | | import com.vci.starter.web.exception.VciBaseException; |
| | | import org.apache.commons.codec.binary.Hex; |
| | | import org.springframework.lang.Nullable; |
| | | import org.springframework.util.DigestUtils; |
| | | |
| | | import javax.crypto.Mac; |
| | | import javax.crypto.SecretKey; |
| | | import javax.crypto.*; |
| | | import javax.crypto.spec.IvParameterSpec; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | import java.security.InvalidKeyException; |
| | | import java.security.MessageDigest; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.security.*; |
| | | import java.security.spec.InvalidKeySpecException; |
| | | import java.security.spec.PKCS8EncodedKeySpec; |
| | | import java.util.Base64; |
| | | |
| | | /** |
| | | * 加密相关工具类直接使用Spring util封装,减少jar依赖 |
| | |
| | | } |
| | | |
| | | /** |
| | | * aes解密 |
| | | * @param secretKey 秘钥 |
| | | * @param encryptedData Data |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String decryptAes(String encryptedData, String secretKey) throws Exception { |
| | | // AES 密钥长度需要是 128, 192, 或 256 位(16, 24, 或 32 字节) |
| | | byte[] keyBytes = secretKey.getBytes("UTF-8"); |
| | | SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); |
| | | |
| | | // 初始化向量 IV 也需要是 128 位(16 字节) |
| | | byte[] ivBytes = secretKey.substring(0, 16).getBytes("UTF-8"); |
| | | IvParameterSpec iv = new IvParameterSpec(ivBytes); |
| | | |
| | | // 解密 |
| | | Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| | | cipher.init(Cipher.DECRYPT_MODE, key, iv); |
| | | |
| | | // 解密的数据需要是 Base64 编码的,因为 JavaScript 的 CryptoJS 默认返回 Base64 编码的字符串 |
| | | byte[] decodedValue = Base64.getDecoder().decode(encryptedData); |
| | | byte[] decryptedBytes = cipher.doFinal(decodedValue); |
| | | |
| | | return new String(decryptedBytes, "UTF-8"); |
| | | } |
| | | |
| | | /** |
| | | * digest HMac |
| | | * |
| | | * @param algorithm 算法 |