mirror of
https://gitee.com/egzosn/pay-java-parent.git
synced 2026-05-31 12:59:35 +08:00
修复微信转账到银行卡
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.io.ByteArrayInputStream;
|
||||
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 class RSA{
|
||||
*/
|
||||
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 class RSA{
|
||||
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;
|
||||
try(InputStream ins = new ByteArrayInputStream(Base64.decode(content)); ByteArrayOutputStream writer = new ByteArrayOutputStream();) {
|
||||
|
||||
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));
|
||||
}
|
||||
//rsa解密的字节大小最多是128,将需要解密的内容,按128位拆开解密
|
||||
byte[] buf = new byte[128];
|
||||
int bufl;
|
||||
while ((bufl = ins.read(buf)) != -1) {
|
||||
byte[] block = null;
|
||||
|
||||
return new String(writer.toByteArray(), characterEncoding);
|
||||
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 class RSA{
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 class RSA2 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.egzosn.pay.common.exception.PayErrorException;
|
||||
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.IOException;
|
||||
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 class WxPayService extends BasePayService {
|
||||
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()));
|
||||
}
|
||||
|
||||
//获取公共参数
|
||||
@@ -563,6 +567,10 @@ public class WxPayService extends BasePayService {
|
||||
}
|
||||
|
||||
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
pom.xml
8
pom.xml
@@ -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>
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
@@ -186,7 +186,7 @@
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugin>-->
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
Reference in New Issue
Block a user