New file |
| | |
| | | package com.ruoyi.integration.drainage; |
| | | |
| | | |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import org.springframework.util.Base64Utils; |
| | | |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.spec.IvParameterSpec; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | |
| | | /** |
| | | * 定义AES加密解密工具类 |
| | | */ |
| | | public class AESUtil { |
| | | |
| | | private static final String KEY_ALGORITHM = "AES";//加密方式 |
| | | |
| | | private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";//默认的加密算法 |
| | | |
| | | private static final String KEY = "1234567890abcdef";//密码 |
| | | |
| | | private static final String IV_PARAMETER = "1234567890abcdef";//偏移量 |
| | | |
| | | private static final String CHARSET = "UTF-8";//编码 |
| | | |
| | | |
| | | |
| | | /** |
| | | * 加密操作 |
| | | * @param content 待加密内容 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static String encrypt(String content) { |
| | | return encrypt(content, KEY, IV_PARAMETER); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 加密操作 |
| | | * @param content 待加密内容 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static String encrypt(String content, String key, String iv_parameter) { |
| | | try { |
| | | if(StringUtils.isEmpty(content)){ |
| | | return content; |
| | | } |
| | | Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); |
| | | byte[] raw = key.getBytes(CHARSET); |
| | | SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM); |
| | | IvParameterSpec iv = new IvParameterSpec(iv_parameter.getBytes()); |
| | | cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); |
| | | byte[] encrypted = cipher.doFinal(content.getBytes(CHARSET)); |
| | | return Base64Utils.encodeToString(encrypted); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | public static String decrypt(String content) { |
| | | return decrypt(content, KEY, IV_PARAMETER); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | *解密操作 |
| | | * @param content 待解密内容 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static String decrypt(String content, String key, String iv_parameter) { |
| | | try { |
| | | if(StringUtils.isEmpty(content)){ |
| | | return content; |
| | | } |
| | | byte[] raw = key.getBytes(CHARSET); |
| | | SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM); |
| | | Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); |
| | | IvParameterSpec iv = new IvParameterSpec(iv_parameter.getBytes()); |
| | | cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); |
| | | |
| | | byte[] encrypted1 = Base64Utils.decodeFromString(content); |
| | | byte[] original = cipher.doFinal(encrypted1); |
| | | String originalString = new String(original, CHARSET); |
| | | return originalString; |
| | | } catch (Exception ex) { |
| | | ex.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | public static void main(String[] ages){ |
| | | // String encrypt = AESUtil.encrypt("{\"OperatorSecret\":\"cn51EeFQDTSMsn11R1ZNsYSEYBxDYcKK\",\"OperatorID\":\"MA25CNM38\"}", "YPFVz1OvAS4nSwLW", "5tLoP60aR9QUB5Mx"); |
| | | // System.err.println(encrypt); |
| | | String travel = AESUtil.decrypt("9gVnNqAh9O7IuqqbyD8G1ukK86O3g0bUBLzM1LJL060bNjPJCtW0wWRjr756abxIm+nOuKkjMy7/DYScMcnl2KSYJJJb6Xc4Aza8L24Mp/UKQyW6Fe9m+ThDVUfMwvn7U2OxHDXniBUdmpNi2Ex9uHa/R18H2ka0DCv/bKCm/F4KTfz48Et/1L64JTTmj7l4the0Wr7KGiVewPIEE03LF5lF+h9j0W8czC6Gx5UJOMU=", "YPFVz1OvAS4nSwLW", "5tLoP60aR9QUB5Mx"); |
| | | System.err.println(travel); |
| | | } |
| | | } |