forked from rongchao/epmet-cloud-rizhao
				
			
				 6 changed files with 151 additions and 28 deletions
			
			
		| @ -0,0 +1,78 @@ | |||
| package com.epmet.opendata.util; | |||
| 
 | |||
| import javax.crypto.Cipher; | |||
| import javax.crypto.spec.SecretKeySpec; | |||
| 
 | |||
| /*** | |||
|  * Aes加解密工具类 | |||
|  * @author www | |||
|  * @date 2022/6/6/0006 11:07 | |||
|  */ | |||
| public class AesUtils { | |||
| 
 | |||
| 
 | |||
|     public static String encryptByAES(String input, String key) throws Exception { | |||
|         // 算法
 | |||
|         String algorithm = "AES"; | |||
|         String transformation = "AES"; | |||
|         // Cipher:密码,获取加密对象
 | |||
|         // transformation:参数表示使用什么类型加密
 | |||
|         Cipher cipher = Cipher.getInstance(transformation); | |||
|         // 指定秘钥规则
 | |||
|         // 第一个参数表示:密钥,key的字节数组 长度必须是16位
 | |||
|         // 第二个参数表示:算法
 | |||
|         SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm); | |||
|         // 对加密进行初始化
 | |||
|         // 第一个参数:表示模式,有加密模式和解密模式
 | |||
|         // 第二个参数:表示秘钥规则
 | |||
|         cipher.init(Cipher.ENCRYPT_MODE, sks); | |||
|         // 进行加密
 | |||
|         byte[] bytes = cipher.doFinal(input.getBytes()); | |||
|         return bytesToHexString(bytes); | |||
|     } | |||
| 
 | |||
|     public static String decryptByAES(String input, String key) throws Exception { | |||
|         // 算法
 | |||
|         String algorithm = "AES"; | |||
|         String transformation = "AES"; | |||
|         // Cipher:密码,获取加密对象
 | |||
|         // transformation:参数表示使用什么类型加密
 | |||
|         Cipher cipher = Cipher.getInstance(transformation); | |||
|         // 指定秘钥规则
 | |||
|         // 第一个参数表示:密钥,key的字节数组 长度必须是16位
 | |||
|         // 第二个参数表示:算法
 | |||
|         SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm); | |||
|         // 对加密进行初始化
 | |||
|         // 第一个参数:表示模式,有加密模式和解密模式
 | |||
|         // 第二个参数:表示秘钥规则
 | |||
|         cipher.init(Cipher.DECRYPT_MODE, sks); | |||
|         // 进行解密
 | |||
|         byte[] inputBytes = hexStringToBytes(input); | |||
|         byte[] bytes = cipher.doFinal(inputBytes); | |||
|         return new String(bytes); | |||
|     } | |||
| 
 | |||
|     private static byte[] hexStringToBytes(String hexString) { | |||
|         if (hexString.length() % 2 != 0) throw new IllegalArgumentException("hexString length not valid"); | |||
|         int length = hexString.length() / 2; | |||
|         byte[] resultBytes = new byte[length]; | |||
|         for (int index = 0; index < length; index++) { | |||
|             String result = hexString.substring(index * 2, index * 2 + 2); | |||
|             resultBytes[index] = Integer.valueOf(Integer.parseInt(result, 16)).byteValue(); | |||
|         } | |||
|         return resultBytes; | |||
|     } | |||
| 
 | |||
|     private static String bytesToHexString(byte[] sources) { | |||
|         if (sources == null) return null; | |||
|         StringBuilder stringBuffer = new StringBuilder(); | |||
|         for (byte source : sources) { | |||
|             String result = Integer.toHexString(source & 0xff); | |||
|             if (result.length() < 2) { | |||
|                 result = "0" + result; | |||
|             } | |||
|             stringBuffer.append(result); | |||
|         } | |||
|         return stringBuffer.toString(); | |||
|     } | |||
| } | |||
					Loading…
					
					
				
		Reference in new issue