新增股票列表返回加密处理
This commit is contained in:
119
src/main/java/cn/stock/market/utils/AESUtil.java
Normal file
119
src/main/java/cn/stock/market/utils/AESUtil.java
Normal file
@@ -0,0 +1,119 @@
|
||||
package cn.stock.market.utils;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
* AES工具类
|
||||
* @author lxiaol
|
||||
* @date 2021年12月23日 13:58
|
||||
*/
|
||||
public class AESUtil {
|
||||
/**
|
||||
* 128位的AESkey
|
||||
*/
|
||||
private static final byte[] AES_KEY = PropertiesUtil.getProperty("aes.key","Jy112211Kj112211").getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
/**
|
||||
* AES解密
|
||||
*
|
||||
* @param data 待解密内容
|
||||
* @return 字节数组
|
||||
*/
|
||||
public static byte[] decrypt(byte[] data) throws InvalidKeyException, NoSuchAlgorithmException,
|
||||
NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
|
||||
Cipher cipher = getCipher(AES_KEY, Cipher.DECRYPT_MODE);
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* AES 加密操作
|
||||
*
|
||||
* @param data 待加密内容
|
||||
* @return 字节数组
|
||||
*/
|
||||
public static byte[] encrypt(byte[] data) throws InvalidKeyException, NoSuchAlgorithmException,
|
||||
NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
|
||||
Cipher cipher = getCipher(AES_KEY, Cipher.ENCRYPT_MODE);
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* AES 加密操作
|
||||
*
|
||||
* @param text 待加密内容
|
||||
* @return Base64转码后的加密数据
|
||||
*/
|
||||
public static String encrypt(String text) {
|
||||
byte[] byteContent = text.getBytes(StandardCharsets.UTF_8);
|
||||
try {
|
||||
byte[] result = encrypt(byteContent);// 加密
|
||||
return Base64.encode(result);//通过Base64转码返回
|
||||
} catch (Exception e) {
|
||||
// log.info("Error message: {}", e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* AES 解密操作
|
||||
*
|
||||
* @param text
|
||||
* @return
|
||||
*/
|
||||
public static String decrypt(String text) {
|
||||
byte[] bytes = Base64.decode(text);
|
||||
try {
|
||||
byte[] result = decrypt(bytes);
|
||||
return new String(result, StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
// log.info("Error message: {}", e.getMessage());
|
||||
e.printStackTrace();
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取加密器
|
||||
* @param key
|
||||
* @param model
|
||||
* @return
|
||||
* @throws NoSuchAlgorithmException
|
||||
* @throws NoSuchPaddingException
|
||||
* @throws InvalidKeyException
|
||||
*/
|
||||
private static Cipher getCipher(byte[] key, int model)
|
||||
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(model, secretKeySpec);
|
||||
return cipher;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String s = "{\n" +
|
||||
" \"mobile\": \"17570717251\",\n" +
|
||||
" \"password\": \"Dd112211\",\n" +
|
||||
" \"platType\": 1,\n" +
|
||||
" \"loginType\": 0,\n" +
|
||||
" \"sourceType\": 1,\n" +
|
||||
" \"equipmentModel\": \"IPHONE\",\n" +
|
||||
" \"version\":1\n" +
|
||||
"}";
|
||||
System.out.println(encrypt(s));
|
||||
System.out.println("Dd112211Ds112211".length());
|
||||
System.out.println(decrypt("Z3aCERCWi+nDUzv67h1/8PqS3CCuCgoj/YRTzkFNa0aNRvagi+wxpj9RsutL7nk2oo65ypyEYbjQXCI8fze8V4kMoVAb2rmoXqO3/DudVeTtY1J2784eXw+DS1QWZlZeHAHKiBaEwLcYe4XcsU9tpQIu6fE6cuSPGNetwN3C7qg6/78t4yUjCf49WW7u0/kErfgsSjMajGaVV/LOg74d2RlUcVBIIq6Us8JW3fFWPQA="));
|
||||
}
|
||||
|
||||
}
|
||||
53
src/main/java/cn/stock/market/utils/StreamUtil.java
Normal file
53
src/main/java/cn/stock/market/utils/StreamUtil.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package cn.stock.market.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 流工具类
|
||||
* @author zhouyl
|
||||
* @date 2023年12月09日 15:11
|
||||
*/
|
||||
@Slf4j
|
||||
public class StreamUtil {
|
||||
private static final Integer BUFFER_SIZE = 128;
|
||||
|
||||
/**
|
||||
* 将requestBody的数据转成字符串
|
||||
*
|
||||
* @param inputStream
|
||||
* @return
|
||||
*/
|
||||
public static String getBodyString(InputStream inputStream) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
BufferedReader bufferedReader = null;
|
||||
try {
|
||||
if (Objects.nonNull(inputStream)) {
|
||||
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
int bytesRead;
|
||||
char[] charBuffer = new char[BUFFER_SIZE];
|
||||
while ((bytesRead = bufferedReader.read(charBuffer)) != -1) {
|
||||
stringBuilder.append(charBuffer, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("get body fail,{}", e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (bufferedReader != null) {
|
||||
try {
|
||||
bufferedReader.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user