Commit 459ae79b by leejoker

增加RSA PKCS1解密支持,调整原有加密揭秘算法

1 parent a3958a65
......@@ -149,8 +149,6 @@
</dependency>
<!--防止进入swagger页面报类型转换错误,排除2.9.2中的引用,手动增加1.5.21版本-->
<dependency>
<groupId>io.swagger</groupId>
......@@ -245,6 +243,23 @@
<artifactId>cos_api</artifactId>
<version>5.6.24</version>
</dependency>
<!-- 增加RSA PKCS1解密支持 -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.68</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpg-jdk15on</artifactId>
<version>1.68</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.68</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.linkwechat.common.utils.wecom;
import sun.security.util.DerInputStream;
import sun.security.util.DerValue;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Base64;
/**
......@@ -26,51 +24,42 @@ public class RSAUtil {
/**
* RSA pkcs1 2048bit 解密工具,
* 获取私钥PrivateKey
*
* @param privKeyPEM 2048bit pkcs1格式,base64编码后的RSA字符串
* @return PrivateKey,用于解密 decryptRSA
* @return PrivateKey, 用于解密 decryptRSA
* @throws IOException 异常
* @throws NoSuchAlgorithmException 异常
* @throws InvalidKeySpecException 异常
*/
public static PrivateKey getPrivateKey(String privKeyPEM) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
String privKeyPEMnew = privKeyPEM.replaceAll("\\r\\n", "")//看看保存到数据库的这个字符是不是有\r,如果没有,那就只需要替换掉"\\n"
.replaceAll("\n", "")
.replace("-----BEGIN RSA PRIVATE KEY-----", "")
.replace("-----END RSA PRIVATE KEY-----", "");
byte[] bytes = java.util.Base64.getDecoder().decode(privKeyPEMnew);
DerInputStream derReader = new DerInputStream(bytes);
DerValue[] seq = derReader.getSequence(0);
BigInteger modulus = seq[1].getBigInteger();
BigInteger publicExp = seq[2].getBigInteger();
BigInteger privateExp = seq[3].getBigInteger();
BigInteger prime1 = seq[4].getBigInteger();
BigInteger prime2 = seq[5].getBigInteger();
BigInteger exp1 = seq[6].getBigInteger();
BigInteger exp2 = seq[7].getBigInteger();
BigInteger crtCoef = seq[8].getBigInteger();
RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
public static PrivateKey getPrivateKey(String privKeyPEM) throws IOException {
PrivateKey privateKey = null;
Reader privateKeyReader = new StringReader(privKeyPEM);
PEMParser privatePemParser = new PEMParser(privateKeyReader);
Object privateObject = privatePemParser.readObject();
if (privateObject instanceof PEMKeyPair) {
PEMKeyPair pemKeyPair = (PEMKeyPair) privateObject;
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
privateKey = converter.getPrivateKey(pemKeyPair.getPrivateKeyInfo());
}
return privateKey;
}
/**
* RSA pkcs1 2048bit 解密工具,
* @param str 被解密的字符串
*
* @param str 被解密的字符串
* @param privateKey 私钥对象 从 getPrivateKey 获取
* @return 解密后数据
* @throws NoSuchPaddingException 异常
* @throws NoSuchAlgorithmException 异常
* @throws InvalidKeyException 异常
* @throws BadPaddingException 异常
* @throws NoSuchPaddingException 异常
* @throws NoSuchAlgorithmException 异常
* @throws InvalidKeyException 异常
* @throws BadPaddingException 异常
* @throws IllegalBlockSizeException 异常
*/
public static String decryptRSA(String str, PrivateKey privateKey) throws NoSuchPaddingException,
NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] randomkeybyte = Base64.getDecoder().decode(str);
byte[] finalrandomkeybyte = cipher.doFinal(randomkeybyte);
return new String(finalrandomkeybyte);
public static String decryptRSA(String str, PrivateKey privateKey) throws NoSuchPaddingException,
NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchProviderException {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
rsa.init(Cipher.DECRYPT_MODE, privateKey);
byte[] utf8 = rsa.doFinal(Base64.getDecoder().decode(str));
return new String(utf8, StandardCharsets.UTF_8);
}
}
......@@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.linkwechat.common.config.CosConfig;
import com.linkwechat.common.config.RuoYiConfig;
import com.linkwechat.common.constant.Constants;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.common.utils.DateUtils;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!