Skip to content

Commit

Permalink
Merge pull request #53 from alipay/feature-base64
Browse files Browse the repository at this point in the history
优化base64多jdk版本兼容
  • Loading branch information
ScottWryyyyy authored Dec 9, 2024
2 parents 179f6d5 + 72f5b95 commit f6d5ae6
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGE.log
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,5 @@ update --- Leg 航司场景,风控诉求
CKP二期支付结果查询和支付结果通知透出支付方式信息
38、Version:2.0.46
update --- notify_dispute 拒付通知增加争议类型
39、Version:2.0.48
update --- 优化base64多jdk版本兼容
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ https://mvnrepository.com/artifact/com.alipay.global.sdk/global-open-sdk-java
<dependency>
<groupId>com.alipay.global.sdk</groupId>
<artifactId>global-open-sdk-java</artifactId>
<version>2.0.46</version>
<version>2.0.48</version>
</dependency>
```

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

```
public class DefaultBase64Encryptor implements Base64Encryptor{
public class DefaultBase64Encryptor implements Base64Encryptor {
@Override
public String encodeToString(byte[] src) {
Expand Down Expand Up @@ -272,6 +272,6 @@ public class YourBase64Encryptor implements Base64Encryptor{
}
SignatureTool.setBase64Encryptor(new YourBase64Encryptor());
Base64Provider.setBase64Encryptor(new YourBase64Encryptor());
```
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.alipay.global.sdk</groupId>
<artifactId>global-open-sdk-java</artifactId>
<packaging>jar</packaging>
<version>2.0.46</version>
<version>2.0.48</version>
<name>global-open-sdk-java</name>
<url>https://github.com/alipay/global-open-sdk-java</url>
<description>
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/alipay/global/api/base64/Base64Provider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.alipay.global.api.base64;

import lombok.Getter;

/**
* 可以根据需求定制base64实现
*/
public class Base64Provider {

@Getter
private static Base64Encryptor base64Encryptor = new DefaultBase64Encryptor();

public static void setBase64Encryptor(Base64Encryptor base64Encryptor) {
Base64Provider.base64Encryptor = base64Encryptor;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import javax.xml.bind.DatatypeConverter;

public class DefaultBase64Encryptor implements Base64Encryptor{
/**
* 为兼容低版本的java,默认使用javax.xml.bind.DatatypeConverter
*/
public class DefaultBase64Encryptor implements Base64Encryptor {

@Override
public String encodeToString(byte[] src) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package com.alipay.global.api.request.ams.risk.tee.crypto;

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

import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.Security;

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

private byte[] encrypt(byte[] data, SecretKeySpec keySpec) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package com.alipay.global.api.request.ams.risk.tee.encryptutil;

import com.alipay.global.api.base64.Base64Provider;
import com.alipay.global.api.model.ams.UserName;
import com.alipay.global.api.model.risk.Order;
import com.alipay.global.api.model.risk.PaymentDetail;
Expand All @@ -13,7 +14,7 @@
import com.alipay.global.api.request.ams.risk.tee.enums.EncryptKeyEnum;
import com.alipay.global.api.request.ams.risk.tee.enums.ErrorCodeEnum;
import com.alipay.global.api.request.ams.risk.tee.exception.CryptoException;
import javax.xml.bind.DatatypeConverter;

import java.nio.charset.Charset;
import java.util.List;

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

/**
Expand All @@ -61,23 +62,23 @@ private static void doEncrypt(byte[] data_key, RiskDecideRequest request, List<E
continue;
}
encrypt = crypto.encrypt(data_key, buyerEmail.getBytes(utf8Charset));
request.getBuyer().setBuyerEmail(DatatypeConverter.printBase64Binary(encrypt));
request.getBuyer().setBuyerEmail(Base64Provider.getBase64Encryptor().encodeToString(encrypt));
break;
case BUYER_PHONE_NO:
String buyerPhoneNo = request.getBuyer().getBuyerPhoneNo();
if (buyerPhoneNo == null || buyerPhoneNo.isEmpty()) {
continue;
}
encrypt = crypto.encrypt(data_key, buyerPhoneNo.getBytes(utf8Charset));
request.getBuyer().setBuyerPhoneNo(DatatypeConverter.printBase64Binary(encrypt));
request.getBuyer().setBuyerPhoneNo(Base64Provider.getBase64Encryptor().encodeToString(encrypt));
break;
case BUYER_REGISTRATION_TIME:
String buyerRegistrationTime = request.getBuyer().getBuyerRegistrationTime();
if (buyerRegistrationTime == null || buyerRegistrationTime.isEmpty()) {
continue;
}
encrypt = crypto.encrypt(data_key, buyerRegistrationTime.getBytes(utf8Charset));
request.getBuyer().setBuyerRegistrationTime(DatatypeConverter.printBase64Binary(encrypt));
request.getBuyer().setBuyerRegistrationTime(Base64Provider.getBase64Encryptor().encodeToString(encrypt));
break;
case CARDHOLDER_NAME:
for (PaymentDetail paymentDetail : paymentDetails) {
Expand All @@ -91,7 +92,7 @@ private static void doEncrypt(byte[] data_key, RiskDecideRequest request, List<E
continue;
}
encrypt = crypto.encrypt(data_key, address1.getBytes(utf8Charset));
order.getShipping().getShippingAddress().setAddress1(DatatypeConverter.printBase64Binary(encrypt));
order.getShipping().getShippingAddress().setAddress1(Base64Provider.getBase64Encryptor().encodeToString(encrypt));
}
break;
case SHIPPING_ADDRESS2:
Expand All @@ -101,7 +102,7 @@ private static void doEncrypt(byte[] data_key, RiskDecideRequest request, List<E
continue;
}
encrypt = crypto.encrypt(data_key, address2.getBytes(utf8Charset));
order.getShipping().getShippingAddress().setAddress2(DatatypeConverter.printBase64Binary(encrypt));
order.getShipping().getShippingAddress().setAddress2(Base64Provider.getBase64Encryptor().encodeToString(encrypt));
}
break;
case SHIPPING_NAME:
Expand All @@ -116,7 +117,7 @@ private static void doEncrypt(byte[] data_key, RiskDecideRequest request, List<E
continue;
}
encrypt = crypto.encrypt(data_key, email.getBytes(utf8Charset));
order.getShipping().setShipToEmail(DatatypeConverter.printBase64Binary(encrypt));
order.getShipping().setShipToEmail(Base64Provider.getBase64Encryptor().encodeToString(encrypt));
}
break;
case SHIPPING_PHONE_NO:
Expand All @@ -126,7 +127,7 @@ private static void doEncrypt(byte[] data_key, RiskDecideRequest request, List<E
continue;
}
encrypt = crypto.encrypt(data_key, phoneNo.getBytes(utf8Charset));
order.getShipping().setShippingPhoneNo(DatatypeConverter.printBase64Binary(encrypt));
order.getShipping().setShippingPhoneNo(Base64Provider.getBase64Encryptor().encodeToString(encrypt));
}
break;
default:
Expand All @@ -147,19 +148,19 @@ private static void encryptName(byte[] data_key, UserName userName, AESCrypto cr
return;
}
if (userName.getFirstName() != null && !userName.getFirstName().isEmpty()) {
userName.setFirstName(DatatypeConverter.printBase64Binary(
userName.setFirstName(Base64Provider.getBase64Encryptor().encodeToString(
crypto.encrypt(data_key, userName.getFirstName().getBytes(utf8Charset))));
}
if (userName.getMiddleName() != null && !userName.getMiddleName().isEmpty()) {
userName.setMiddleName(DatatypeConverter.printBase64Binary(
userName.setMiddleName(Base64Provider.getBase64Encryptor().encodeToString(
crypto.encrypt(data_key, userName.getMiddleName().getBytes(utf8Charset))));
}
if (userName.getLastName() != null && !userName.getLastName().isEmpty()) {
userName.setLastName(DatatypeConverter.printBase64Binary(
userName.setLastName(Base64Provider.getBase64Encryptor().encodeToString(
crypto.encrypt(data_key, userName.getLastName().getBytes(utf8Charset))));
}
if (userName.getFullName() != null && !userName.getFullName().isEmpty()) {
userName.setFullName(DatatypeConverter.printBase64Binary(
userName.setFullName(Base64Provider.getBase64Encryptor().encodeToString(
crypto.encrypt(data_key, userName.getFullName().getBytes(utf8Charset))));
}
}
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/com/alipay/global/api/tools/SignatureTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
/**
* Alipay.com Inc. Copyright (c) 2004-2019 All Rights Reserved.
*/
import com.alipay.global.api.base64.Base64Encryptor;
import com.alipay.global.api.base64.DefaultBase64Encryptor;

import com.alipay.global.api.base64.Base64Provider;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.*;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

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

private static Base64Encryptor base64Encryptor = new DefaultBase64Encryptor();

public static String sign(String httpMethod, String path, String clientId, String reqTimeStr, String reqBody, String merchantPrivateKey) throws Exception{
String reqContent = genSignContent(httpMethod, path, clientId, reqTimeStr, reqBody);
return encode(signWithSHA256RSA(reqContent, merchantPrivateKey), DEFAULT_CHARSET);
Expand Down Expand Up @@ -80,7 +81,7 @@ private static boolean verifySignatureWithSHA256RSA(String rspContent, String si
publicSignature.initVerify(publicKey);
publicSignature.update(rspContent.getBytes(DEFAULT_CHARSET));

byte[] signatureBytes = base64Encryptor.decode(signature);
byte[] signatureBytes = Base64Provider.getBase64Encryptor().decode(signature);
return publicSignature.verify(signatureBytes);

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

return base64Encryptor.encodeToString(s);
return Base64Provider.getBase64Encryptor().encodeToString(s);
}

/**
Expand All @@ -109,7 +110,7 @@ private static String signWithSHA256RSA(String reqContent, String strPrivateKey)
* @return
*/
private static PublicKey getPublicKeyFromBase64String(String publicKeyString) throws Exception{
byte[] b1 = base64Encryptor.decode(publicKeyString);
byte[] b1 = Base64Provider.getBase64Encryptor().decode(publicKeyString);
X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(b1);
KeyFactory kf = KeyFactory.getInstance(RSA);
return kf.generatePublic(X509publicKey);
Expand All @@ -122,7 +123,7 @@ private static PublicKey getPublicKeyFromBase64String(String publicKeyString) th
* @throws Exception
*/
private static PrivateKey getPrivateKeyFromBase64String(String privateKeyString) throws Exception{
byte[] b1 = base64Encryptor.decode(privateKeyString);
byte[] b1 = Base64Provider.getBase64Encryptor().decode(privateKeyString);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(b1);
KeyFactory kf = KeyFactory.getInstance(RSA);
return kf.generatePrivate(spec);
Expand Down Expand Up @@ -152,8 +153,4 @@ private static String decode(String originalStr,
return URLDecoder.decode(originalStr, characterEncoding);
}

public static void setBase64Encryptor(Base64Encryptor customBase64Encryptor){
base64Encryptor = customBase64Encryptor;
}

}

0 comments on commit f6d5ae6

Please sign in to comment.