-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSAUtils.java
More file actions
190 lines (176 loc) · 6.82 KB
/
Copy pathRSAUtils.java
File metadata and controls
190 lines (176 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package httpTest;
import javax.crypto.Cipher;
import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
*
*
* @author yxpweb
*/
public class RSAUtils {
static byte[] publicKeyBytes;
static byte[] privateKeyBytes;
static {
try {
// publicKeyBytes = RSAKeys.decryptBASE64(CoreConfig.stringValue("SystemPublicKey"));
// privateKeyBytes = RSAKeys.decryptBASE64(CoreConfig.stringValue("SystemPrivateKey"));
/*publicKeyBytes = RSAKeys.decryptBASE64("test");
privateKeyBytes = RSAKeys.decryptBASE64("123456");*/
} catch (Exception e) {
e.printStackTrace();
}
}
// /**
// * 用私钥加密
// *
// * @param data
// * 加密数据
// * @return
// * @throws Exception
// */
// public static byte[] encryptByPrivateKey(byte[] data) throws Exception {
// // 取私钥
// PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
// KeyFactory keyFactory = KeyFactory.getInstance(RSAKeys.KEY_ALGORITHM);
// Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
// // 对数据加密
// Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
// cipher.init(Cipher.ENCRYPT_MODE, privateKey);
// return cipher.doFinal(data);
// }
// /**
// * 不支持长度大于117!
// * 用私钥解密
// *
// * @param data
// * 加密数据
// * @return 解密后的数据
// * @throws Exception
// */
// public static byte[] decryptByPrivateKey(byte[] data) throws Exception {
// PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
// KeyFactory keyFactory = KeyFactory.getInstance(RSAKeys.KEY_ALGORITHM);
// Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
// // 对数据解密
// Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
// cipher.init(Cipher.DECRYPT_MODE, privateKey);
// return cipher.doFinal(data);
// }
/**
* 用私钥解密,数据长度支持大于117
*
* @param data
* 加密数据
* @return 解密后的数据
* @throws Exception
*/
public static byte[] decryptByPrivateKey(byte[] data) throws Exception {
int dataLen = data.length;
short encryptLen = (short) (data[0] << 8 | data[1] & 0xFF);
int backLen = dataLen - 2 - encryptLen;
byte[] encryptFront = new byte[encryptLen];
System.arraycopy(data, 2, encryptFront, 0, encryptLen);
// System.out.println("encryptFront>>" + Arrays.toString(encryptFront));
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
//KeyFactory keyFactory = KeyFactory.getInstance(RSAKeys.KEY_ALGORITHM);
KeyFactory keyFactory = KeyFactory.getInstance("tsdatsd");
Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] front = cipher.doFinal(encryptFront);
// System.out.println("front>>" + Arrays.toString(front));
int frontLen = front.length;
byte[] out = new byte[frontLen + backLen];
System.arraycopy(front, 0, out, 0, frontLen);
if (backLen > 0) {
System.arraycopy(data, 2 + encryptLen, out, frontLen, backLen);
}
return out;
}
/**
* 用公钥加密,数据长度支持大于117
*
* @param data
* 加密数据
* @return 加密后的数据,结构: 加密部分长度(short) + 加密部分 + 未加密部分
* @throws Exception
*/
public static byte[] encryptByPublicKey(byte[] data) throws Exception {
int dataLen = data.length;
byte[] front = null;
int limitLen = 117;
if (dataLen > limitLen) {
front = new byte[limitLen];
System.arraycopy(data, 0, front, 0, limitLen);
} else {
front = data;
}
// System.out.println("front>" + Arrays.toString(front));
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKeyBytes);
//KeyFactory keyFactory = KeyFactory.getInstance(RSAKeys.KEY_ALGORITHM);
KeyFactory keyFactory = KeyFactory.getInstance("tsdatsd");
Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptFront = cipher.doFinal(front);
// System.out.println("encryptFront>" + Arrays.toString(encryptFront));
int encryptLen = encryptFront.length;
int backLen = dataLen - limitLen;
// System.out.println("dataLen=" + dataLen + " backLen=" + backLen);
byte[] out = backLen > 0 ? new byte[2 + encryptLen + backLen] : new byte[2 + encryptLen]; // 前面2字节是加密数据的长度
out[0] = (byte) (encryptLen >>> 8);
out[1] = (byte) (encryptLen);
System.arraycopy(encryptFront, 0, out, 2, encryptLen);
if (backLen > 0) {
System.arraycopy(data, limitLen, out, 2 + encryptLen, backLen);
}
return out;
}
public static void main(String[] args) throws Exception {
String s = "abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#abcd中文!$%#";
byte[] data = s.getBytes("utf-8");
System.out.println("data.length>" + data.length);
byte[] encrypt = encryptByPublicKey(data);
byte[] decrypt = decryptByPrivateKey(encrypt);
System.out.println(new String(decrypt, "utf-8"));
}
// /**
// * 不支持数据长度大于117!
// * 用公钥加密
// *
// * @param data
// * 加密数据
// * @return 加密后的数据
// * @throws Exception
// */
// public static byte[] encryptByPublicKey(byte[] data) throws Exception {
// X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKeyBytes);
// KeyFactory keyFactory = KeyFactory.getInstance(RSAKeys.KEY_ALGORITHM);
// Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
// // 对数据解密
// Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
// cipher.init(Cipher.ENCRYPT_MODE, publicKey);
// return cipher.doFinal(data);
// }
// /**
// * 用公钥解密
// *
// * @param data
// * 加密数据
// * @return
// * @throws Exception
// */
// public static byte[] decryptByPublicKey(byte[] data) throws Exception {
// X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKeyBytes);
// KeyFactory keyFactory = KeyFactory.getInstance(RSAKeys.KEY_ALGORITHM);
// Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
//
// // 对数据解密
// Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
// cipher.init(Cipher.DECRYPT_MODE, publicKey);
//
// return cipher.doFinal(data);
// }
}