Skip to content

Commit f6d5ae6

Browse files
authored
Merge pull request #53 from alipay/feature-base64
优化base64多jdk版本兼容
2 parents 179f6d5 + 72f5b95 commit f6d5ae6

File tree

8 files changed

+53
-33
lines changed

8 files changed

+53
-33
lines changed

CHANGE.log

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,5 @@ update --- Leg 航司场景,风控诉求
130130
CKP二期支付结果查询和支付结果通知透出支付方式信息
131131
38、Version:2.0.46
132132
update --- notify_dispute 拒付通知增加争议类型
133+
39、Version:2.0.48
134+
update --- 优化base64多jdk版本兼容

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ https://mvnrepository.com/artifact/com.alipay.global.sdk/global-open-sdk-java
1212
<dependency>
1313
<groupId>com.alipay.global.sdk</groupId>
1414
<artifactId>global-open-sdk-java</artifactId>
15-
<version>2.0.46</version>
15+
<version>2.0.48</version>
1616
</dependency>
1717
```
1818

@@ -238,7 +238,7 @@ boolean isPass = SignatureTool.verify(httpMethod, path, clientId, rspTimeStr,
238238
For compatibility with lower version of Java JDK, signatureTool provided a base64 encryptor DefaultBase64Encryptor by default.
239239

240240
```
241-
public class DefaultBase64Encryptor implements Base64Encryptor{
241+
public class DefaultBase64Encryptor implements Base64Encryptor {
242242
243243
@Override
244244
public String encodeToString(byte[] src) {
@@ -272,6 +272,6 @@ public class YourBase64Encryptor implements Base64Encryptor{
272272
273273
}
274274
275-
SignatureTool.setBase64Encryptor(new YourBase64Encryptor());
275+
Base64Provider.setBase64Encryptor(new YourBase64Encryptor());
276276
277277
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.alipay.global.sdk</groupId>
55
<artifactId>global-open-sdk-java</artifactId>
66
<packaging>jar</packaging>
7-
<version>2.0.46</version>
7+
<version>2.0.48</version>
88
<name>global-open-sdk-java</name>
99
<url>https://github.com/alipay/global-open-sdk-java</url>
1010
<description>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.alipay.global.api.base64;
2+
3+
import lombok.Getter;
4+
5+
/**
6+
* 可以根据需求定制base64实现
7+
*/
8+
public class Base64Provider {
9+
10+
@Getter
11+
private static Base64Encryptor base64Encryptor = new DefaultBase64Encryptor();
12+
13+
public static void setBase64Encryptor(Base64Encryptor base64Encryptor) {
14+
Base64Provider.base64Encryptor = base64Encryptor;
15+
}
16+
17+
}

src/main/java/com/alipay/global/api/base64/DefaultBase64Encryptor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import javax.xml.bind.DatatypeConverter;
44

5-
public class DefaultBase64Encryptor implements Base64Encryptor{
5+
/**
6+
* 为兼容低版本的java,默认使用javax.xml.bind.DatatypeConverter
7+
*/
8+
public class DefaultBase64Encryptor implements Base64Encryptor {
69

710
@Override
811
public String encodeToString(byte[] src) {

src/main/java/com/alipay/global/api/request/ams/risk/tee/crypto/AESCrypto.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package com.alipay.global.api.request.ams.risk.tee.crypto;
66

7+
import com.alipay.global.api.base64.Base64Provider;
78
import com.alipay.global.api.request.ams.risk.tee.constants.CryptoSdkConstant;
89
import com.alipay.global.api.request.ams.risk.tee.enums.ErrorCodeEnum;
910
import com.alipay.global.api.request.ams.risk.tee.exception.CryptoException;
@@ -15,7 +16,6 @@
1516
import org.bouncycastle.jce.provider.BouncyCastleProvider;
1617

1718
import javax.crypto.spec.SecretKeySpec;
18-
import javax.xml.bind.DatatypeConverter;
1919
import java.security.Security;
2020

2121
/**
@@ -79,7 +79,7 @@ public byte[] encrypt(String dataKeyBase64, byte[] data) {
7979
if (dataKeyBase64 == null || dataKeyBase64.length() == 0) {
8080
throw new CryptoException(ErrorCodeEnum.PARAM_ILLEGAL, "dataKey cannot be empty");
8181
}
82-
return encrypt(DatatypeConverter.parseBase64Binary(dataKeyBase64), data);
82+
return encrypt(Base64Provider.getBase64Encryptor().decode(dataKeyBase64), data);
8383
}
8484

8585
private byte[] encrypt(byte[] data, SecretKeySpec keySpec) throws Exception {

src/main/java/com/alipay/global/api/request/ams/risk/tee/encryptutil/RiskDecideEncryptUtil.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package com.alipay.global.api.request.ams.risk.tee.encryptutil;
66

7+
import com.alipay.global.api.base64.Base64Provider;
78
import com.alipay.global.api.model.ams.UserName;
89
import com.alipay.global.api.model.risk.Order;
910
import com.alipay.global.api.model.risk.PaymentDetail;
@@ -13,7 +14,7 @@
1314
import com.alipay.global.api.request.ams.risk.tee.enums.EncryptKeyEnum;
1415
import com.alipay.global.api.request.ams.risk.tee.enums.ErrorCodeEnum;
1516
import com.alipay.global.api.request.ams.risk.tee.exception.CryptoException;
16-
import javax.xml.bind.DatatypeConverter;
17+
1718
import java.nio.charset.Charset;
1819
import java.util.List;
1920

@@ -34,7 +35,7 @@ public static void encrypt(String dataKeyBase64, AlipayRequest<?> request, List<
3435
}
3536
RiskDecideRequest riskDecideRequest = (RiskDecideRequest) request;
3637
AESCrypto crypto = AESCrypto.getInstance();
37-
doEncrypt(DatatypeConverter.parseBase64Binary(dataKeyBase64), riskDecideRequest, encryptKeyList, crypto);
38+
doEncrypt(Base64Provider.getBase64Encryptor().decode(dataKeyBase64), riskDecideRequest, encryptKeyList, crypto);
3839
}
3940

4041
/**
@@ -61,23 +62,23 @@ private static void doEncrypt(byte[] data_key, RiskDecideRequest request, List<E
6162
continue;
6263
}
6364
encrypt = crypto.encrypt(data_key, buyerEmail.getBytes(utf8Charset));
64-
request.getBuyer().setBuyerEmail(DatatypeConverter.printBase64Binary(encrypt));
65+
request.getBuyer().setBuyerEmail(Base64Provider.getBase64Encryptor().encodeToString(encrypt));
6566
break;
6667
case BUYER_PHONE_NO:
6768
String buyerPhoneNo = request.getBuyer().getBuyerPhoneNo();
6869
if (buyerPhoneNo == null || buyerPhoneNo.isEmpty()) {
6970
continue;
7071
}
7172
encrypt = crypto.encrypt(data_key, buyerPhoneNo.getBytes(utf8Charset));
72-
request.getBuyer().setBuyerPhoneNo(DatatypeConverter.printBase64Binary(encrypt));
73+
request.getBuyer().setBuyerPhoneNo(Base64Provider.getBase64Encryptor().encodeToString(encrypt));
7374
break;
7475
case BUYER_REGISTRATION_TIME:
7576
String buyerRegistrationTime = request.getBuyer().getBuyerRegistrationTime();
7677
if (buyerRegistrationTime == null || buyerRegistrationTime.isEmpty()) {
7778
continue;
7879
}
7980
encrypt = crypto.encrypt(data_key, buyerRegistrationTime.getBytes(utf8Charset));
80-
request.getBuyer().setBuyerRegistrationTime(DatatypeConverter.printBase64Binary(encrypt));
81+
request.getBuyer().setBuyerRegistrationTime(Base64Provider.getBase64Encryptor().encodeToString(encrypt));
8182
break;
8283
case CARDHOLDER_NAME:
8384
for (PaymentDetail paymentDetail : paymentDetails) {
@@ -91,7 +92,7 @@ private static void doEncrypt(byte[] data_key, RiskDecideRequest request, List<E
9192
continue;
9293
}
9394
encrypt = crypto.encrypt(data_key, address1.getBytes(utf8Charset));
94-
order.getShipping().getShippingAddress().setAddress1(DatatypeConverter.printBase64Binary(encrypt));
95+
order.getShipping().getShippingAddress().setAddress1(Base64Provider.getBase64Encryptor().encodeToString(encrypt));
9596
}
9697
break;
9798
case SHIPPING_ADDRESS2:
@@ -101,7 +102,7 @@ private static void doEncrypt(byte[] data_key, RiskDecideRequest request, List<E
101102
continue;
102103
}
103104
encrypt = crypto.encrypt(data_key, address2.getBytes(utf8Charset));
104-
order.getShipping().getShippingAddress().setAddress2(DatatypeConverter.printBase64Binary(encrypt));
105+
order.getShipping().getShippingAddress().setAddress2(Base64Provider.getBase64Encryptor().encodeToString(encrypt));
105106
}
106107
break;
107108
case SHIPPING_NAME:
@@ -116,7 +117,7 @@ private static void doEncrypt(byte[] data_key, RiskDecideRequest request, List<E
116117
continue;
117118
}
118119
encrypt = crypto.encrypt(data_key, email.getBytes(utf8Charset));
119-
order.getShipping().setShipToEmail(DatatypeConverter.printBase64Binary(encrypt));
120+
order.getShipping().setShipToEmail(Base64Provider.getBase64Encryptor().encodeToString(encrypt));
120121
}
121122
break;
122123
case SHIPPING_PHONE_NO:
@@ -126,7 +127,7 @@ private static void doEncrypt(byte[] data_key, RiskDecideRequest request, List<E
126127
continue;
127128
}
128129
encrypt = crypto.encrypt(data_key, phoneNo.getBytes(utf8Charset));
129-
order.getShipping().setShippingPhoneNo(DatatypeConverter.printBase64Binary(encrypt));
130+
order.getShipping().setShippingPhoneNo(Base64Provider.getBase64Encryptor().encodeToString(encrypt));
130131
}
131132
break;
132133
default:
@@ -147,19 +148,19 @@ private static void encryptName(byte[] data_key, UserName userName, AESCrypto cr
147148
return;
148149
}
149150
if (userName.getFirstName() != null && !userName.getFirstName().isEmpty()) {
150-
userName.setFirstName(DatatypeConverter.printBase64Binary(
151+
userName.setFirstName(Base64Provider.getBase64Encryptor().encodeToString(
151152
crypto.encrypt(data_key, userName.getFirstName().getBytes(utf8Charset))));
152153
}
153154
if (userName.getMiddleName() != null && !userName.getMiddleName().isEmpty()) {
154-
userName.setMiddleName(DatatypeConverter.printBase64Binary(
155+
userName.setMiddleName(Base64Provider.getBase64Encryptor().encodeToString(
155156
crypto.encrypt(data_key, userName.getMiddleName().getBytes(utf8Charset))));
156157
}
157158
if (userName.getLastName() != null && !userName.getLastName().isEmpty()) {
158-
userName.setLastName(DatatypeConverter.printBase64Binary(
159+
userName.setLastName(Base64Provider.getBase64Encryptor().encodeToString(
159160
crypto.encrypt(data_key, userName.getLastName().getBytes(utf8Charset))));
160161
}
161162
if (userName.getFullName() != null && !userName.getFullName().isEmpty()) {
162-
userName.setFullName(DatatypeConverter.printBase64Binary(
163+
userName.setFullName(Base64Provider.getBase64Encryptor().encodeToString(
163164
crypto.encrypt(data_key, userName.getFullName().getBytes(utf8Charset))));
164165
}
165166
}

src/main/java/com/alipay/global/api/tools/SignatureTool.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
/**
55
* Alipay.com Inc. Copyright (c) 2004-2019 All Rights Reserved.
66
*/
7-
import com.alipay.global.api.base64.Base64Encryptor;
8-
import com.alipay.global.api.base64.DefaultBase64Encryptor;
7+
8+
import com.alipay.global.api.base64.Base64Provider;
99

1010
import java.io.UnsupportedEncodingException;
1111
import java.net.URLDecoder;
1212
import java.net.URLEncoder;
13-
import java.security.*;
13+
import java.security.KeyFactory;
14+
import java.security.PrivateKey;
15+
import java.security.PublicKey;
16+
import java.security.Signature;
1417
import java.security.spec.PKCS8EncodedKeySpec;
1518
import java.security.spec.X509EncodedKeySpec;
1619

@@ -20,8 +23,6 @@ public class SignatureTool {
2023
private static final String SHA256WITHRSA = "SHA256withRSA";
2124
private static final String DEFAULT_CHARSET = "UTF-8";
2225

23-
private static Base64Encryptor base64Encryptor = new DefaultBase64Encryptor();
24-
2526
public static String sign(String httpMethod, String path, String clientId, String reqTimeStr, String reqBody, String merchantPrivateKey) throws Exception{
2627
String reqContent = genSignContent(httpMethod, path, clientId, reqTimeStr, reqBody);
2728
return encode(signWithSHA256RSA(reqContent, merchantPrivateKey), DEFAULT_CHARSET);
@@ -80,7 +81,7 @@ private static boolean verifySignatureWithSHA256RSA(String rspContent, String si
8081
publicSignature.initVerify(publicKey);
8182
publicSignature.update(rspContent.getBytes(DEFAULT_CHARSET));
8283

83-
byte[] signatureBytes = base64Encryptor.decode(signature);
84+
byte[] signatureBytes = Base64Provider.getBase64Encryptor().decode(signature);
8485
return publicSignature.verify(signatureBytes);
8586

8687
}
@@ -100,7 +101,7 @@ private static String signWithSHA256RSA(String reqContent, String strPrivateKey)
100101
privateSignature.update(reqContent.getBytes(DEFAULT_CHARSET));
101102
byte[] s = privateSignature.sign();
102103

103-
return base64Encryptor.encodeToString(s);
104+
return Base64Provider.getBase64Encryptor().encodeToString(s);
104105
}
105106

106107
/**
@@ -109,7 +110,7 @@ private static String signWithSHA256RSA(String reqContent, String strPrivateKey)
109110
* @return
110111
*/
111112
private static PublicKey getPublicKeyFromBase64String(String publicKeyString) throws Exception{
112-
byte[] b1 = base64Encryptor.decode(publicKeyString);
113+
byte[] b1 = Base64Provider.getBase64Encryptor().decode(publicKeyString);
113114
X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(b1);
114115
KeyFactory kf = KeyFactory.getInstance(RSA);
115116
return kf.generatePublic(X509publicKey);
@@ -122,7 +123,7 @@ private static PublicKey getPublicKeyFromBase64String(String publicKeyString) th
122123
* @throws Exception
123124
*/
124125
private static PrivateKey getPrivateKeyFromBase64String(String privateKeyString) throws Exception{
125-
byte[] b1 = base64Encryptor.decode(privateKeyString);
126+
byte[] b1 = Base64Provider.getBase64Encryptor().decode(privateKeyString);
126127
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(b1);
127128
KeyFactory kf = KeyFactory.getInstance(RSA);
128129
return kf.generatePrivate(spec);
@@ -152,8 +153,4 @@ private static String decode(String originalStr,
152153
return URLDecoder.decode(originalStr, characterEncoding);
153154
}
154155

155-
public static void setBase64Encryptor(Base64Encryptor customBase64Encryptor){
156-
base64Encryptor = customBase64Encryptor;
157-
}
158-
159156
}

0 commit comments

Comments
 (0)