From 3ca5981b8a086feb249cab90657f0ea234f1af21 Mon Sep 17 00:00:00 2001 From: egan Date: Mon, 23 Jul 2018 12:45:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BE=AE=E4=BF=A1=E8=BD=AC?= =?UTF-8?q?=E8=B4=A6=E5=88=B0=E9=93=B6=E8=A1=8C=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pay-java-ali/pom.xml | 2 +- pay-java-common/pom.xml | 2 +- .../pay/common/util/sign/encrypt/RSA.java | 105 ++++++++++++++---- .../pay/common/util/sign/encrypt/RSA2.java | 6 + pay-java-demo/pom.xml | 2 +- pay-java-fuiou/pom.xml | 2 +- pay-java-payoneer/pom.xml | 2 +- pay-java-paypal/pom.xml | 2 +- pay-java-union/pom.xml | 2 +- pay-java-wx-youdian/pom.xml | 2 +- pay-java-wx/pom.xml | 2 +- .../com/egzosn/pay/wx/api/WxPayService.java | 16 ++- pom.xml | 8 +- 13 files changed, 113 insertions(+), 40 deletions(-) diff --git a/pay-java-ali/pom.xml b/pay-java-ali/pom.xml index b332a1d2..b21a748f 100644 --- a/pay-java-ali/pom.xml +++ b/pay-java-ali/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.1 + 2.10.2 4.0.0 pay-java-ali diff --git a/pay-java-common/pom.xml b/pay-java-common/pom.xml index eba3667e..73a994c0 100644 --- a/pay-java-common/pom.xml +++ b/pay-java-common/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.1 + 2.10.2 4.0.0 jar diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/util/sign/encrypt/RSA.java b/pay-java-common/src/main/java/com/egzosn/pay/common/util/sign/encrypt/RSA.java index 26aef76a..3b1c9289 100644 --- a/pay-java-common/src/main/java/com/egzosn/pay/common/util/sign/encrypt/RSA.java +++ b/pay-java-common/src/main/java/com/egzosn/pay/common/util/sign/encrypt/RSA.java @@ -6,6 +6,7 @@ import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.PublicKey; @@ -16,6 +17,7 @@ public class RSA{ private static final String ALGORITHM = "RSA"; + private static final String SIGN_ALGORITHMS = "SHA1WithRSA"; @@ -106,9 +108,7 @@ public static String sign(String content, PrivateKey privateKey ,String characte */ public static boolean verify(String content, String sign, String publicKey, String signAlgorithms, String characterEncoding){ try { - KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); - byte[] encodedKey = Base64.decode(publicKey); - PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); + PublicKey pubKey = getPublicKey(publicKey, ALGORITHM); java.security.Signature signature = java.security.Signature.getInstance(signAlgorithms); signature.initVerify(pubKey); signature.update( content.getBytes(characterEncoding) ); @@ -177,26 +177,28 @@ public static String decrypt(String content, String privateKey, String character PrivateKey prikey = getPrivateKey(privateKey); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, prikey); - InputStream ins = new ByteArrayInputStream(Base64.decode(content)); - ByteArrayOutputStream writer = new ByteArrayOutputStream(); - //rsa解密的字节大小最多是128,将需要解密的内容,按128位拆开解密 - byte[] buf = new byte[128]; - int bufl; - while ((bufl = ins.read(buf)) != -1) { - byte[] block = null; - - if (buf.length == bufl) { - block = buf; - } else { - block = new byte[bufl]; - for (int i = 0; i < bufl; i++) { - block[i] = buf[i]; - } - } - writer.write(cipher.doFinal(block)); - } - - return new String(writer.toByteArray(), characterEncoding); + try(InputStream ins = new ByteArrayInputStream(Base64.decode(content)); ByteArrayOutputStream writer = new ByteArrayOutputStream();) { + + //rsa解密的字节大小最多是128,将需要解密的内容,按128位拆开解密 + byte[] buf = new byte[128]; + int bufl; + while ((bufl = ins.read(buf)) != -1) { + byte[] block = null; + + if (buf.length == bufl) { + block = buf; + } else { + block = new byte[bufl]; + + for (int i = 0; i < bufl; i++) { + block[i] = buf[i]; + } + } + writer.write(cipher.doFinal(block)); + } + + return new String(writer.toByteArray(), characterEncoding); + } } @@ -215,4 +217,61 @@ public static PrivateKey getPrivateKey(String key) throws Exception { PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; } + + /** + * 得到公钥 + * @param key 密钥字符串(经过base64编码) + * @throws Exception 加密异常 + * @return 公钥 + */ + public static PublicKey getPublicKey(String key, String signAlgorithms) throws Exception { + KeyFactory keyFactory = KeyFactory.getInstance(signAlgorithms); + byte[] encodedKey = Base64.decode(key); + PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); + return pubKey; + } + + + /** + * 得到公钥 + * @param key 密钥字符串(经过base64编码) + * @throws Exception 加密异常 + * @return 公钥 + */ + public static PublicKey getPublicKey(String key) throws Exception { + + return getPublicKey(key, ALGORITHM); + } + + + + public static byte[] encrypt(byte[] plainBytes, PublicKey publicKey, int keyLength, int reserveSize, String cipherAlgorithm) throws Exception { + int keyByteSize = keyLength / 8; + int encryptBlockSize = keyByteSize - reserveSize; + int length = plainBytes.length; + int nBlock = length / encryptBlockSize; + if ((length % encryptBlockSize) != 0) { + nBlock += 1; + } + Cipher cipher = Cipher.getInstance(cipherAlgorithm); + cipher.init(Cipher.ENCRYPT_MODE, publicKey); + try (ByteArrayOutputStream outbuf = new ByteArrayOutputStream(nBlock * keyByteSize)) { + + for (int offset = 0; offset encryptBlockSize) { + inputLen = encryptBlockSize; + } + byte[] encryptedBlock = cipher.doFinal(plainBytes, offset, inputLen); + outbuf.write(encryptedBlock); + } + outbuf.flush(); + return outbuf.toByteArray(); + } + } + + public static String encrypt(String content, String publicKey, String cipherAlgorithm, String characterEncoding ) throws Exception { + return new String(RSA.encrypt(content.getBytes(Charset.forName(characterEncoding)), RSA.getPublicKey(publicKey), 1024, 11, cipherAlgorithm), characterEncoding); + } + } diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/util/sign/encrypt/RSA2.java b/pay-java-common/src/main/java/com/egzosn/pay/common/util/sign/encrypt/RSA2.java index a4c59c4a..fd00a5dc 100644 --- a/pay-java-common/src/main/java/com/egzosn/pay/common/util/sign/encrypt/RSA2.java +++ b/pay-java-common/src/main/java/com/egzosn/pay/common/util/sign/encrypt/RSA2.java @@ -1,6 +1,7 @@ package com.egzosn.pay.common.util.sign.encrypt; +import java.nio.charset.Charset; import java.security.PrivateKey; import java.security.PublicKey; @@ -77,4 +78,9 @@ public static String decrypt(String content, String privateKey, String character public static PrivateKey getPrivateKey(String key) throws Exception { return RSA.getPrivateKey(key); } + + + public static String encrypt(String content, String publicKey, String cipherAlgorithm, String characterEncoding ) throws Exception { + return new String(RSA.encrypt(content.getBytes(Charset.forName(characterEncoding)), RSA.getPublicKey(publicKey), 2048, 11, cipherAlgorithm), characterEncoding); + } } diff --git a/pay-java-demo/pom.xml b/pay-java-demo/pom.xml index 20173c17..eceb839d 100644 --- a/pay-java-demo/pom.xml +++ b/pay-java-demo/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.1 + 2.10.2 4.0.0 war diff --git a/pay-java-fuiou/pom.xml b/pay-java-fuiou/pom.xml index d207c47e..05a9d0a2 100644 --- a/pay-java-fuiou/pom.xml +++ b/pay-java-fuiou/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.1 + 2.10.2 4.0.0 pay-java-fuiou diff --git a/pay-java-payoneer/pom.xml b/pay-java-payoneer/pom.xml index 5ddb0859..021a88f3 100644 --- a/pay-java-payoneer/pom.xml +++ b/pay-java-payoneer/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.1 + 2.10.2 4.0.0 pay-java-payoneer diff --git a/pay-java-paypal/pom.xml b/pay-java-paypal/pom.xml index c2d0392b..008948a7 100644 --- a/pay-java-paypal/pom.xml +++ b/pay-java-paypal/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.1 + 2.10.2 4.0.0 diff --git a/pay-java-union/pom.xml b/pay-java-union/pom.xml index 0aa958ed..3bcb4846 100644 --- a/pay-java-union/pom.xml +++ b/pay-java-union/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.1 + 2.10.2 4.0.0 diff --git a/pay-java-wx-youdian/pom.xml b/pay-java-wx-youdian/pom.xml index 6edb721f..95011e6c 100644 --- a/pay-java-wx-youdian/pom.xml +++ b/pay-java-wx-youdian/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.1 + 2.10.2 4.0.0 pay-java-wx-youdian diff --git a/pay-java-wx/pom.xml b/pay-java-wx/pom.xml index d9fa23db..6f8ba266 100644 --- a/pay-java-wx/pom.xml +++ b/pay-java-wx/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.1 + 2.10.2 4.0.0 pay-java-wx diff --git a/pay-java-wx/src/main/java/com/egzosn/pay/wx/api/WxPayService.java b/pay-java-wx/src/main/java/com/egzosn/pay/wx/api/WxPayService.java index 490eac5d..1b2f4c07 100644 --- a/pay-java-wx/src/main/java/com/egzosn/pay/wx/api/WxPayService.java +++ b/pay-java-wx/src/main/java/com/egzosn/pay/wx/api/WxPayService.java @@ -10,6 +10,7 @@ import com.egzosn.pay.common.http.HttpConfigStorage; import com.egzosn.pay.common.util.MatrixToImageWriter; import com.egzosn.pay.common.util.sign.SignUtils; +import com.egzosn.pay.common.util.sign.encrypt.RSA2; import com.egzosn.pay.common.util.str.StringUtils; import com.egzosn.pay.wx.bean.WxPayError; import com.egzosn.pay.wx.bean.WxTransactionType; @@ -21,6 +22,7 @@ import java.io.InputStream; import java.math.BigDecimal; import java.net.URLEncoder; +import java.nio.charset.Charset; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; @@ -50,6 +52,8 @@ public class WxPayService extends BasePayService { public final static String SUCCESS = "SUCCESS"; public final static String RETURN_CODE = "return_code"; public final static String SIGN = "sign"; + public final static String CIPHER_ALGORITHM = "RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING"; + public final static String FAILURE = "failure"; @@ -483,18 +487,18 @@ public Map downloadbill(Date billDate, String billType) { public Map secondaryInterface(Object transactionIdOrBillDate, String outTradeNoBillType, TransactionType transactionType) { if (transactionType == WxTransactionType.REFUND) { - throw new PayErrorException(new PayException("failure", "通用接口不支持:" + transactionType)); + throw new PayErrorException(new PayException(FAILURE, "通用接口不支持:" + transactionType)); } if (transactionType == WxTransactionType.DOWNLOADBILL){ if (transactionIdOrBillDate instanceof Date){ return downloadbill((Date) transactionIdOrBillDate, outTradeNoBillType); } - throw new PayErrorException(new PayException("failure", "非法类型异常:" + transactionIdOrBillDate.getClass())); + throw new PayErrorException(new PayException(FAILURE, "非法类型异常:" + transactionIdOrBillDate.getClass())); } if (!(null == transactionIdOrBillDate || transactionIdOrBillDate instanceof String)){ - throw new PayErrorException(new PayException("failure", "非法类型异常:" + transactionIdOrBillDate.getClass())); + throw new PayErrorException(new PayException(FAILURE, "非法类型异常:" + transactionIdOrBillDate.getClass())); } //获取公共参数 @@ -563,6 +567,10 @@ public int conversion(BigDecimal amount){ } public String keyPublic(String content){ - return SignUtils.RSA.createSign(content, payConfigStorage.getKeyPublic(), payConfigStorage.getInputCharset()); + try { + return RSA2.encrypt(content, payConfigStorage.getKeyPublic(), CIPHER_ALGORITHM, payConfigStorage.getInputCharset()); + } catch (Exception e) { + throw new PayErrorException(new WxPayError(FAILURE, e.getLocalizedMessage())); + } } } diff --git a/pom.xml b/pom.xml index 09e13705..b748d64d 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.egzosn pay-java-parent pom - 2.10.1 + 2.10.2 Pay Java - Parent Pay Java Parent @@ -56,7 +56,7 @@ - 2.10.1 + 2.10.2 4.5.4 1.2.17 1.2.41 @@ -126,7 +126,7 @@ utf-8 - +