diff --git a/pay-java-ali/pom.xml b/pay-java-ali/pom.xml index b21a748f..76ea453d 100644 --- a/pay-java-ali/pom.xml +++ b/pay-java-ali/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.2 + 2.10.2-SNAPSHOT 4.0.0 pay-java-ali diff --git a/pay-java-common/pom.xml b/pay-java-common/pom.xml index 73a994c0..37c7b673 100644 --- a/pay-java-common/pom.xml +++ b/pay-java-common/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.2 + 2.10.2-SNAPSHOT 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 3b1c9289..6269ec94 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 @@ -2,11 +2,7 @@ package com.egzosn.pay.common.util.sign.encrypt; import javax.crypto.Cipher; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.InputStream; -import java.io.UnsupportedEncodingException; -import java.nio.charset.Charset; +import java.io.*; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.PublicKey; @@ -225,10 +221,7 @@ public static PrivateKey getPrivateKey(String key) 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; + return getPublicKey(new ByteArrayInputStream(key.getBytes()), signAlgorithms); } @@ -243,22 +236,36 @@ public static PublicKey getPublicKey(String key) throws Exception { return getPublicKey(key, ALGORITHM); } - + public static PublicKey getPublicKey(InputStream inputStream, String keyAlgorithm) throws Exception { + try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));) { + StringBuilder sb = new StringBuilder(); + String readLine = null; + while ((readLine = br.readLine()) != null) { + if (readLine.charAt(0) == '-') { + continue; + } + sb.append(readLine); + sb.append('\r'); + } + X509EncodedKeySpec pubX509 = new X509EncodedKeySpec(Base64.decode(sb.toString())); + KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm); + PublicKey publicKey = keyFactory.generatePublic(pubX509); + return publicKey; + } + } 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) { + int nBlock = plainBytes.length / encryptBlockSize; + if ((plainBytes.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; } @@ -269,9 +276,8 @@ public static byte[] encrypt(byte[] plainBytes, PublicKey publicKey, int keyLeng 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); + return Base64.encode(RSA.encrypt(content.getBytes(characterEncoding), RSA.getPublicKey(publicKey),1024, 11, cipherAlgorithm)); } } 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 fd00a5dc..745364e3 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 @@ -81,6 +81,6 @@ public static PrivateKey getPrivateKey(String key) throws Exception { 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); + return Base64.encode(RSA.encrypt(content.getBytes(characterEncoding), RSA.getPublicKey(publicKey),2048, 11, cipherAlgorithm)); } } diff --git a/pay-java-demo/pom.xml b/pay-java-demo/pom.xml index eceb839d..f870be41 100644 --- a/pay-java-demo/pom.xml +++ b/pay-java-demo/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.2 + 2.10.2-SNAPSHOT 4.0.0 war diff --git a/pay-java-fuiou/pom.xml b/pay-java-fuiou/pom.xml index 05a9d0a2..59b44527 100644 --- a/pay-java-fuiou/pom.xml +++ b/pay-java-fuiou/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.2 + 2.10.2-SNAPSHOT 4.0.0 pay-java-fuiou diff --git a/pay-java-payoneer/pom.xml b/pay-java-payoneer/pom.xml index 021a88f3..3b5379ec 100644 --- a/pay-java-payoneer/pom.xml +++ b/pay-java-payoneer/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.2 + 2.10.2-SNAPSHOT 4.0.0 pay-java-payoneer diff --git a/pay-java-paypal/pom.xml b/pay-java-paypal/pom.xml index 008948a7..831f07f4 100644 --- a/pay-java-paypal/pom.xml +++ b/pay-java-paypal/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.2 + 2.10.2-SNAPSHOT 4.0.0 diff --git a/pay-java-union/pom.xml b/pay-java-union/pom.xml index 3bcb4846..f987764e 100644 --- a/pay-java-union/pom.xml +++ b/pay-java-union/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.2 + 2.10.2-SNAPSHOT 4.0.0 diff --git a/pay-java-wx-youdian/pom.xml b/pay-java-wx-youdian/pom.xml index 95011e6c..0674bcf9 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.2 + 2.10.2-SNAPSHOT 4.0.0 pay-java-wx-youdian diff --git a/pay-java-wx/pom.xml b/pay-java-wx/pom.xml index 6f8ba266..d0b76e77 100644 --- a/pay-java-wx/pom.xml +++ b/pay-java-wx/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.10.2 + 2.10.2-SNAPSHOT 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 1b2f4c07..9b5fceeb 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 @@ -535,8 +535,9 @@ public Map transfer(TransferOrder order) { if (!StringUtils.isEmpty(order.getRemark())){ parameters.put("desc", order.getRemark()); } - parameters.put(SIGN, SignUtils.valueOf(payConfigStorage.getSignType()).sign(parameters, payConfigStorage.getKeyPrivate(), payConfigStorage.getInputCharset())); - return getHttpRequestTemplate().postForObject(getUrl(WxTransactionType.BANK), parameters, JSONObject.class); + parameters.put(SIGN, createSign(SignUtils.parameterText(parameters, "&", SIGN), payConfigStorage.getInputCharset())); + + return getHttpRequestTemplate().postForObject(getUrl(WxTransactionType.BANK), XML.getMap2Xml(parameters), JSONObject.class); } /** @@ -553,8 +554,8 @@ public Map transferQuery(String outNo, String tradeNo) { parameters.put("mch_id", payConfigStorage.getPid()); parameters.put("partner_trade_no", StringUtils.isEmpty(outNo) ? tradeNo : outNo); parameters.put("nonce_str", SignUtils.randomStr()); - parameters.put(SIGN, SignUtils.valueOf(payConfigStorage.getSignType()).sign(parameters, payConfigStorage.getKeyPrivate(), payConfigStorage.getInputCharset())); - return getHttpRequestTemplate().postForObject(getUrl(WxTransactionType.QUERY_BANK), parameters, JSONObject.class); + parameters.put(SIGN, createSign(SignUtils.parameterText(parameters, "&", SIGN), payConfigStorage.getInputCharset())); + return getHttpRequestTemplate().postForObject(getUrl(WxTransactionType.QUERY_BANK), XML.getMap2Xml(parameters), JSONObject.class); } /** @@ -573,4 +574,7 @@ public String keyPublic(String content){ throw new PayErrorException(new WxPayError(FAILURE, e.getLocalizedMessage())); } } + + + } diff --git a/pom.xml b/pom.xml index b748d64d..b2f9e512 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.egzosn pay-java-parent pom - 2.10.2 + 2.10.2-SNAPSHOT Pay Java - Parent Pay Java Parent @@ -56,7 +56,7 @@ - 2.10.2 + 2.10.2-SNAPSHOT 4.5.4 1.2.17 1.2.41 @@ -126,7 +126,7 @@ utf-8 - + \ No newline at end of file