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 325e5dd..6269ec9 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 class RSA{ * @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,32 +236,48 @@ public class RSA{ return getPublicKey(key, ALGORITHM); } - - - public static String encrypt(byte[] plainBytes, PublicKey publicKey, String cipherAlgorithm, String characterEncoding ) throws Exception { - Cipher cipher = Cipher.getInstance(cipherAlgorithm); - cipher.init(Cipher.DECRYPT_MODE, publicKey); - try(InputStream ins = new ByteArrayInputStream(plainBytes); ByteArrayOutputStream writer = new ByteArrayOutputStream();) { - 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]; - } + 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; } - writer.write(cipher.doFinal(block)); + sb.append(readLine); + sb.append('\r'); } - return new String(writer.toByteArray(), characterEncoding); + 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 nBlock = plainBytes.length / encryptBlockSize; + if ((plainBytes.length % encryptBlockSize) != 0) { + nBlock += 1; + } + 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; + } + 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 RSA.encrypt(Base64.decode(content), RSA.getPublicKey(publicKey), 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 6356977..745364e 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 class RSA2 { public static String encrypt(String content, String publicKey, String cipherAlgorithm, String characterEncoding ) throws Exception { - return RSA.encrypt(content.getBytes(Charset.forName(characterEncoding)), RSA.getPublicKey(publicKey),cipherAlgorithm, characterEncoding); + return Base64.encode(RSA.encrypt(content.getBytes(characterEncoding), RSA.getPublicKey(publicKey),2048, 11, cipherAlgorithm)); } } 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 d1e9558..3deeb64 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,7 +535,7 @@ public class WxPayService extends BasePayService { if (!StringUtils.isEmpty(order.getRemark())){ parameters.put("desc", order.getRemark()); } - parameters.put(SIGN, SignUtils.valueOf(payConfigStorage.getSignType()).sign(parameters, payConfigStorage.getKeyPrivate(), payConfigStorage.getInputCharset())); + parameters.put(SIGN, createSign(parameters, payConfigStorage.getInputCharset())); return getHttpRequestTemplate().postForObject(getUrl(WxTransactionType.BANK), XML.getMap2Xml(parameters), JSONObject.class); } @@ -553,7 +553,7 @@ public class WxPayService extends BasePayService { 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())); + parameters.put(SIGN, createSign(parameters, payConfigStorage.getInputCharset())); return getHttpRequestTemplate().postForObject(getUrl(WxTransactionType.QUERY_BANK), XML.getMap2Xml(parameters), JSONObject.class); } @@ -573,4 +573,7 @@ public class WxPayService extends BasePayService { throw new PayErrorException(new WxPayError(FAILURE, e.getLocalizedMessage())); } } + + + }