Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
IJPay
提交
11566625
I
IJPay
项目概览
int
/
IJPay
大约 1 年 前同步成功
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
I
IJPay
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
11566625
编写于
2月 29, 2020
作者:
J
Javen205
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
⚡
优化代码、添加注释
上级
bb71c2c6
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
56 addition
and
56 deletion
+56
-56
IJPay-Core/src/main/java/com/ijpay/core/kit/RsaKit.java
IJPay-Core/src/main/java/com/ijpay/core/kit/RsaKit.java
+56
-56
未找到文件。
IJPay-Core/src/main/java/com/ijpay/core/kit/RsaKit.java
浏览文件 @
11566625
...
@@ -5,16 +5,14 @@ import cn.hutool.core.util.StrUtil;
...
@@ -5,16 +5,14 @@ import cn.hutool.core.util.StrUtil;
import
javax.crypto.Cipher
;
import
javax.crypto.Cipher
;
import
java.io.ByteArrayOutputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.math.BigInteger
;
import
java.math.BigInteger
;
import
java.nio.charset.StandardCharsets
;
import
java.security.*
;
import
java.security.*
;
import
java.security.interfaces.RSAPrivateKey
;
import
java.security.interfaces.RSAPrivateKey
;
import
java.security.interfaces.RSAPublicKey
;
import
java.security.interfaces.RSAPublicKey
;
import
java.security.spec.*
;
import
java.security.spec.*
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Properties
;
/**
/**
* <p>IJPay 让支付触手可及,封装了微信支付、支付宝支付、银联支付常用的支付方式以及各种常用的接口。</p>
* <p>IJPay 让支付触手可及,封装了微信支付、支付宝支付、银联支付常用的支付方式以及各种常用的接口。</p>
...
@@ -49,7 +47,7 @@ public class RsaKit {
...
@@ -49,7 +47,7 @@ public class RsaKit {
/**
/**
* 生成公钥和私钥
* 生成公钥和私钥
*
*
* @throws Exception
* @throws Exception
异常信息
*/
*/
public
static
Map
<
String
,
String
>
getKeys
()
throws
Exception
{
public
static
Map
<
String
,
String
>
getKeys
()
throws
Exception
{
KeyPairGenerator
keyPairGen
=
KeyPairGenerator
.
getInstance
(
KEY_ALGORITHM
);
KeyPairGenerator
keyPairGen
=
KeyPairGenerator
.
getInstance
(
KEY_ALGORITHM
);
...
@@ -77,7 +75,7 @@ public class RsaKit {
...
@@ -77,7 +75,7 @@ public class RsaKit {
*
*
* @param modulus 模
* @param modulus 模
* @param exponent 公钥指数
* @param exponent 公钥指数
* @return
* @return
{@link RSAPublicKey}
*/
*/
public
static
RSAPublicKey
getPublicKey
(
String
modulus
,
String
exponent
)
{
public
static
RSAPublicKey
getPublicKey
(
String
modulus
,
String
exponent
)
{
try
{
try
{
...
@@ -99,7 +97,7 @@ public class RsaKit {
...
@@ -99,7 +97,7 @@ public class RsaKit {
*
*
* @param modulus 模
* @param modulus 模
* @param exponent 指数
* @param exponent 指数
* @return
* @return
{@link RSAPrivateKey}
*/
*/
public
static
RSAPrivateKey
getPrivateKey
(
String
modulus
,
String
exponent
)
{
public
static
RSAPrivateKey
getPrivateKey
(
String
modulus
,
String
exponent
)
{
try
{
try
{
...
@@ -120,12 +118,20 @@ public class RsaKit {
...
@@ -120,12 +118,20 @@ public class RsaKit {
* @param data 需要加密的数据
* @param data 需要加密的数据
* @param publicKey 公钥
* @param publicKey 公钥
* @return 加密后的数据
* @return 加密后的数据
* @throws Exception
* @throws Exception
异常信息
*/
*/
public
static
String
encryptByPublicKey
(
String
data
,
String
publicKey
)
throws
Exception
{
public
static
String
encryptByPublicKey
(
String
data
,
String
publicKey
)
throws
Exception
{
return
encryptByPublicKey
(
data
,
publicKey
,
"RSA/ECB/PKCS1Padding"
);
return
encryptByPublicKey
(
data
,
publicKey
,
"RSA/ECB/PKCS1Padding"
);
}
}
/**
* 公钥加密
*
* @param data 需要加密的数据
* @param publicKey 公钥
* @return 加密后的数据
* @throws Exception 异常信息
*/
public
static
String
encryptByPublicKeyByWx
(
String
data
,
String
publicKey
)
throws
Exception
{
public
static
String
encryptByPublicKeyByWx
(
String
data
,
String
publicKey
)
throws
Exception
{
return
encryptByPublicKey
(
data
,
publicKey
,
"RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING"
);
return
encryptByPublicKey
(
data
,
publicKey
,
"RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING"
);
}
}
...
@@ -137,10 +143,10 @@ public class RsaKit {
...
@@ -137,10 +143,10 @@ public class RsaKit {
* @param publicKey 公钥
* @param publicKey 公钥
* @param fillMode 填充模式
* @param fillMode 填充模式
* @return 加密后的数据
* @return 加密后的数据
* @throws Exception
* @throws Exception
异常信息
*/
*/
public
static
String
encryptByPublicKey
(
String
data
,
String
publicKey
,
String
fillMode
)
throws
Exception
{
public
static
String
encryptByPublicKey
(
String
data
,
String
publicKey
,
String
fillMode
)
throws
Exception
{
byte
[]
dataByte
=
data
.
getBytes
(
"UTF-8"
);
byte
[]
dataByte
=
data
.
getBytes
(
StandardCharsets
.
UTF_8
);
byte
[]
keyBytes
=
Base64
.
decode
(
publicKey
);
byte
[]
keyBytes
=
Base64
.
decode
(
publicKey
);
X509EncodedKeySpec
x509KeySpec
=
new
X509EncodedKeySpec
(
keyBytes
);
X509EncodedKeySpec
x509KeySpec
=
new
X509EncodedKeySpec
(
keyBytes
);
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
KEY_ALGORITHM
);
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
KEY_ALGORITHM
);
...
@@ -175,7 +181,7 @@ public class RsaKit {
...
@@ -175,7 +181,7 @@ public class RsaKit {
* @param data 需要加密的数据
* @param data 需要加密的数据
* @param privateKey 私钥
* @param privateKey 私钥
* @return 加密后的数据
* @return 加密后的数据
* @throws Exception
* @throws Exception
异常信息
*/
*/
public
static
String
encryptByPrivateKey
(
String
data
,
String
privateKey
)
throws
Exception
{
public
static
String
encryptByPrivateKey
(
String
data
,
String
privateKey
)
throws
Exception
{
PKCS8EncodedKeySpec
priPkcs8
=
new
PKCS8EncodedKeySpec
(
Base64
.
decode
(
privateKey
));
PKCS8EncodedKeySpec
priPkcs8
=
new
PKCS8EncodedKeySpec
(
Base64
.
decode
(
privateKey
));
...
@@ -184,7 +190,7 @@ public class RsaKit {
...
@@ -184,7 +190,7 @@ public class RsaKit {
java
.
security
.
Signature
signature
=
java
.
security
.
Signature
.
getInstance
(
"SHA256WithRSA"
);
java
.
security
.
Signature
signature
=
java
.
security
.
Signature
.
getInstance
(
"SHA256WithRSA"
);
signature
.
initSign
(
priKey
);
signature
.
initSign
(
priKey
);
signature
.
update
(
data
.
getBytes
(
"UTF-8"
));
signature
.
update
(
data
.
getBytes
(
StandardCharsets
.
UTF_8
));
byte
[]
signed
=
signature
.
sign
();
byte
[]
signed
=
signature
.
sign
();
return
StrUtil
.
str
(
Base64
.
encode
(
signed
));
return
StrUtil
.
str
(
Base64
.
encode
(
signed
));
}
}
...
@@ -196,7 +202,7 @@ public class RsaKit {
...
@@ -196,7 +202,7 @@ public class RsaKit {
* @param sign 签名
* @param sign 签名
* @param publicKey 公钥
* @param publicKey 公钥
* @return 验证结果
* @return 验证结果
* @throws Exception
* @throws Exception
异常信息
*/
*/
public
static
boolean
checkByPublicKey
(
String
data
,
String
sign
,
String
publicKey
)
throws
Exception
{
public
static
boolean
checkByPublicKey
(
String
data
,
String
sign
,
String
publicKey
)
throws
Exception
{
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
KEY_ALGORITHM
);
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
KEY_ALGORITHM
);
...
@@ -204,22 +210,46 @@ public class RsaKit {
...
@@ -204,22 +210,46 @@ public class RsaKit {
PublicKey
pubKey
=
keyFactory
.
generatePublic
(
new
X509EncodedKeySpec
(
encodedKey
));
PublicKey
pubKey
=
keyFactory
.
generatePublic
(
new
X509EncodedKeySpec
(
encodedKey
));
java
.
security
.
Signature
signature
=
java
.
security
.
Signature
.
getInstance
(
"SHA256WithRSA"
);
java
.
security
.
Signature
signature
=
java
.
security
.
Signature
.
getInstance
(
"SHA256WithRSA"
);
signature
.
initVerify
(
pubKey
);
signature
.
initVerify
(
pubKey
);
signature
.
update
(
data
.
getBytes
(
"UTF-8"
));
signature
.
update
(
data
.
getBytes
(
StandardCharsets
.
UTF_8
));
return
signature
.
verify
(
Base64
.
decode
(
sign
.
getBytes
(
"UTF-8"
)));
return
signature
.
verify
(
Base64
.
decode
(
sign
.
getBytes
(
StandardCharsets
.
UTF_8
)));
}
/**
* 公钥验证签名
*
* @param data 需要加密的数据
* @param sign 签名
* @param publicKey 公钥
* @return 验证结果
* @throws Exception 异常信息
*/
public
static
boolean
checkByPublicKey
(
String
data
,
String
sign
,
PublicKey
publicKey
)
throws
Exception
{
java
.
security
.
Signature
signature
=
java
.
security
.
Signature
.
getInstance
(
"SHA256WithRSA"
);
signature
.
initVerify
(
publicKey
);
signature
.
update
(
data
.
getBytes
(
StandardCharsets
.
UTF_8
));
return
signature
.
verify
(
Base64
.
decode
(
sign
.
getBytes
(
StandardCharsets
.
UTF_8
)));
}
}
/**
/**
* 私钥解密
* 私钥解密
*
*
* @param data
* @param data
需要解密的数据
* @param privateKey
* @param privateKey
私钥
* @return
* @return
解密后的数据
* @throws Exception
* @throws Exception
异常信息
*/
*/
public
static
String
decryptByPrivateKey
(
String
data
,
String
privateKey
)
throws
Exception
{
public
static
String
decryptByPrivateKey
(
String
data
,
String
privateKey
)
throws
Exception
{
return
decryptByPrivateKey
(
data
,
privateKey
,
"RSA/ECB/PKCS1Padding"
);
return
decryptByPrivateKey
(
data
,
privateKey
,
"RSA/ECB/PKCS1Padding"
);
}
}
/**
* 私钥解密
*
* @param data 需要解密的数据
* @param privateKey 私钥
* @return 解密后的数据
* @throws Exception 异常信息
*/
public
static
String
decryptByPrivateKeyByWx
(
String
data
,
String
privateKey
)
throws
Exception
{
public
static
String
decryptByPrivateKeyByWx
(
String
data
,
String
privateKey
)
throws
Exception
{
return
decryptByPrivateKey
(
data
,
privateKey
,
"RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING"
);
return
decryptByPrivateKey
(
data
,
privateKey
,
"RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING"
);
}
}
...
@@ -227,11 +257,11 @@ public class RsaKit {
...
@@ -227,11 +257,11 @@ public class RsaKit {
/**
/**
* 私钥解密
* 私钥解密
*
*
* @param data
* @param data
需要解密的数据
* @param privateKey
* @param privateKey
私钥
* @param fillMode 填充模式
* @param fillMode 填充模式
* @return
* @return
解密后的数据
* @throws Exception
* @throws Exception
异常信息
*/
*/
public
static
String
decryptByPrivateKey
(
String
data
,
String
privateKey
,
String
fillMode
)
throws
Exception
{
public
static
String
decryptByPrivateKey
(
String
data
,
String
privateKey
,
String
fillMode
)
throws
Exception
{
byte
[]
encryptedData
=
Base64
.
decode
(
data
);
byte
[]
encryptedData
=
Base64
.
decode
(
data
);
...
@@ -263,41 +293,11 @@ public class RsaKit {
...
@@ -263,41 +293,11 @@ public class RsaKit {
return
new
String
(
decryptedData
);
return
new
String
(
decryptedData
);
}
}
/**
* 获取模数和密钥
*
* @return
*/
public
static
Map
<
String
,
String
>
getModulusAndKeys
()
{
Map
<
String
,
String
>
map
=
new
HashMap
<
String
,
String
>(
3
);
try
{
InputStream
in
=
RsaKit
.
class
.
getResourceAsStream
(
"/rsa.properties"
);
Properties
prop
=
new
Properties
();
prop
.
load
(
in
);
String
modulus
=
prop
.
getProperty
(
"modulus"
);
String
publicKey
=
prop
.
getProperty
(
"publicKey"
);
String
privateKey
=
prop
.
getProperty
(
"privateKey"
);
in
.
close
();
map
.
put
(
"modulus"
,
modulus
);
map
.
put
(
"publicKey"
,
publicKey
);
map
.
put
(
"privateKey"
,
privateKey
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
map
;
}
/**
/**
* 从字符串中加载公钥
* 从字符串中加载公钥
*
*
* @param publicKeyStr 公钥数据字符串
* @param publicKeyStr 公钥数据字符串
* @throws Exception
加载公钥时产生的异常
* @throws Exception
异常信息
*/
*/
public
static
PublicKey
loadPublicKey
(
String
publicKeyStr
)
throws
Exception
{
public
static
PublicKey
loadPublicKey
(
String
publicKeyStr
)
throws
Exception
{
try
{
try
{
...
@@ -318,9 +318,9 @@ public class RsaKit {
...
@@ -318,9 +318,9 @@ public class RsaKit {
* 从字符串中加载私钥<br>
* 从字符串中加载私钥<br>
* 加载时使用的是PKCS8EncodedKeySpec(PKCS#8编码的Key指令)。
* 加载时使用的是PKCS8EncodedKeySpec(PKCS#8编码的Key指令)。
*
*
* @param privateKeyStr
* @param privateKeyStr
私钥
* @return
* @return
{@link PrivateKey}
* @throws Exception
* @throws Exception
异常信息
*/
*/
public
static
PrivateKey
loadPrivateKey
(
String
privateKeyStr
)
throws
Exception
{
public
static
PrivateKey
loadPrivateKey
(
String
privateKeyStr
)
throws
Exception
{
try
{
try
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录