客户端配置迁移
This commit is contained in:
parent
e9d639d3b8
commit
297e069046
|
@ -2,5 +2,3 @@ spring.application.name = LuckyFrameClient
|
|||
server.port = 6633
|
||||
spring.servlet.multipart.max-file-size=30MB
|
||||
spring.servlet.multipart.max-request-size=30MB
|
||||
client.config.slat=12331312
|
||||
client.config.pass=123
|
|
@ -36,3 +36,6 @@ jenkins.password=test1
|
|||
#==================Web Driver配置========================
|
||||
#是否高亮显示 true打开 false关闭
|
||||
webdriver.highlight=false
|
||||
#==================ソヘサァカヒナ葷テテワヤソ========================
|
||||
client.config.privateKey=MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKZzwv5UN94a4OdMlmRS20n7x5e267LAV9Ue8wFOxUL2BJH33+mrJBqfj8rpcSgp7K+etX2kFq35VeeK3cr+ndBq9z+fO1FHw1kfsE8SQnSw17jfM1pdekC40gle+1bRKlm28zoLYcUMkQ8UFyVEyn72JH5ByJf9zNxWPVAIuzh3AgMBAAECgYBUrXd5Yua4+vuHL5MinnQogwnayJfHa8pDBWeiNqxrkDRq/arPeNz5HktMcs26HPnnrdmufRd8ked9z0tdxYIKfxeZrWuY3+YQkthGkROcWPLc4H1BdlXMTSoDS1hoTokehJ22i+SH2Qr9IYs++xHdEg7s5lPkoi9eVboTpOC0wQJBAM+qft5kp7gH3XV/XtpBeUxwB5rmpPVRIBPHxjHURhFI19+VDwidIq8MbN4tP1ukqegGNj5tWotNpoP9S+dS49kCQQDNMZiSW9HsX51CP7tJ1PVGnmE7QN5pu5TnlEl2WJQup6JamzvMiNYbJ4XzI4tBoL9+n4iAqbDyNIKY9y7ci1zPAkAT/PEhLscF65vhR1U15L9sIAxbWH/gLOLNhJkLCseDumMs3ggIaDeCacvAxtImW/8Omip2b8gbSGoSojrAFTZhAkAsWmoWSM6pf0loxonfPa7+4sE1o6HvzOI05HK0qBTvuMB5QVC9t15AZFyp36b8Opjp7+X7mbJryDnqgpOYjG81AkEAlgZbPEKrFmvBAdhnsOdLywYO+iaKVHsUV/xB65gOJk9TtJNG5GQd9zJkTJ589itK4Avg0x247PELbQjd3Tge2w==
|
||||
client.config.publicKey=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmc8L+VDfeGuDnTJZkUttJ+8eXtuuywFfVHvMBTsVC9gSR99/pqyQan4/K6XEoKeyvnrV9pBat+VXnit3K/p3Qavc/nztRR8NZH7BPEkJ0sNe43zNaXXpAuNIJXvtW0SpZtvM6C2HFDJEPFBclRMp+9iR+QciX/czcVj1QCLs4dwIDAQAB
|
||||
|
|
|
@ -1,70 +1,257 @@
|
|||
package luckyclient.utils;
|
||||
|
||||
import luckyclient.utils.config.SysConfig;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.SecretKeyFactory;
|
||||
import javax.crypto.spec.PBEKeySpec;
|
||||
import javax.crypto.spec.PBEParameterSpec;
|
||||
import java.security.Key;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.security.*;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
|
||||
/**
|
||||
* @author fengjian
|
||||
* @date 2020/6/22 22:17
|
||||
*/
|
||||
@Component
|
||||
public class EncryptionUtils {
|
||||
|
||||
private static String salt;
|
||||
/**
|
||||
* RSA最大加密明文大小
|
||||
*/
|
||||
private static final int MAX_ENCRYPT_BLOCK = 117;
|
||||
|
||||
private static String pass;
|
||||
/**
|
||||
* RSA最大解密密文大小
|
||||
*/
|
||||
private static final int MAX_DECRYPT_BLOCK = 128;
|
||||
|
||||
@Value("${client.config.slat}")
|
||||
public void setSalt(String salt) {
|
||||
EncryptionUtils.salt = salt;
|
||||
/**
|
||||
* 默认公钥
|
||||
*/
|
||||
private static final String DEFAULT_PUBLIC_KEY="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCadmkcep05BmZ3aWH72ab8iw8xD4XYrXmeySwBgKQY4mhHo2MrT8fKiNaG0PC/Jy09inPczBPqf/IPILlE79ujgpc84bHnR27u9IH7kJlyoLiPRGoN+oQbWJakmYTwGkdG4z1Re9xoKi4Ww1WShkvJspMwOWtkwfwub5zkvQtSWQIDAQAB";
|
||||
|
||||
/**
|
||||
* 默认私钥
|
||||
*/
|
||||
private static final String DEFAULT_PRIVATE_KEY="MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJp2aRx6nTkGZndpYfvZpvyLDzEPhditeZ7JLAGApBjiaEejYytPx8qI1obQ8L8nLT2Kc9zME+p/8g8guUTv26OClzzhsedHbu70gfuQmXKguI9Eag36hBtYlqSZhPAaR0bjPVF73GgqLhbDVZKGS8mykzA5a2TB/C5vnOS9C1JZAgMBAAECgYBNTjYNKtDFWY6u9O81PRl2C6LuyvYSG8Bi2AxONDPswGOwdvWLF8LGevXjQ286PEFIK6MRPpI5Kw/awmX3OpSR10nAzLHo7KU03+1+71EpGcGt0OAudDG+Qzzz10rjyoBwV21d8utoJmy4m5MLbp7yxxZ0caGNfkJMj7QJyxsQAQJBAOtTwyqdGbhLle0rD/9WhK5huFBAaXCw21mJK/wkByVFk9ynHN1P0e3fgS4S2KOyWGEwMgfaxRxvn+Tmj8sQLkkCQQCoCBhihIZj0epYvdQdf63sgrzVlUr3d3IIlKio4JLfvo4gFGpQjV/mOlyS7AGNWf5iDFzJvpXoXET5GYkmpEORAkA784LtAEjlIpx3Z1kT+76hjlOeXkp+Yw/+p2uFOMh5PliFBi3cU9FvgFkwm6yFR5IscFLOnXVJ4UYi0nofiWfBAkBMZvnneci9hIog9ZeIHjEP9FY2a16d7RLNsgKKXyqJT9TB42Z/3/h1751+NI90HTJclLBwDxeMgr/d3+2Lw27xAkBdQqmrWTAmHPGS48CZ/VYu9repRhDmV+8nsWtX1fdU410kcfYgib7WX9Y22v4vGQrVt72waBvvEvbjWjXH+Ael";
|
||||
|
||||
/**
|
||||
* 用户配置的私钥
|
||||
*/
|
||||
private static String USER_PRIVATE_KEY= SysConfig.getConfiguration().getProperty("client.config.privateKey");
|
||||
|
||||
/**
|
||||
* 用户配置的公钥
|
||||
*/
|
||||
private static String USER_PUBLIC_KEY= SysConfig.getConfiguration().getProperty("client.config.publicKey");
|
||||
|
||||
@Value("${client.config.privateKey}")
|
||||
public void setUserPrivateKey(String userPrivateKey) {
|
||||
USER_PRIVATE_KEY = userPrivateKey;
|
||||
}
|
||||
|
||||
@Value("${client.config.pass}")
|
||||
public void setPass(String pass) {
|
||||
EncryptionUtils.pass = pass;
|
||||
@Value("${client.config.publicKey}")
|
||||
public void setUserPublicKey(String userPublicKey) {
|
||||
USER_PUBLIC_KEY = userPublicKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取密钥对
|
||||
*
|
||||
* @return 密钥对
|
||||
*/
|
||||
public static KeyPair getKeyPair() throws Exception {
|
||||
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
||||
generator.initialize(1024);
|
||||
return generator.generateKeyPair();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取私钥
|
||||
*
|
||||
* @param privateKey 私钥字符串
|
||||
* @return
|
||||
*/
|
||||
public static PrivateKey getPrivateKey(String privateKey) throws Exception {
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
byte[] decodedKey = Base64.decodeBase64(privateKey.getBytes());
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);
|
||||
return keyFactory.generatePrivate(keySpec);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公钥
|
||||
*
|
||||
* @param publicKey 公钥字符串
|
||||
* @return
|
||||
*/
|
||||
public static PublicKey getPublicKey(String publicKey) throws Exception {
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
byte[] decodedKey = Base64.decodeBase64(publicKey.getBytes());
|
||||
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
|
||||
return keyFactory.generatePublic(keySpec);
|
||||
}
|
||||
|
||||
/**
|
||||
* RSA加密
|
||||
*
|
||||
* @param data 待加密数据
|
||||
* @param publicKey 公钥
|
||||
* @return
|
||||
*/
|
||||
public static String encrypt(String data, PublicKey publicKey) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance("RSA");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||
int inputLen = data.getBytes().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.getBytes(), offset, MAX_ENCRYPT_BLOCK);
|
||||
} else {
|
||||
cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
|
||||
}
|
||||
out.write(cache, 0, cache.length);
|
||||
i++;
|
||||
offset = i * MAX_ENCRYPT_BLOCK;
|
||||
}
|
||||
byte[] encryptedData = out.toByteArray();
|
||||
out.close();
|
||||
// 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
|
||||
// 加密后的字符串
|
||||
return new String(Base64.encodeBase64String(encryptedData));
|
||||
}
|
||||
|
||||
/**
|
||||
* RSA解密
|
||||
*
|
||||
* @param data 待解密数据
|
||||
* @param privateKey 私钥
|
||||
* @return
|
||||
*/
|
||||
public static String decrypt(String data, PrivateKey privateKey) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance("RSA");
|
||||
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||
byte[] dataBytes = Base64.decodeBase64(data);
|
||||
int inputLen = dataBytes.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(dataBytes, offset, MAX_DECRYPT_BLOCK);
|
||||
} else {
|
||||
cache = cipher.doFinal(dataBytes, 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");
|
||||
}
|
||||
|
||||
/**
|
||||
* 签名
|
||||
*
|
||||
* @param data 待签名数据
|
||||
* @param privateKey 私钥
|
||||
* @return 签名
|
||||
*/
|
||||
public static String sign(String data, PrivateKey privateKey) throws Exception {
|
||||
byte[] keyBytes = privateKey.getEncoded();
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
PrivateKey key = keyFactory.generatePrivate(keySpec);
|
||||
Signature signature = Signature.getInstance("MD5withRSA");
|
||||
signature.initSign(key);
|
||||
signature.update(data.getBytes());
|
||||
return new String(Base64.encodeBase64(signature.sign()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 验签
|
||||
*
|
||||
* @param srcData 原始字符串
|
||||
* @param publicKey 公钥
|
||||
* @param sign 签名
|
||||
* @return 是否验签通过
|
||||
*/
|
||||
public static boolean verify(String srcData, PublicKey publicKey, String sign) throws Exception {
|
||||
byte[] keyBytes = publicKey.getEncoded();
|
||||
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
PublicKey key = keyFactory.generatePublic(keySpec);
|
||||
Signature signature = Signature.getInstance("MD5withRSA");
|
||||
signature.initVerify(key);
|
||||
signature.update(srcData.getBytes());
|
||||
return signature.verify(Base64.decodeBase64(sign.getBytes()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密
|
||||
* @param value
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
public static String encrypt(String value) throws Exception {
|
||||
// 加密
|
||||
PBEKeySpec pbeKeySpec = new PBEKeySpec(pass.toCharArray());
|
||||
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES");
|
||||
Key key = factory.generateSecret(pbeKeySpec);
|
||||
PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt.getBytes(),100);
|
||||
Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, key, pbeParameterSpec);
|
||||
byte[] result = cipher.doFinal(value.getBytes());
|
||||
System.out.println("jdk PBE encrypt: " + Base64.encodeBase64String(result));
|
||||
return Base64.encodeBase64String(result);
|
||||
public static String encrypt(String data)
|
||||
{
|
||||
try
|
||||
{
|
||||
String publicKey= StringUtils.isNotEmpty(USER_PUBLIC_KEY)?USER_PUBLIC_KEY:DEFAULT_PUBLIC_KEY;
|
||||
String encryptData = encrypt(data, getPublicKey(publicKey));
|
||||
return encryptData;
|
||||
}catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
* @param value
|
||||
* @param encryptData
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String decrypt(String value)throws Exception
|
||||
public static String decrypt(String encryptData)
|
||||
{
|
||||
// 解密
|
||||
PBEKeySpec pbeKeySpec = new PBEKeySpec(pass.toCharArray());
|
||||
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES");
|
||||
PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt.getBytes(),100);
|
||||
Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
|
||||
Key key = factory.generateSecret(pbeKeySpec);
|
||||
cipher.init(Cipher.DECRYPT_MODE, key,pbeParameterSpec);
|
||||
byte[] result = cipher.doFinal(Base64.decodeBase64(value));
|
||||
return new String(result);
|
||||
try
|
||||
{
|
||||
String privateKey= StringUtils.isNotEmpty(USER_PRIVATE_KEY)?USER_PRIVATE_KEY:DEFAULT_PRIVATE_KEY;
|
||||
String data = decrypt(encryptData, getPrivateKey(privateKey));
|
||||
return data;
|
||||
}catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return encryptData;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// 生成密钥对
|
||||
KeyPair keyPair = getKeyPair();
|
||||
String privateKey = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()));
|
||||
String publicKey = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded()));
|
||||
System.out.println("私钥:" + privateKey);
|
||||
System.out.println("公钥:" + publicKey);
|
||||
// RSA加密
|
||||
String data = "待加密的文字内容";
|
||||
String encryptData = encrypt(data, getPublicKey(publicKey));
|
||||
System.out.println("加密后内容:" + encryptData);
|
||||
// RSA解密
|
||||
String decryptData = decrypt(encryptData, getPrivateKey(privateKey));
|
||||
System.out.println("解密后内容:" + decryptData);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.print("加解密异常");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue