From a03f3515a4dee95bbcbb11dd45f6ab8671d2b61e Mon Sep 17 00:00:00 2001 From: seagull <24163551@qq.com> Date: Tue, 7 Jul 2020 20:28:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E9=85=8D=E7=BD=AE=E9=A1=B9?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8F=B73.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- pom.xml | 2 +- src/main/Resources/sys_config.properties | 2 +- .../luckyclient/utils/EncryptionUtils.java | 86 +++++++++---------- .../utils/proxy/PropertiesProxy.java | 15 ++-- 5 files changed, 52 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 343605a..31a15a8 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ code style - +

diff --git a/pom.xml b/pom.xml index f1d3cdd..e3dbbbf 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ LuckyFrameClient LuckyFrameClient - 3.2.1 + 3.3 jar LuckyFrameClient diff --git a/src/main/Resources/sys_config.properties b/src/main/Resources/sys_config.properties index a9c35a8..9dbcfb1 100644 --- a/src/main/Resources/sys_config.properties +++ b/src/main/Resources/sys_config.properties @@ -1,5 +1,5 @@ #===============================ϵͳ===================================== -client.verison=3.2.1 +client.verison=3.3 client.name=Կͻ server.web.ip=localhost server.web.port=80 diff --git a/src/main/java/luckyclient/utils/EncryptionUtils.java b/src/main/java/luckyclient/utils/EncryptionUtils.java index a6bb7f8..46fcb7c 100644 --- a/src/main/java/luckyclient/utils/EncryptionUtils.java +++ b/src/main/java/luckyclient/utils/EncryptionUtils.java @@ -17,32 +17,32 @@ import java.security.spec.X509EncodedKeySpec; public class EncryptionUtils { /** - * RSA最大加密明文大小 + * RSAĴС */ private static final int MAX_ENCRYPT_BLOCK = 117; /** - * RSA最大解密密文大小 + * RSAĴС */ private static final int MAX_DECRYPT_BLOCK = 128; /** - * 默认公钥 + * ĬϹԿ */ 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"); @@ -57,9 +57,9 @@ public class EncryptionUtils { } /** - * 获取密钥对 + * ȡԿ * - * @return 密钥对 + * @return Կ */ public static KeyPair getKeyPair() throws Exception { KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); @@ -68,9 +68,9 @@ public class EncryptionUtils { } /** - * 获取私钥 + * ȡ˽Կ * - * @param privateKey 私钥字符串 + * @param privateKey ˽Կַ * @return */ public static PrivateKey getPrivateKey(String privateKey) throws Exception { @@ -81,9 +81,9 @@ public class EncryptionUtils { } /** - * 获取公钥 + * ȡԿ * - * @param publicKey 公钥字符串 + * @param publicKey Կַ * @return */ public static PublicKey getPublicKey(String publicKey) throws Exception { @@ -94,10 +94,10 @@ public class EncryptionUtils { } /** - * RSA加密 + * RSA * - * @param data 待加密数据 - * @param publicKey 公钥 + * @param data + * @param publicKey Կ * @return */ public static String encrypt(String data, PublicKey publicKey) throws Exception { @@ -108,7 +108,7 @@ public class EncryptionUtils { 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); @@ -121,16 +121,16 @@ public class EncryptionUtils { } byte[] encryptedData = out.toByteArray(); out.close(); - // 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串 - // 加密后的字符串 + // ȡʹbase64б,UTF-8Ϊ׼תַ + // ַܺ return new String(Base64.encodeBase64String(encryptedData)); } /** - * RSA解密 + * RSA * - * @param data 待解密数据 - * @param privateKey 私钥 + * @param data + * @param privateKey ˽Կ * @return */ public static String decrypt(String data, PrivateKey privateKey) throws Exception { @@ -142,7 +142,7 @@ public class EncryptionUtils { 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); @@ -155,16 +155,16 @@ public class EncryptionUtils { } byte[] decryptedData = out.toByteArray(); out.close(); - // 解密后的内容 + // ܺ return new String(decryptedData, "UTF-8"); } /** - * 签名 + * ǩ * - * @param data 待签名数据 - * @param privateKey 私钥 - * @return 签名 + * @param data ǩ + * @param privateKey ˽Կ + * @return ǩ */ public static String sign(String data, PrivateKey privateKey) throws Exception { byte[] keyBytes = privateKey.getEncoded(); @@ -178,12 +178,12 @@ public class EncryptionUtils { } /** - * 验签 + * ǩ * - * @param srcData 原始字符串 - * @param publicKey 公钥 - * @param sign 签名 - * @return 是否验签通过 + * @param srcData ԭʼַ + * @param publicKey Կ + * @param sign ǩ + * @return Ƿǩͨ */ public static boolean verify(String srcData, PublicKey publicKey, String sign) throws Exception { byte[] keyBytes = publicKey.getEncoded(); @@ -197,7 +197,7 @@ public class EncryptionUtils { } /** - * 加密 + * * @param data * @return */ @@ -216,7 +216,7 @@ public class EncryptionUtils { } /** - * 解密 + * * @param encryptData * @return */ @@ -236,22 +236,22 @@ public class EncryptionUtils { 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 = "待加密的文字内容"; + System.out.println("˽Կ:" + privateKey); + System.out.println("Կ:" + publicKey); + // RSA + String data = "ܵ"; String encryptData = encrypt(data, getPublicKey(publicKey)); - System.out.println("加密后内容:" + encryptData); - // RSA解密 + System.out.println("ܺ:" + encryptData); + // RSA String decryptData = decrypt(encryptData, getPrivateKey(privateKey)); - System.out.println("解密后内容:" + decryptData); + System.out.println("ܺ:" + decryptData); } catch (Exception e) { e.printStackTrace(); - System.out.print("加解密异常"); + System.out.print("ӽ쳣"); } } } \ No newline at end of file diff --git a/src/main/java/luckyclient/utils/proxy/PropertiesProxy.java b/src/main/java/luckyclient/utils/proxy/PropertiesProxy.java index fe532e0..0455a18 100644 --- a/src/main/java/luckyclient/utils/proxy/PropertiesProxy.java +++ b/src/main/java/luckyclient/utils/proxy/PropertiesProxy.java @@ -3,10 +3,9 @@ package luckyclient.utils.proxy; import com.alibaba.fastjson.JSONObject; import luckyclient.netty.ClientHandler; import luckyclient.utils.EncryptionUtils; +import luckyclient.utils.LogUtil; import luckyclient.utils.httputils.HttpRequest; import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.Properties; @@ -16,8 +15,6 @@ import java.util.Properties; */ public class PropertiesProxy extends Properties { - private static final Logger log = LoggerFactory.getLogger(PropertiesProxy.class); - private Properties wapper = new Properties(); public Properties getWapper() { @@ -30,7 +27,7 @@ public class PropertiesProxy extends Properties { @Override public String getProperty(String key) { - log.info("get config from service"); + LogUtil.APP.info("get config from service"); //ȡ //ͨӿڻȡ try{ @@ -41,21 +38,21 @@ public class PropertiesProxy extends Properties { if(res.get("code")!=null&&res.getInteger("code")==200) { String value= EncryptionUtils.decrypt(res.get("value").toString()); - log.info("get config from server:"+res.toJSONString()+";value="+value); + LogUtil.APP.info("get config from server:"+res.toJSONString()+";value="+value); return value; } } }catch (Exception e) { - log.info("û:"+key); + LogUtil.APP.error("û:"+key); } return wapper.getProperty(key); } @Override public String getProperty(String key, String defaultValue) { - log.debug("get config from service"); + LogUtil.APP.info("get config from service"); //ȡ //ͨӿڻȡ try{ @@ -66,7 +63,7 @@ public class PropertiesProxy extends Properties { } }catch (Exception e) { - log.debug("û:"+key); + LogUtil.APP.error("û:"+key); } return wapper.getProperty(key, defaultValue); }