This commit is contained in:
Carol 2021-09-01 13:30:04 +08:00
parent d212a43a62
commit 5b48828c77
8 changed files with 107 additions and 124 deletions

View File

@ -1,6 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="spring" />
<option name="name" value="spring" />
<option name="url" value="http://10.1.15.172:8080/nexus/content/groups/public" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
@ -16,6 +21,16 @@
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="central" />
<option name="url" value="http://10.1.15.172:8080/nexus/content/groups/public" />
</remote-repository>
<remote-repository>
<option name="id" value="apache" />
<option name="name" value="apache" />
<option name="url" value="http://10.1.15.172:8080/nexus/content/groups/public" />
</remote-repository>
<remote-repository>
<option name="id" value="LocalMirrorId" />
<option name="name" value="LocalMirrorId" />

View File

@ -12,8 +12,6 @@ i
.gitattributes,2\4\24139dae656713ba861751fb2c2ac38839349a7a
9
README.md,8/e/8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d
7
pom.xml,4/4/442292b8a7efeabbe4cc176709b833b1792140ec
k
;src/main/java/site/cnkj/common/config/AsyncExecutePool.java,4\1\416390ba57649e9f4e6365934e9a6827cca3ffed
p
@ -44,8 +42,6 @@ o
?src/main/java/site/cnkj/common/utils/data/MongoClientsInit.java,4/9/490c965ee8a02ce158895987485283bb4b666ab6
g
7src/main/java/site/cnkj/common/utils/date/DateUtil.java,1\5\157e0e9285b69e300624f35e79300c0ed6098936
q
Asrc/main/java/site/cnkj/common/utils/file/FileEncryptDecrypt.java,e\a\eaa75651d9d6785c2bd75843529d61a722450af5
g
7src/main/java/site/cnkj/common/utils/file/FileUtil.java,d\f\df021943680d67b4f49e98599f4bb0e86803c590
m
@ -57,8 +53,6 @@ e
`
0src/main/java/site/cnkj/common/utils/io/DES.java,6\4\644b718a1d63acca44c411c399624e16ce98e11c
g
7src/main/java/site/cnkj/common/utils/io/RSAEncrypt.java,8\2\82864735cd8af377e6ad061ae189505a1e3e9d7c
g
7src/main/java/site/cnkj/common/utils/io/StringUtil.java,a\4\a43b6e3feeba3336586237a758acfa97b791be5b
k
;src/main/java/site/cnkj/common/utils/logger/LoggerUtil.java,4\f\4ffd3a6de814df96de78c74e6c16f964b4fe9a03

View File

@ -4,21 +4,14 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="52b7c556-aac6-402c-b840-5f2564f1f5d7" name="Default" comment="change MongoClientInit and fix some bug.">
<change afterPath="$PROJECT_DIR$/.idea/CommonUtil.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/codeStyles/codeStyleConfig.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/intellij-javadocs-4.0.1.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/jarRepositories.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/compiler.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/compiler.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/encodings.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/sonarlint/issuestore/4/9/490c965ee8a02ce158895987485283bb4b666ab6" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/sonarlint/issuestore/4/9/490c965ee8a02ce158895987485283bb4b666ab6" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/sonarlint/issuestore/b/b/bb173f9557479135e0f1bcd9c6c5f7b4ae5f77e8" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/sonarlint/issuestore/c/1/c131e8dffaaace66f248f9071023b60be7b277a4" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/sonarlint/issuestore/c/1/c131e8dffaaace66f248f9071023b60be7b277a4" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/sonarlint/issuestore/e/a/eaaa4be2d68e0a2d603579b394cb7c0ee6bb6df5" beforeDir="false" />
<list default="true" id="8ab3ef3f-5475-495f-a0ee-8551c01ff741" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/jarRepositories.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/jarRepositories.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/sonarlint/issuestore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/sonarlint/issuestore/8/2/82864735cd8af377e6ad061ae189505a1e3e9d7c" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/sonarlint/issuestore/e/a/eaa75651d9d6785c2bd75843529d61a722450af5" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/sonarlint/issuestore/index.pb" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/sonarlint/issuestore/index.pb" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/vcs.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/site/cnkj/common/utils/io/RSAEncrypt.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/site/cnkj/common/utils/io/RSAEncrypt.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -93,13 +86,6 @@
<detection-done>true</detection-done>
<sorting>DEFINITION_ORDER</sorting>
</component>
<component name="MavenImportPreferences">
<option name="generalSettings">
<MavenGeneralSettings>
<option name="alwaysUpdateSnapshots" value="true" />
</MavenGeneralSettings>
</option>
</component>
<component name="MavenProjectNavigator">
<treeState>
<expand>
@ -388,7 +374,7 @@
<property name="SONARLINT_PRECOMMIT_ANALYSIS" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/../../csgl/hettrmsservice" />
<property name="node.js.detected.package.eslint" value="true" />
<property name="node.js.detected.package.standard" value="true" />
<property name="node.js.path.for.package.eslint" value="project" />

8
CommonUtil.iml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="CheckStyle-IDEA-Module">
<option name="configuration">
<map />
</option>
</component>
</module>

View File

@ -1,7 +1,7 @@
package site.cnkj.common.utils.io;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import sun.misc.BASE64Decoder;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
@ -18,15 +18,19 @@ import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
* RSA非对称数据加解密
* @version 1.0 created by LXW on 2019/4/15 15:31
*/
public class RSAEncrypt {
private static final String DEFAULT_PUBLIC_KEY=
public static final String DEFAULT_PUBLIC_KEY=
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQChDzcjw/rWgFwnxunbKp7/4e8w" + "\r" +
"/UmXx2jk6qEEn69t6N2R1i/LmcyDT1xr/T2AHGOiXNQ5V8W4iCaaeNawi7aJaRht" + "\r" +
"Vx1uOH/2U378fscEESEG8XDqll0GCfB1/TjKI2aitVSzXOtRs8kYgGU78f7VmDNg" + "\r" +
"XIlk3gdhnzh+uoEQywIDAQAB" + "\r";
private static final String DEFAULT_PRIVATE_KEY=
public static final String DEFAULT_PRIVATE_KEY=
"MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKEPNyPD+taAXCfG" + "\r" +
"6dsqnv/h7zD9SZfHaOTqoQSfr23o3ZHWL8uZzINPXGv9PYAcY6Jc1DlXxbiIJpp4" + "\r" +
"1rCLtolpGG1XHW44f/ZTfvx+xwQRIQbxcOqWXQYJ8HX9OMojZqK1VLNc61GzyRiA" + "\r" +
@ -42,6 +46,8 @@ public class RSAEncrypt {
"Fae7oAqkMAd3xCNY6ec180tAe57hZ6kS+SYLKwb4gGzYaCxc22vMtYksXHtUeamo" + "\r" +
"1NMLzI2ZfUoX" + "\r";
/**
* 私钥
*/
@ -85,170 +91,144 @@ public class RSAEncrypt {
e.printStackTrace();
}
if (keyPairGen != null){
keyPairGen.initialize(1024, new SecureRandom());
keyPairGen.initialize(2048, new SecureRandom());
KeyPair keyPair= keyPairGen.generateKeyPair();
this.privateKey= (RSAPrivateKey) keyPair.getPrivate();
this.publicKey= (RSAPublicKey) keyPair.getPublic();
}
}
private StringBuilder loadKey(InputStream in) throws IOException {
BufferedReader br= new BufferedReader(new InputStreamReader(in));
String readLine= null;
StringBuilder sb= new StringBuilder();
while((readLine= br.readLine())!=null){
if(readLine.charAt(0)!='-'){
sb.append(readLine);
sb.append('\r');
}
}
return sb;
}
/**
* 从文件中输入流中加载公钥
* @param in 公钥输入流
* @throws Exception 加载公钥时产生的异常
* @throws IOException e
* @throws NoSuchAlgorithmException e
* @throws InvalidKeySpecException e
*/
public void loadPublicKey(InputStream in) throws Exception{
try {
BufferedReader br= new BufferedReader(new InputStreamReader(in));
String readLine= null;
StringBuilder sb= new StringBuilder();
while((readLine= br.readLine())!=null){
if(readLine.charAt(0)=='-'){
continue;
}else{
sb.append(readLine);
sb.append('\r');
}
}
loadPublicKey(sb.toString());
} catch (IOException e) {
throw new Exception("公钥数据流读取错误");
} catch (NullPointerException e) {
throw new Exception("公钥输入流为空");
}
public void loadPublicKey(InputStream in) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
loadPublicKey(loadKey(in).toString());
}
/**
* 从字符串中加载公钥
* @param publicKeyStr 公钥数据字符串
* @throws Exception 加载公钥时产生的异常
* @throws NoSuchAlgorithmException e
* @throws InvalidKeySpecException e
*/
public void loadPublicKey(String publicKeyStr) throws Exception{
try {
BASE64Decoder base64Decoder= new BASE64Decoder();
byte[] buffer= base64Decoder.decodeBuffer(publicKeyStr);
KeyFactory keyFactory= KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec= new X509EncodedKeySpec(buffer);
this.publicKey= (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此算法");
} catch (InvalidKeySpecException e) {
throw new Exception("公钥非法");
} catch (IOException e) {
throw new Exception("公钥数据内容读取错误");
} catch (NullPointerException e) {
throw new Exception("公钥数据为空");
}
public void loadPublicKey(String publicKeyStr) throws NoSuchAlgorithmException, InvalidKeySpecException {
byte[] buffer= Base64.decodeBase64(publicKeyStr);
KeyFactory keyFactory= KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec= new X509EncodedKeySpec(buffer);
this.publicKey= (RSAPublicKey) keyFactory.generatePublic(keySpec);
}
/**
* 从文件中加载私钥
* @param in 输入流
* @return 是否成功
* @throws Exception
* @throws NoSuchAlgorithmException e
* @throws InvalidKeySpecException e
* @throws IOException e
*/
public void loadPrivateKey(InputStream in) throws Exception{
try {
BufferedReader br= new BufferedReader(new InputStreamReader(in));
String readLine= null;
StringBuilder sb= new StringBuilder();
while((readLine= br.readLine())!=null){
if(readLine.charAt(0)=='-'){
continue;
}else{
sb.append(readLine);
sb.append('\r');
}
}
loadPrivateKey(sb.toString());
} catch (IOException e) {
throw new Exception("私钥数据读取错误");
} catch (NullPointerException e) {
throw new Exception("私钥输入流为空");
}
public void loadPrivateKey(InputStream in) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException{
loadPrivateKey(loadKey(in).toString());
}
public void loadPrivateKey(String privateKeyStr) throws Exception{
public void loadPrivateKey(String privateKeyStr) throws NoSuchAlgorithmException, InvalidKeySpecException {
try {
BASE64Decoder base64Decoder= new BASE64Decoder();
byte[] buffer= base64Decoder.decodeBuffer(privateKeyStr);
byte[] buffer= Base64.decodeBase64(privateKeyStr);
PKCS8EncodedKeySpec keySpec= new PKCS8EncodedKeySpec(buffer);
KeyFactory keyFactory= KeyFactory.getInstance("RSA");
this.privateKey= (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此算法");
throw new NoSuchAlgorithmException("无此算法");
} catch (InvalidKeySpecException e) {
throw new Exception("私钥非法");
} catch (IOException e) {
throw new Exception("私钥数据内容读取错误");
throw new InvalidKeySpecException("私钥非法");
} catch (NullPointerException e) {
throw new Exception("私钥数据为空");
throw new NullPointerException("私钥数据为空");
}
}
/**
* 加密过程
* @param publicKey 公钥
* @param plainTextData 明文数据
* @return
* @throws Exception 加密过程中的异常信息
* @return 加密后字节
* @throws NoSuchAlgorithmException e
* @throws InvalidKeyException e
* @throws IllegalBlockSizeException e
* @throws BadPaddingException e
* @throws NoSuchPaddingException e
*/
public byte[] encrypt(RSAPublicKey publicKey, byte[] plainTextData) throws Exception{
public byte[] encrypt(RSAPublicKey publicKey, byte[] plainTextData) throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException {
if(publicKey== null){
throw new Exception("加密公钥为空, 请设置");
throw new NullPointerException("加密公钥为空, 请设置");
}
Cipher cipher= null;
try {
cipher= Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding", new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] output= cipher.doFinal(plainTextData);
return output;
return cipher.doFinal(plainTextData);
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此加密算法");
} catch (NoSuchPaddingException e) {
e.printStackTrace();
return null;
throw new NoSuchAlgorithmException("无此加密算法");
} catch (InvalidKeyException e) {
throw new Exception("加密公钥非法,请检查");
throw new InvalidKeyException("加密公钥非法,请检查");
} catch (IllegalBlockSizeException e) {
throw new Exception("明文长度非法");
throw new IllegalBlockSizeException("明文长度非法");
} catch (BadPaddingException e) {
throw new Exception("明文数据已损坏");
} catch (Exception e){
e.printStackTrace();
return null;
throw new BadPaddingException("明文数据已损坏");
} catch (NoSuchPaddingException e) {
throw new NoSuchPaddingException("无此类填充");
}
}
/**
* 解密过程
* @param privateKey 私钥
* @param cipherData 密文数据
* @return 明文
* @throws Exception 解密过程中的异常信息
* @throws NoSuchAlgorithmException e
* @throws NoSuchPaddingException e
* @throws InvalidKeyException e
* @throws IllegalBlockSizeException e
* @throws BadPaddingException e
*/
public byte[] decrypt(RSAPrivateKey privateKey, byte[] cipherData) throws Exception{
public byte[] decrypt(RSAPrivateKey privateKey, byte[] cipherData) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
if (privateKey== null){
throw new Exception("解密私钥为空, 请设置");
throw new NullPointerException("解密私钥为空, 请设置");
}
Cipher cipher= null;
try {
cipher= Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding", new BouncyCastleProvider());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] output= cipher.doFinal(cipherData);
return output;
return cipher.doFinal(cipherData);
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此解密算法");
throw new NoSuchAlgorithmException("无此解密算法");
} catch (NoSuchPaddingException e) {
e.printStackTrace();
return null;
throw new NoSuchPaddingException("无此类填充");
}catch (InvalidKeyException e) {
throw new Exception("解密私钥非法,请检查");
throw new InvalidKeyException("解密私钥非法,请检查");
} catch (IllegalBlockSizeException e) {
throw new Exception("密文长度非法");
throw new IllegalBlockSizeException("密文长度非法");
} catch (BadPaddingException e) {
throw new Exception("密文数据已损坏");
throw new BadPaddingException("密文数据已损坏");
}
}