Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
江名洋
/
linkwe-scrm
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 459ae79b
authored
Mar 01, 2021
by
leejoker
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
增加RSA PKCS1解密支持,调整原有加密揭秘算法
1 parent
a3958a65
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
40 deletions
linkwe-common/pom.xml
linkwe-common/src/main/java/com/linkwechat/common/utils/wecom/RSAUtil.java
linkwe-common/src/main/java/com/tencent/wework/FinanceUtils.java
linkwe-common/pom.xml
View file @
459ae79
...
@@ -149,8 +149,6 @@
...
@@ -149,8 +149,6 @@
</dependency>
</dependency>
<!--防止进入swagger页面报类型转换错误,排除2.9.2中的引用,手动增加1.5.21版本-->
<!--防止进入swagger页面报类型转换错误,排除2.9.2中的引用,手动增加1.5.21版本-->
<dependency>
<dependency>
<groupId>
io.swagger
</groupId>
<groupId>
io.swagger
</groupId>
...
@@ -245,6 +243,23 @@
...
@@ -245,6 +243,23 @@
<artifactId>
cos_api
</artifactId>
<artifactId>
cos_api
</artifactId>
<version>
5.6.24
</version>
<version>
5.6.24
</version>
</dependency>
</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>
</dependencies>
</project>
</project>
\ No newline at end of file
\ No newline at end of file
linkwe-common/src/main/java/com/linkwechat/common/utils/wecom/RSAUtil.java
View file @
459ae79
package
com
.
linkwechat
.
common
.
utils
.
wecom
;
package
com
.
linkwechat
.
common
.
utils
.
wecom
;
import
sun.security.util.DerInputStream
;
import
org.bouncycastle.openssl.PEMKeyPair
;
import
sun.security.util.DerValue
;
import
org.bouncycastle.openssl.PEMParser
;
import
org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter
;
import
javax.crypto.BadPaddingException
;
import
javax.crypto.BadPaddingException
;
import
javax.crypto.Cipher
;
import
javax.crypto.Cipher
;
import
javax.crypto.IllegalBlockSizeException
;
import
javax.crypto.IllegalBlockSizeException
;
import
javax.crypto.NoSuchPaddingException
;
import
javax.crypto.NoSuchPaddingException
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.math.BigInteger
;
import
java.io.Reader
;
import
java.security.InvalidKeyException
;
import
java.io.StringReader
;
import
java.security.KeyFactory
;
import
java.nio.charset.StandardCharsets
;
import
java.security.NoSuchAlgorithmException
;
import
java.security.*
;
import
java.security.PrivateKey
;
import
java.security.spec.InvalidKeySpecException
;
import
java.security.spec.RSAPrivateCrtKeySpec
;
import
java.util.Base64
;
import
java.util.Base64
;
/**
/**
...
@@ -26,36 +24,27 @@ public class RSAUtil {
...
@@ -26,36 +24,27 @@ public class RSAUtil {
/**
/**
* RSA pkcs1 2048bit 解密工具,
* RSA pkcs1 2048bit 解密工具,
* 获取私钥PrivateKey
* 获取私钥PrivateKey
*
* @param privKeyPEM 2048bit pkcs1格式,base64编码后的RSA字符串
* @param privKeyPEM 2048bit pkcs1格式,base64编码后的RSA字符串
* @return
PrivateKey,
用于解密 decryptRSA
* @return
PrivateKey,
用于解密 decryptRSA
* @throws IOException 异常
* @throws IOException 异常
* @throws NoSuchAlgorithmException 异常
* @throws InvalidKeySpecException 异常
*/
*/
public
static
PrivateKey
getPrivateKey
(
String
privKeyPEM
)
throws
IOException
,
NoSuchAlgorithmException
,
InvalidKeySpecException
{
public
static
PrivateKey
getPrivateKey
(
String
privKeyPEM
)
throws
IOException
{
String
privKeyPEMnew
=
privKeyPEM
.
replaceAll
(
"\\r\\n"
,
""
)
//看看保存到数据库的这个字符是不是有\r,如果没有,那就只需要替换掉"\\n"
PrivateKey
privateKey
=
null
;
.
replaceAll
(
"\n"
,
""
)
Reader
privateKeyReader
=
new
StringReader
(
privKeyPEM
);
.
replace
(
"-----BEGIN RSA PRIVATE KEY-----"
,
""
)
PEMParser
privatePemParser
=
new
PEMParser
(
privateKeyReader
);
.
replace
(
"-----END RSA PRIVATE KEY-----"
,
""
);
Object
privateObject
=
privatePemParser
.
readObject
();
byte
[]
bytes
=
java
.
util
.
Base64
.
getDecoder
().
decode
(
privKeyPEMnew
);
if
(
privateObject
instanceof
PEMKeyPair
)
{
DerInputStream
derReader
=
new
DerInputStream
(
bytes
);
PEMKeyPair
pemKeyPair
=
(
PEMKeyPair
)
privateObject
;
DerValue
[]
seq
=
derReader
.
getSequence
(
0
);
JcaPEMKeyConverter
converter
=
new
JcaPEMKeyConverter
().
setProvider
(
"BC"
);
BigInteger
modulus
=
seq
[
1
].
getBigInteger
();
privateKey
=
converter
.
getPrivateKey
(
pemKeyPair
.
getPrivateKeyInfo
());
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
);
return
privateKey
;
return
privateKey
;
}
}
/**
/**
* RSA pkcs1 2048bit 解密工具,
* RSA pkcs1 2048bit 解密工具,
*
* @param str 被解密的字符串
* @param str 被解密的字符串
* @param privateKey 私钥对象 从 getPrivateKey 获取
* @param privateKey 私钥对象 从 getPrivateKey 获取
* @return 解密后数据
* @return 解密后数据
...
@@ -66,11 +55,11 @@ public class RSAUtil {
...
@@ -66,11 +55,11 @@ public class RSAUtil {
* @throws IllegalBlockSizeException 异常
* @throws IllegalBlockSizeException 异常
*/
*/
public
static
String
decryptRSA
(
String
str
,
PrivateKey
privateKey
)
throws
NoSuchPaddingException
,
public
static
String
decryptRSA
(
String
str
,
PrivateKey
privateKey
)
throws
NoSuchPaddingException
,
NoSuchAlgorithmException
,
InvalidKeyException
,
BadPaddingException
,
IllegalBlockSizeException
{
NoSuchAlgorithmException
,
InvalidKeyException
,
BadPaddingException
,
IllegalBlockSizeException
,
NoSuchProviderException
{
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
Security
.
addProvider
(
new
org
.
bouncycastle
.
jce
.
provider
.
BouncyCastleProvider
()
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
privateKey
);
Cipher
rsa
=
Cipher
.
getInstance
(
"RSA/ECB/PKCS1Padding"
,
"BC"
);
byte
[]
randomkeybyte
=
Base64
.
getDecoder
().
decode
(
str
);
rsa
.
init
(
Cipher
.
DECRYPT_MODE
,
privateKey
);
byte
[]
finalrandomkeybyte
=
cipher
.
doFinal
(
randomkeybyte
);
byte
[]
utf8
=
rsa
.
doFinal
(
Base64
.
getDecoder
().
decode
(
str
)
);
return
new
String
(
finalrandomkeybyte
);
return
new
String
(
utf8
,
StandardCharsets
.
UTF_8
);
}
}
}
}
linkwe-common/src/main/java/com/tencent/wework/FinanceUtils.java
View file @
459ae79
...
@@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONArray;
...
@@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONArray;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.linkwechat.common.config.CosConfig
;
import
com.linkwechat.common.config.CosConfig
;
import
com.linkwechat.common.config.RuoYiConfig
;
import
com.linkwechat.common.config.RuoYiConfig
;
import
com.linkwechat.common.constant.Constants
;
import
com.linkwechat.common.constant.WeConstans
;
import
com.linkwechat.common.constant.WeConstans
;
import
com.linkwechat.common.core.redis.RedisCache
;
import
com.linkwechat.common.core.redis.RedisCache
;
import
com.linkwechat.common.utils.DateUtils
;
import
com.linkwechat.common.utils.DateUtils
;
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment