|
@@ -1,7 +1,8 @@
|
1
|
1
|
package com.lqkj.link.util;
|
2
|
2
|
|
|
3
|
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
4
|
+
|
3
|
5
|
import javax.crypto.Cipher;
|
4
|
|
-import javax.crypto.NoSuchPaddingException;
|
5
|
6
|
import java.nio.charset.StandardCharsets;
|
6
|
7
|
import java.security.*;
|
7
|
8
|
import java.security.spec.InvalidKeySpecException;
|
|
@@ -17,15 +18,9 @@ import java.util.Base64;
|
17
|
18
|
**/
|
18
|
19
|
public class RSAUtils {
|
19
|
20
|
//KeyPair is a simple holder for a key pair.
|
|
21
|
+ private static BouncyCastleProvider provider = new BouncyCastleProvider();
|
20
|
22
|
private static final KeyPair keyPair = initKey();
|
21
|
23
|
|
22
|
|
- private static Cipher cipher = null;
|
23
|
|
-
|
24
|
|
- public static synchronized void getInstance(Provider provider) throws NoSuchPaddingException, NoSuchAlgorithmException {
|
25
|
|
- if (cipher == null) {
|
26
|
|
- cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
|
27
|
|
- }
|
28
|
|
- }
|
29
|
24
|
|
30
|
25
|
|
31
|
26
|
/**
|
|
@@ -36,7 +31,6 @@ public class RSAUtils {
|
36
|
31
|
private static KeyPair initKey() {
|
37
|
32
|
try {
|
38
|
33
|
//添加provider
|
39
|
|
- Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
|
40
|
34
|
Security.addProvider(provider);
|
41
|
35
|
//产生用于安全加密的随机数
|
42
|
36
|
SecureRandom random = new SecureRandom();
|
|
@@ -81,9 +75,8 @@ public class RSAUtils {
|
81
|
75
|
|
82
|
76
|
private static byte[] encrypt(byte[] byteArray){
|
83
|
77
|
try {
|
84
|
|
- Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
|
85
|
78
|
Security.addProvider(provider);
|
86
|
|
- getInstance(provider);
|
|
79
|
+ Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
|
87
|
80
|
PublicKey publicKey = keyPair.getPublic();
|
88
|
81
|
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
89
|
82
|
return cipher.doFinal(byteArray);
|
|
@@ -94,11 +87,10 @@ public class RSAUtils {
|
94
|
87
|
|
95
|
88
|
private static byte[] decrypt(byte[] byteArray) {
|
96
|
89
|
try {
|
|
90
|
+ Security.addProvider(provider);
|
97
|
91
|
//Cipher: 提供加密和解密功能的实例
|
98
|
92
|
//transformation: "algorithm/mode/padding"
|
99
|
|
- Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
|
100
|
|
- Security.addProvider(provider);
|
101
|
|
- getInstance(provider);
|
|
93
|
+ Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
|
102
|
94
|
PrivateKey privateKey = keyPair.getPrivate();
|
103
|
95
|
//初始化
|
104
|
96
|
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
|
@@ -111,9 +103,8 @@ public class RSAUtils {
|
111
|
103
|
|
112
|
104
|
private static byte[] encrypt(byte[] byteArray, String publicKey){
|
113
|
105
|
try {
|
114
|
|
- Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
|
115
|
106
|
Security.addProvider(provider);
|
116
|
|
- getInstance(provider);
|
|
107
|
+ Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
|
117
|
108
|
cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKey));
|
118
|
109
|
return cipher.doFinal(byteArray);
|
119
|
110
|
} catch (Exception e) {
|