Skip to content

Commit

Permalink
修复微信转账到银行卡
Browse files Browse the repository at this point in the history
  • Loading branch information
egzosn committed Jul 23, 2018
1 parent 62a8cda commit 3ca5981
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 40 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.1</version>
<version>2.10.2</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.1</version>
<version>2.10.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +17,7 @@ public class RSA{

private static final String ALGORITHM = "RSA";


private static final String SIGN_ALGORITHMS = "SHA1WithRSA";


Expand Down Expand Up @@ -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) );
Expand Down Expand Up @@ -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);
}
}


Expand All @@ -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 <length; offset += encryptBlockSize) {
int inputLen = length - offset;
if (inputLen > 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);
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
}
}
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.1</version>
<version>2.10.2</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.1</version>
<version>2.10.2</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.1</version>
<version>2.10.2</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.1</version>
<version>2.10.2</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.1</version>
<version>2.10.2</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.1</version>
<version>2.10.2</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.1</version>
<version>2.10.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay-java-wx</artifactId>
Expand Down
16 changes: 12 additions & 4 deletions pay-java-wx/src/main/java/com/egzosn/pay/wx/api/WxPayService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -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";



Expand Down Expand Up @@ -483,18 +487,18 @@ public Map<String, Object> downloadbill(Date billDate, String billType) {
public Map<String, Object> 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()));
}

//获取公共参数
Expand Down Expand Up @@ -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()));
}
}
}
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.1</version>
<version>2.10.2</version>

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


<properties>
<pay.version>2.10.1</pay.version>
<pay.version>2.10.2</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 @@ -173,7 +173,7 @@
</execution>
</executions>
</plugin>
<!-- <plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
Expand Down

0 comments on commit 3ca5981

Please sign in to comment.