import com.alibaba.fastjson.JSON;
import org.apache.logging.log4j.util.Base64Util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
@Component
public class RSAUtils {
//定义加密方式
public static final String KEY_RSA = "RSA";
/**
* **
* RSA最大加密大小
*/
private final static int MAX_ENCRYPT_BLOCK = 117;
/**
* **
* RSA最大解密大小
*/
private final static int MAX_DECRYPT_BLOCK = 128;
@Value("${rsa.public_key}")
private String PUBLIC_KEY;
@Value("${rsa.private_key}")
private String PRIVATE_KEY;
/**
* BASE64 解码
*
* @param key 需要Base64解码的字符串
* @return 字节数组
*/
public static byte[] decryptBase64(String key) {
return Base64.getDecoder().decode(key);
}
/**
* BASE64 编码
*
* @param key 需要Base64编码的字节数组
* @return 字符串
*/
public static String encryptBase64(byte[] key) {
return new String(Base64.getEncoder().encode(key));
}
/**
* 公钥加密(分段)
*
* @return
*/
public String publicEncrypt(Object obj) {
try {
String encryptingStr = JSON.toJSONString(obj);
encryptingStr=java.net.URLEncoder.encode(encryptingStr);
// 将公钥由字符串转为UTF-8格式的字节数组
byte[] publicKeyBytes = decryptBase64(PUBLIC_KEY);
// 获得公钥
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
// 取得待加密数据
byte[] data = encryptingStr.getBytes("UTF-8");
KeyFactory factory;
factory = KeyFactory.getInstance(KEY_RSA);
PublicKey publicKey = factory.generatePublic(keySpec);
// 对数据加密
Cipher cipher = Cipher.getInstance(factory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
// 返回加密后由Base64编码的加密信息
int inputLen = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_ENCRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
return encryptBase64(decryptedData);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 私钥解密(分段)
*
* @param encryptedStr
* @return
*/
public String decryptByPrivateKey(String encryptedStr) {
try {
// 对私钥解密
byte[] privateKeyBytes = decryptBase64(PRIVATE_KEY);
// 获得私钥
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
// 获得待解密数据
byte[] data = decryptBase64(encryptedStr);
KeyFactory factory = KeyFactory.getInstance(KEY_RSA);
PrivateKey privateKey = factory.generatePrivate(keySpec);
// 对数据解密
Cipher cipher = Cipher.getInstance(factory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
// 返回UTF-8编码的解密信息
int inputLen = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(data, offSet, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(data, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_DECRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
return new String(decryptedData, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 公钥加密(分段)
*
* @param encryptingStr
* @param publicKeyStr
* @return
*/
public static String encryptByPublicKey(String encryptingStr, String publicKeyStr) {
try {
// 将公钥由字符串转为UTF-8格式的字节数组
byte[] publicKeyBytes = decryptBase64(publicKeyStr);
// 获得公钥
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
// 取得待加密数据
byte[] data = encryptingStr.getBytes("UTF-8");
KeyFactory factory;
factory = KeyFactory.getInstance(KEY_RSA);
PublicKey publicKey = factory.generatePublic(keySpec);
// 对数据加密
Cipher cipher = Cipher.getInstance(factory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
// 返回加密后由Base64编码的加密信息
int inputLen = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_ENCRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
return encryptBase64(decryptedData);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 私钥解密(分段)
*
* @param encryptedStr
* @param privateKeyStr
* @return
*/
public static String decryptByPrivateKey(String encryptedStr, String privateKeyStr) {
try {
// 对私钥解密
byte[] privateKeyBytes = decryptBase64(privateKeyStr);
// 获得私钥
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
// 获得待解密数据
byte[] data = decryptBase64(encryptedStr);
KeyFactory factory = KeyFactory.getInstance(KEY_RSA);
PrivateKey privateKey = factory.generatePrivate(keySpec);
// 对数据解密
Cipher cipher = Cipher.getInstance(factory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
// 返回UTF-8编码的解密信息
int inputLen = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(data, offSet, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(data, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_DECRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
return new String(decryptedData, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
网友评论