Skip to content

Commit

Permalink
修复微信转账到银行卡
Browse files Browse the repository at this point in the history
  • Loading branch information
egzosn committed Jul 24, 2018
1 parent 3ca5981 commit 4104ae0
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 39 deletions.
2 changes: 1 addition & 1 deletion pay-java-ali/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.10.2</version>
<version>2.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay-java-ali</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pay-java-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.10.2</version>
<version>2.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}


Expand All @@ -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 <length; offset += encryptBlockSize) {
int inputLen = length - offset;
try (ByteArrayOutputStream outbuf = new ByteArrayOutputStream(nBlock * keyByteSize);) {
Cipher cipher = Cipher.getInstance(cipherAlgorithm);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
for (int offset = 0; offset < plainBytes.length; offset += encryptBlockSize) {
int inputLen = plainBytes.length - offset;
if (inputLen > encryptBlockSize) {
inputLen = encryptBlockSize;
}
Expand All @@ -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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
2 changes: 1 addition & 1 deletion pay-java-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.10.2</version>
<version>2.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
Expand Down
2 changes: 1 addition & 1 deletion pay-java-fuiou/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.10.2</version>
<version>2.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay-java-fuiou</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pay-java-payoneer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.10.2</version>
<version>2.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay-java-payoneer</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pay-java-paypal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.10.2</version>
<version>2.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion pay-java-union/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.10.2</version>
<version>2.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion pay-java-wx-youdian/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.10.2</version>
<version>2.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay-java-wx-youdian</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pay-java-wx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.10.2</version>
<version>2.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay-java-wx</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,9 @@ public Map<String, Object> 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);
}

/**
Expand All @@ -553,8 +554,8 @@ public Map<String, Object> 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);
}

/**
Expand All @@ -573,4 +574,7 @@ public String keyPublic(String content){
throw new PayErrorException(new WxPayError(FAILURE, e.getLocalizedMessage()));
}
}



}
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.egzosn</groupId>
<artifactId>pay-java-parent</artifactId>
<packaging>pom</packaging>
<version>2.10.2</version>
<version>2.10.2-SNAPSHOT</version>

<name>Pay Java - Parent</name>
<description>Pay Java Parent</description>
Expand Down Expand Up @@ -56,7 +56,7 @@


<properties>
<pay.version>2.10.2</pay.version>
<pay.version>2.10.2-SNAPSHOT</pay.version>
<httpmime.version>4.5.4</httpmime.version>
<log4j.version>1.2.17</log4j.version>
<fastjson.version>1.2.41</fastjson.version>
Expand Down Expand Up @@ -126,7 +126,7 @@
<encoding>utf-8</encoding>
</configuration>
</plugin>
<!-- <plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</version>
Expand Down Expand Up @@ -186,7 +186,7 @@
</goals>
</execution>
</executions>
</plugin>-->
</plugin>
</plugins>
</build>
</project>

0 comments on commit 4104ae0

Please sign in to comment.