| | |
| | | |
| | | import cn.huge.base.common.exception.BaseException; |
| | | import com.thoughtworks.xstream.core.util.Base64Encoder; |
| | | import org.apache.commons.codec.DecoderException; |
| | | import org.apache.commons.codec.binary.Base64; |
| | | import org.apache.commons.codec.binary.Hex; |
| | | |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.*; |
| | | import javax.crypto.spec.IvParameterSpec; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.security.InvalidAlgorithmParameterException; |
| | | import java.security.InvalidKeyException; |
| | | import java.security.NoSuchAlgorithmException; |
| | | |
| | | /** |
| | | * @title: aes加密工具 |
| | |
| | | */ |
| | | public static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding"; |
| | | |
| | | private static final String iv = "1qaz#EDC123456(,"; |
| | | private static final String Algorithm = "AES"; |
| | | private static final String AlgorithmProvider = "AES/CBC/PKCS5Padding"; |
| | | |
| | | /** |
| | | * 加密 |
| | | * |
| | | * @param content |
| | | * @return |
| | | * @throws Exception |
| | |
| | | |
| | | /** |
| | | * 加密 |
| | | * |
| | | * @param content |
| | | * @return |
| | | * @throws Exception |
| | |
| | | |
| | | /** |
| | | * 加密 |
| | | * |
| | | * @param data 返回数据 |
| | | * @return ReturnBo |
| | | * @throws Exception |
| | |
| | | |
| | | /** |
| | | * 解密 |
| | | * |
| | | * @param content |
| | | * @return |
| | | * @throws Exception |
| | |
| | | |
| | | /** |
| | | * 解密 |
| | | * |
| | | * @param content |
| | | * @return |
| | | * @throws Exception |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | public static String decrypt(String enc, String uniqueKey) throws NoSuchPaddingException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidAlgorithmParameterException, InvalidKeyException, DecoderException, IllegalBlockSizeException, BadPaddingException { |
| | | byte[] key = uniqueKey.getBytes(); |
| | | SecretKey secretKey = new SecretKeySpec(key, Algorithm); |
| | | IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes("utf-8")); |
| | | Cipher cipher = Cipher.getInstance(AlgorithmProvider); |
| | | cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec); |
| | | byte[] hexBytes = hexStringToBytes(enc); |
| | | byte[] plainBytes = cipher.doFinal(hexBytes); |
| | | return new String(plainBytes, "UTF-8"); |
| | | } |
| | | |
| | | /** |
| | | * 将16进制字符串转换为byte数组 |
| | | * |
| | | * @param hexString |
| | | * @return |
| | | */ |
| | | private static byte[] hexStringToBytes(String hexString) throws DecoderException { |
| | | return Hex.decodeHex(hexString); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 字节数组转Base64编码 |
| | | * |