fix:sonar
This commit is contained in:
@@ -9,7 +9,7 @@ import java.util.Collections;
|
||||
public class SdmResponse<T> implements Serializable {
|
||||
private int code;
|
||||
private String message;
|
||||
private T data;
|
||||
private transient T data;
|
||||
|
||||
public SdmResponse() {
|
||||
}
|
||||
|
||||
@@ -1,214 +0,0 @@
|
||||
package com.ccag.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.crypto.*;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.Security;
|
||||
import java.util.Base64;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AESUtil {
|
||||
|
||||
private static final String FINAL_PARAM = "XzKRqYnUypdE8VJ41yo/i0rMpZ0IlztSZ1PqWhr0q/c=";
|
||||
|
||||
/**
|
||||
* 块大小固定为8字节
|
||||
*/
|
||||
private final static String AES_CBC_PKCS5PADDING = "RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING";
|
||||
|
||||
/**
|
||||
* 加密字符串
|
||||
*
|
||||
* @param content 加密数据
|
||||
* @return 密文
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String encode(String content) throws Exception {
|
||||
byte[] key = Base64.getDecoder().decode(FINAL_PARAM);
|
||||
byte[] data = content.getBytes();
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
|
||||
Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
|
||||
return Base64.getEncoder().encodeToString(cipher.doFinal(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密字符串
|
||||
*
|
||||
* @param content 密文
|
||||
* @return 解密后的数据
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String decode(String content) throws Exception {
|
||||
byte[] key = Base64.getDecoder().decode(FINAL_PARAM);
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
|
||||
return new String(cipher.doFinal(Base64.getDecoder().decode(content)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机密钥
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static SecretKey keyGenerator() {
|
||||
KeyGenerator keyGenerator = null;
|
||||
try {
|
||||
keyGenerator = KeyGenerator.getInstance("AES");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
keyGenerator.init(256, new SecureRandom());
|
||||
return keyGenerator.generateKey();
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
// try {
|
||||
// String ret = encode("03BD691EB0264CECF79");
|
||||
// System.out.println("encode:" + ret);
|
||||
// String raw = decode(ret);
|
||||
// System.out.println("decode:" + raw);
|
||||
// } catch (Exception e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* get Cipher
|
||||
*
|
||||
* @param mode
|
||||
* @param sKey
|
||||
* @return
|
||||
*/
|
||||
private static Cipher getCipher(int mode, String sKey) {
|
||||
byte[] IV = new byte[16];
|
||||
SecureRandom random = new SecureRandom();
|
||||
random.nextBytes(IV);
|
||||
Cipher cipher = null;
|
||||
try {
|
||||
cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING);
|
||||
cipher.init(mode, getSecretKeySpec(sKey));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return cipher;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过Skey得到秘钥算法对象
|
||||
*
|
||||
* @param sKey
|
||||
* @return
|
||||
*/
|
||||
private static SecretKeySpec getSecretKeySpec(String sKey) {
|
||||
SecretKeySpec key = null;
|
||||
try {
|
||||
//"AES":请求的密钥算法的标准名称
|
||||
KeyGenerator kgen = KeyGenerator.getInstance("AES");
|
||||
//256:密钥生成参数;secure random:密钥生成器的随机源
|
||||
SecureRandom securerandom = SecureRandom.getInstance("SHA1PRNG");
|
||||
securerandom.setSeed(sKey.getBytes());
|
||||
kgen.init(256, securerandom);
|
||||
//生成秘密(对称)密钥
|
||||
SecretKey secretKey = kgen.generateKey();
|
||||
//返回基本编码格式的密钥
|
||||
byte[] enCodeFormat = secretKey.getEncoded();
|
||||
//根据给定的字节数组构造一个密钥。enCodeFormat:密钥内容;"AES":与给定的密钥内容相关联的密钥算法的名称
|
||||
key = new SecretKeySpec(enCodeFormat, "AES");
|
||||
//将提供程序添加到下一个可用位置
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件加密(传入文件流,输出流对象后直接处理即加密文件流后存储文件)
|
||||
*
|
||||
* @param inputStream
|
||||
* @param outputStream
|
||||
* @return
|
||||
*/
|
||||
public static boolean encryptFile(InputStream inputStream, OutputStream outputStream, String key) {
|
||||
try {
|
||||
CipherInputStream cipherInputStream = new CipherInputStream(inputStream, getCipher(Cipher.ENCRYPT_MODE, key));
|
||||
byte[] cache = new byte[1024];
|
||||
int nRead = 0;
|
||||
while ((nRead = cipherInputStream.read(cache)) != -1) {
|
||||
outputStream.write(cache, 0, nRead);
|
||||
outputStream.flush();
|
||||
}
|
||||
cipherInputStream.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* **上传文件加密(传入字节,加密后返回字节)**
|
||||
*
|
||||
* @param plainFile
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] encryptFile(byte[] plainFile, String key) {
|
||||
byte[] cipherText = null;
|
||||
try {
|
||||
cipherText = getCipher(Cipher.ENCRYPT_MODE, key).doFinal(plainFile);
|
||||
} catch (IllegalBlockSizeException | BadPaddingException e) {
|
||||
log.error("文件加密失败:" + e);
|
||||
}
|
||||
return cipherText;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载文件解密(传入文件流,输出流对象后直接处理即解密文件流后输出文件)
|
||||
*
|
||||
* @param inputStream
|
||||
* @param outputStream
|
||||
* @return
|
||||
*/
|
||||
public static boolean decryptFile(InputStream inputStream, OutputStream outputStream, String key) {
|
||||
try {
|
||||
CipherOutputStream cipherOutputStream = new CipherOutputStream(
|
||||
outputStream, getCipher(Cipher.DECRYPT_MODE, key));
|
||||
byte[] buffer = new byte[1024];
|
||||
int r;
|
||||
while ((r = inputStream.read(buffer)) >= 0) {
|
||||
cipherOutputStream.write(buffer, 0, r);
|
||||
}
|
||||
cipherOutputStream.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载文件解密(传入字节,解密后返回字节)
|
||||
*
|
||||
* @param cipherFile
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] decryptFile(byte[] cipherFile, String key) {
|
||||
byte[] cipherText = new byte[0];
|
||||
try {
|
||||
cipherText = getCipher(Cipher.DECRYPT_MODE, key).doFinal(cipherFile);
|
||||
} catch (IllegalBlockSizeException | BadPaddingException e) {
|
||||
log.error("文件解密失败:" + e);
|
||||
}
|
||||
return cipherText;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user