feat: 第三方登录完善
This commit is contained in:
@@ -1,110 +0,0 @@
|
||||
package com.schisandra.system.common.utils;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @ClassName AESUtils 一定要选择16位密钥长度,也就是KEY_LENGTH=16*8,36的话就需要修改环境的jar包。
|
||||
* @Description TODO
|
||||
* @Author L
|
||||
* @Date 2024/1/26 17:27
|
||||
*/
|
||||
public class AESUtils {
|
||||
/**
|
||||
* 加密算法AES
|
||||
*/
|
||||
private static final String KEY_ALGORITHM = "AES";
|
||||
|
||||
/**
|
||||
* key的长度,Wrong key size: must be equal to 128, 192 or 256
|
||||
* 传入时需要16、24、36
|
||||
*/
|
||||
private static final int KEY_LENGTH = 16 * 8;
|
||||
|
||||
/**
|
||||
* 算法名称/加密模式/数据填充方式
|
||||
* 默认:AES/ECB/PKCS5Padding
|
||||
*/
|
||||
private static final String ALGORITHMS = "AES/ECB/PKCS5Padding";
|
||||
|
||||
/**
|
||||
* 后端AES的key,由静态代码块赋值
|
||||
*/
|
||||
public static String key;
|
||||
|
||||
|
||||
static {
|
||||
key = getKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取key
|
||||
*/
|
||||
public static String getKey() {
|
||||
int length = KEY_LENGTH / 8;
|
||||
StringBuilder uid = new StringBuilder(length);
|
||||
//产生32位的强随机数
|
||||
Random rd = new SecureRandom();
|
||||
for (int i = 0; i < length; i++) {
|
||||
//产生0-2的3位随机数
|
||||
switch (rd.nextInt(3)) {
|
||||
case 0:
|
||||
//0-9的随机数
|
||||
uid.append(rd.nextInt(10));
|
||||
break;
|
||||
case 1:
|
||||
//ASCII在65-90之间为大写,获取大写随机
|
||||
uid.append((char) (rd.nextInt(26) + 65));
|
||||
break;
|
||||
case 2:
|
||||
//ASCII在97-122之间为小写,获取小写随机
|
||||
uid.append((char) (rd.nextInt(26) + 97));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return uid.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* AES 加密
|
||||
*
|
||||
* @param content 加密的字符串
|
||||
* @param encryptKey key值
|
||||
*/
|
||||
public static String encrypt(String content, String encryptKey) throws Exception {
|
||||
//设置Cipher对象
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHMS);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), KEY_ALGORITHM));
|
||||
|
||||
//调用doFinal
|
||||
// 转base64
|
||||
return Base64.encodeBase64String(cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* AES 解密
|
||||
*
|
||||
* @param encryptStr 解密的字符串
|
||||
* @param decryptKey 解密的key值
|
||||
*/
|
||||
public static String decrypt(String encryptStr, String decryptKey) throws Exception {
|
||||
//base64格式的key字符串转byte
|
||||
byte[] decodeBase64 = Base64.decodeBase64(encryptStr);
|
||||
|
||||
|
||||
//设置Cipher对象
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHMS);
|
||||
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), KEY_ALGORITHM));
|
||||
//调用doFinal解密
|
||||
return new String(cipher.doFinal(decodeBase64));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package com.schisandra.system.common.utils;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
|
||||
public class MD5Util {
|
||||
//十六进制下数字到字符的映射数组
|
||||
private final static String[] hexDigits = {"0", "1", "2", "3", "4",
|
||||
"5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
|
||||
|
||||
|
||||
/**
|
||||
* 把inputString加密
|
||||
*/
|
||||
public static String md5(String inputString) {
|
||||
return encodeByMD5(inputString);
|
||||
}
|
||||
/**
|
||||
* 对字符串进行MD5加密
|
||||
*/
|
||||
private static String encodeByMD5(String originString) {
|
||||
if (originString != null) {
|
||||
try {
|
||||
//创建具有指定算法名称的信息摘要
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
//使用指定的字节数组对摘要进行最后更新,然后完成摘要计算
|
||||
byte[] results = md.digest(originString.getBytes("utf-8"));
|
||||
//将得到的字节数组变成字符串返回
|
||||
String resultString = byteArrayToHexString(results);
|
||||
return resultString.toUpperCase();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* 转换字节数组为十六进制字符串
|
||||
*
|
||||
* @param
|
||||
* @return 十六进制字符串
|
||||
*/
|
||||
private static String byteArrayToHexString(byte[] b) {
|
||||
StringBuffer resultSb = new StringBuffer();
|
||||
for (int i = 0; i < b.length; i++) {
|
||||
resultSb.append(byteToHexString(b[i]));
|
||||
}
|
||||
return resultSb.toString();
|
||||
}
|
||||
|
||||
private static String byteToHexString(byte b) {
|
||||
int n = b;
|
||||
if (n < 0) {
|
||||
n += 256;
|
||||
}
|
||||
int d1 = n / 16;
|
||||
int d2 = n % 16;
|
||||
return hexDigits[d1] + hexDigits[d2];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,172 +0,0 @@
|
||||
package com.schisandra.system.common.utils;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.security.Key;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.interfaces.RSAPrivateKey;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @ClassName RSAUtils
|
||||
* @Description TODO
|
||||
* @Author L
|
||||
* @Date 2024/1/26 17:28
|
||||
*/
|
||||
public class RSAUtils {
|
||||
|
||||
/**
|
||||
* 加密算法RSA
|
||||
*/
|
||||
private static final String KEY_ALGORITHM = "RSA";
|
||||
|
||||
/**
|
||||
* 算法名称/加密模式/数据填充方式
|
||||
* 默认:RSA/ECB/PKCS1Padding
|
||||
*/
|
||||
private static final String ALGORITHMS = "RSA/ECB/PKCS1Padding";
|
||||
|
||||
/**
|
||||
* RSA最大加密明文大小
|
||||
*/
|
||||
private static final int MAX_ENCRYPT_BLOCK = 245;
|
||||
|
||||
/**
|
||||
* RSA最大解密密文大小
|
||||
*/
|
||||
private static final int MAX_DECRYPT_BLOCK = 256;
|
||||
|
||||
/**
|
||||
* RSA 位数 如果采用2048 上面最大加密和最大解密则须填写: 245 256
|
||||
*/
|
||||
private static final int INITIALIZE_LENGTH = 2048;
|
||||
|
||||
/**
|
||||
* 后端RSA的密钥对(公钥和私钥)Map,由静态代码块赋值
|
||||
*/
|
||||
private static final Map<String, String> map = new LinkedHashMap<>(2);
|
||||
|
||||
/**
|
||||
* 生成密钥对(公钥和私钥)
|
||||
*/
|
||||
|
||||
public static Map<String,String> genKeyPair() throws Exception {
|
||||
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
|
||||
keyPairGen.initialize(INITIALIZE_LENGTH);
|
||||
KeyPair keyPair = keyPairGen.generateKeyPair();
|
||||
// 获取公钥
|
||||
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
|
||||
// 获取私钥
|
||||
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
|
||||
// 得到公钥字符串
|
||||
String publicKeyString = Base64.encodeBase64String(publicKey.getEncoded());
|
||||
// 得到私钥字符串
|
||||
String privateKeyString = Base64.encodeBase64String((privateKey.getEncoded()));
|
||||
map.put("publicKey",publicKeyString);
|
||||
map.put("privateKey",privateKeyString);
|
||||
return map;
|
||||
}
|
||||
public static String getPrivateKey(){
|
||||
return map.get("privateKey");
|
||||
}
|
||||
public static String getPublicKey(){
|
||||
return map.get("publicKey");
|
||||
}
|
||||
/**
|
||||
* RSA私钥解密
|
||||
* @param data BASE64编码过的密文
|
||||
* @param privateKey 私钥(BASE64编码)
|
||||
* @return utf-8编码的明文
|
||||
*/
|
||||
public static byte[] decryptByPrivateKey(byte[] data, String privateKey) throws Exception {
|
||||
//base64格式的key字符串转Key对象
|
||||
Key privateK = KeyFactory.getInstance(KEY_ALGORITHM).generatePrivate(new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey)));
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHMS);
|
||||
cipher.init(Cipher.DECRYPT_MODE, privateK);
|
||||
|
||||
//分段进行解密操作
|
||||
return encryptAndDecryptOfSubsection(data, cipher, MAX_DECRYPT_BLOCK);
|
||||
}
|
||||
|
||||
/**
|
||||
* RSA公钥加密
|
||||
* @param data BASE64编码过的密文
|
||||
* @param publicKey 公钥(BASE64编码)
|
||||
* @return utf-8编码的明文
|
||||
*/
|
||||
public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
|
||||
//base64格式的key字符串转Key对象
|
||||
Key publicK = KeyFactory.getInstance(KEY_ALGORITHM).generatePublic(new X509EncodedKeySpec(Base64.decodeBase64(publicKey)));
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHMS);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, publicK);
|
||||
//分段进行加密操作
|
||||
return encryptAndDecryptOfSubsection(data, cipher, MAX_ENCRYPT_BLOCK);
|
||||
}
|
||||
|
||||
/**
|
||||
* RSA公钥解密
|
||||
* @param data BASE64编码过的密文
|
||||
* @param publicKey RSA公钥
|
||||
* @return utf-8编码的明文
|
||||
*/
|
||||
public static byte[] pubKeyDec(byte[] data, String publicKey) throws Exception {
|
||||
//base64格式的key字符串转Key对象
|
||||
Key privateK = KeyFactory.getInstance(KEY_ALGORITHM).generatePublic(new X509EncodedKeySpec(Base64.decodeBase64(publicKey)));
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHMS);
|
||||
cipher.init(Cipher.DECRYPT_MODE, privateK);
|
||||
|
||||
//分段进行解密操作
|
||||
return encryptAndDecryptOfSubsection(data, cipher, MAX_DECRYPT_BLOCK);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* RSA私钥加密
|
||||
* @param data 待加密的明文
|
||||
* @param privateKey RSA私钥
|
||||
* @return 经BASE64编码后的密文
|
||||
*/
|
||||
public static byte[] privKeyEnc(byte[] data, String privateKey) throws Exception {
|
||||
|
||||
//base64格式的key字符串转Key对象
|
||||
Key publicK = KeyFactory.getInstance(KEY_ALGORITHM).generatePrivate(new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey)));
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHMS);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, publicK);
|
||||
|
||||
//分段进行加密操作
|
||||
return encryptAndDecryptOfSubsection(data, cipher, MAX_ENCRYPT_BLOCK);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分段进行加密、解密操作
|
||||
*/
|
||||
private static byte[] encryptAndDecryptOfSubsection(byte[] data, Cipher cipher, int encryptBlock) throws Exception {
|
||||
int inputLen = data.length;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
int offSet = 0;
|
||||
byte[] cache;
|
||||
int i = 0;
|
||||
// 对数据分段加密
|
||||
while (inputLen - offSet > 0) {
|
||||
if (inputLen - offSet > encryptBlock) {
|
||||
cache = cipher.doFinal(data, offSet, encryptBlock);
|
||||
} else {
|
||||
cache = cipher.doFinal(data, offSet, inputLen - offSet);
|
||||
}
|
||||
out.write(cache, 0, cache.length);
|
||||
i++;
|
||||
offSet = i * encryptBlock;
|
||||
}
|
||||
out.close();
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user