Skip to content

Commit f586c6f

Browse files
egzosnegzosn
authored andcommitted
转账订单设置属性信息
1 parent c12d4ab commit f586c6f

4 files changed

Lines changed: 54 additions & 35 deletions

File tree

pay-java-common/src/main/java/com/egzosn/pay/common/bean/TransferOrder.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/**
88
* 转账订单
9+
*
910
* @author egan
1011
* <pre>
1112
* email egzosn@gmail.com
@@ -27,12 +28,12 @@ public class TransferOrder implements Order {
2728
/**
2829
* 收款方账户, 用户openid,卡号等等
2930
*/
30-
private String payeeAccount ;
31+
private String payeeAccount;
3132

3233
/**
3334
* 转账金额
3435
*/
35-
private BigDecimal amount ;
36+
private BigDecimal amount;
3637

3738
/**
3839
* 付款人名称
@@ -55,11 +56,11 @@ public class TransferOrder implements Order {
5556

5657
/**
5758
* 收款开户行
58-
*/
59+
*/
5960
private Bank bank;
6061

6162
/**
62-
* 收款开户行地址
63+
* 收款开户行地址
6364
*/
6465
private String payeeBankAddress;
6566

@@ -198,9 +199,10 @@ public String getIp() {
198199
public void setIp(String ip) {
199200
this.ip = ip;
200201
}
202+
201203
@Override
202204
public Map<String, Object> getAttrs() {
203-
if (null == attr){
205+
if (null == attr) {
204206
attr = new HashMap<>();
205207
}
206208
return attr;
@@ -214,9 +216,11 @@ public Object getAttr(String key) {
214216

215217
/**
216218
* 添加订单信息
217-
* @param key key
219+
*
220+
* @param key key
218221
* @param value 值
219222
*/
223+
@Override
220224
public void addAttr(String key, Object value) {
221225
getAttrs().put(key, value);
222226
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package com.egzosn.pay.common.bean;
22

3+
import java.util.Map;
4+
35
/**
46
* 转账类型
57
* @author egan
68
* email egzosn@gmail.com
79
* date 2018/9/28.19:45
810
*/
911
public interface TransferType extends TransactionType{
10-
11-
12+
/**
13+
* 设置属性
14+
*
15+
* @param attr 已有属性对象
16+
* @param order 转账订单
17+
* @return 属性对象
18+
*/
19+
Map<String, Object> setAttr(Map<String, Object> attr, TransferOrder order);
1220
}

pay-java-common/src/main/java/com/egzosn/pay/common/util/sign/encrypt/RSA.java

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import javax.crypto.Cipher;
55
import java.io.*;
6+
import java.security.GeneralSecurityException;
67
import java.security.KeyFactory;
78
import java.security.PrivateKey;
89
import java.security.PublicKey;
@@ -34,9 +35,9 @@ public class RSA{
3435
*/
3536
public static String sign(String content, String privateKey, String signAlgorithms, String characterEncoding) {
3637
try {
37-
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec( Base64.decode(privateKey));
38-
KeyFactory keyf = KeyFactory.getInstance(ALGORITHM);
39-
PrivateKey priKey = keyf.generatePrivate(priPKCS8);
38+
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decode(privateKey));
39+
KeyFactory keyf = KeyFactory.getInstance(ALGORITHM);
40+
PrivateKey priKey = keyf.generatePrivate(priPKCS8);
4041

4142
java.security.Signature signature = java.security.Signature.getInstance(signAlgorithms);
4243

@@ -111,11 +112,11 @@ public static String sign(String content, PrivateKey privateKey ,String characte
111112
*/
112113
public static boolean verify(String content, String sign, String publicKey, String signAlgorithms, String characterEncoding){
113114
try {
114-
PublicKey pubKey = getPublicKey(publicKey, ALGORITHM);
115+
PublicKey pubKey = getPublicKey(publicKey, ALGORITHM);
115116
java.security.Signature signature = java.security.Signature.getInstance(signAlgorithms);
116117
signature.initVerify(pubKey);
117-
signature.update( content.getBytes(characterEncoding) );
118-
return signature.verify( Base64.decode(sign) );
118+
signature.update(content.getBytes(characterEncoding) );
119+
return signature.verify(Base64.decode(sign) );
119120
} catch (Exception e) {
120121
e.printStackTrace();
121122
}
@@ -135,8 +136,8 @@ public static boolean verify(String content, String sign, PublicKey publicKey, S
135136
try {
136137
java.security.Signature signature = java.security.Signature.getInstance(signAlgorithms);
137138
signature.initVerify(publicKey);
138-
signature.update( content.getBytes(characterEncoding) );
139-
return signature.verify( Base64.decode(sign) );
139+
signature.update(content.getBytes(characterEncoding) );
140+
return signature.verify(Base64.decode(sign) );
140141
} catch (Exception e) {
141142
e.printStackTrace();
142143
}
@@ -176,9 +177,9 @@ public static boolean verify(String content, String sign, PublicKey publicKey, S
176177
* @return 解密后的字符串
177178
* @throws Exception 解密异常
178179
*/
179-
public static String decrypt(String content, String privateKey, String characterEncoding) throws Exception {
180-
PrivateKey prikey = getPrivateKey(privateKey);
181-
Cipher cipher = Cipher.getInstance(ALGORITHM);
180+
public static String decrypt(String content, String privateKey, String characterEncoding) throws GeneralSecurityException, IOException {
181+
PrivateKey prikey = getPrivateKey(privateKey);
182+
Cipher cipher = Cipher.getInstance(ALGORITHM);
182183
cipher.init(Cipher.DECRYPT_MODE, prikey);
183184
try(InputStream ins = new ByteArrayInputStream(Base64.decode(content)); ByteArrayOutputStream writer = new ByteArrayOutputStream();) {
184185

@@ -204,14 +205,14 @@ public static String decrypt(String content, String privateKey, String character
204205
}
205206
}
206207

207-
208+
208209
/**
209210
* 得到私钥
210211
* @param key 密钥字符串(经过base64编码)
211-
* @throws Exception 加密异常
212+
* @throws GeneralSecurityException 加密异常
212213
* @return 私钥
213214
*/
214-
public static PrivateKey getPrivateKey(String key) throws Exception {
215+
public static PrivateKey getPrivateKey(String key) throws GeneralSecurityException {
215216

216217
byte[] keyBytes;
217218
keyBytes = Base64.decode(key);
@@ -225,26 +226,30 @@ public static PrivateKey getPrivateKey(String key) throws Exception {
225226
* 得到公钥
226227
* @param key 密钥字符串(经过base64编码)
227228
* @param signAlgorithms 密钥类型
228-
* @throws Exception 加密异常
229+
* @throws GeneralSecurityException 加密异常
230+
* @throws IOException 加密异常
229231
* @return 公钥
230232
*/
231-
public static PublicKey getPublicKey(String key, String signAlgorithms) throws Exception {
232-
return getPublicKey(new ByteArrayInputStream(key.getBytes("ISO8859-1")), signAlgorithms);
233+
public static PublicKey getPublicKey(String key, String signAlgorithms) throws GeneralSecurityException, IOException {
234+
try (ByteArrayInputStream is = new ByteArrayInputStream(key.getBytes("ISO8859-1"))){
235+
return getPublicKey(is, signAlgorithms);
236+
}
233237
}
234238

235239

236240
/**
237241
* 得到公钥
238242
* @param key 密钥字符串(经过base64编码)
239-
* @throws Exception 加密异常
243+
* @throws GeneralSecurityException 加密异常
244+
* @throws IOException 加密异常
240245
* @return 公钥
241246
*/
242-
public static PublicKey getPublicKey(String key) throws Exception {
247+
public static PublicKey getPublicKey(String key) throws GeneralSecurityException, IOException {
243248

244249
return getPublicKey(key, ALGORITHM);
245250
}
246251

247-
public static PublicKey getPublicKey(InputStream inputStream, String keyAlgorithm) throws Exception {
252+
public static PublicKey getPublicKey(InputStream inputStream, String keyAlgorithm) throws IOException, GeneralSecurityException {
248253
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));) {
249254
StringBuilder sb = new StringBuilder();
250255
String readLine = null;
@@ -262,14 +267,14 @@ public static PublicKey getPublicKey(InputStream inputStream, String keyAlgorith
262267
}
263268
}
264269

265-
public static byte[] encrypt(byte[] plainBytes, PublicKey publicKey, int keyLength, int reserveSize, String cipherAlgorithm) throws Exception {
270+
public static byte[] encrypt(byte[] plainBytes, PublicKey publicKey, int keyLength, int reserveSize, String cipherAlgorithm) throws IOException, GeneralSecurityException {
266271
int keyByteSize = keyLength / 8;
267272
int encryptBlockSize = keyByteSize - reserveSize;
268273
int nBlock = plainBytes.length / encryptBlockSize;
269274
if ((plainBytes.length % encryptBlockSize) != 0) {
270275
nBlock += 1;
271276
}
272-
try (ByteArrayOutputStream outbuf = new ByteArrayOutputStream(nBlock * keyByteSize);) {
277+
try (ByteArrayOutputStream outbuf = new ByteArrayOutputStream(nBlock * keyByteSize)) {
273278
Cipher cipher = Cipher.getInstance(cipherAlgorithm);
274279
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
275280
for (int offset = 0; offset < plainBytes.length; offset += encryptBlockSize) {
@@ -284,7 +289,7 @@ public static byte[] encrypt(byte[] plainBytes, PublicKey publicKey, int keyLeng
284289
return outbuf.toByteArray();
285290
}
286291
}
287-
public static String encrypt(String content, String publicKey, String cipherAlgorithm, String characterEncoding ) throws Exception {
292+
public static String encrypt(String content, String publicKey, String cipherAlgorithm, String characterEncoding ) throws IOException, GeneralSecurityException {
288293
return Base64.encode(RSA.encrypt(content.getBytes(characterEncoding), RSA.getPublicKey(publicKey),1024, 11, cipherAlgorithm));
289294
}
290295

pay-java-common/src/main/java/com/egzosn/pay/common/util/sign/encrypt/RSA2.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
package com.egzosn.pay.common.util.sign.encrypt;
33

4+
import java.io.IOException;
5+
import java.security.GeneralSecurityException;
46
import java.security.PrivateKey;
57
import java.security.PublicKey;
68

@@ -63,11 +65,11 @@ public static boolean verify(String content, String sign, PublicKey publicKey, S
6365
* @return 解密后的字符串
6466
* @throws Exception 解密异常
6567
*/
66-
public static String decrypt(String content, String privateKey, String characterEncoding) throws Exception {
68+
public static String decrypt(String content, String privateKey, String characterEncoding) throws GeneralSecurityException, IOException {
6769
return RSA.decrypt(content, privateKey, characterEncoding);
6870
}
6971

70-
72+
7173
/**
7274
* 得到私钥
7375
* @param key 密钥字符串(经过base64编码)
@@ -79,7 +81,7 @@ public static PrivateKey getPrivateKey(String key) throws Exception {
7981
}
8082

8183

82-
public static String encrypt(String content, String publicKey, String cipherAlgorithm, String characterEncoding ) throws Exception {
83-
return Base64.encode(RSA.encrypt(content.getBytes(characterEncoding), RSA.getPublicKey(publicKey),2048, 11, cipherAlgorithm));
84+
public static String encrypt(String content, String publicKey, String cipherAlgorithm, String characterEncoding ) throws GeneralSecurityException, IOException {
85+
return Base64.encode(RSA.encrypt(content.getBytes(characterEncoding), RSA.getPublicKey(publicKey), 2048, 11, cipherAlgorithm));
8486
}
8587
}

0 commit comments

Comments
 (0)