|
8 | 8 | import com.github.binarywang.wxpay.service.WxPayService; |
9 | 9 | import com.github.binarywang.wxpay.util.SignUtils; |
10 | 10 | import org.apache.commons.codec.binary.Base64; |
| 11 | +import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; |
11 | 12 | import org.bouncycastle.jce.provider.BouncyCastleProvider; |
12 | | -import org.bouncycastle.jce.provider.JCERSAPublicKey; |
13 | | -import org.bouncycastle.openssl.PEMReader; |
14 | | -import org.bouncycastle.openssl.PasswordFinder; |
| 13 | +import org.bouncycastle.openssl.PEMParser; |
| 14 | +import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; |
15 | 15 |
|
16 | 16 | import javax.crypto.Cipher; |
17 | 17 | import java.io.File; |
18 | 18 | import java.io.FileReader; |
19 | 19 | import java.io.IOException; |
20 | 20 | import java.nio.file.Files; |
21 | 21 | import java.nio.file.Path; |
| 22 | +import java.security.PublicKey; |
22 | 23 | import java.security.Security; |
23 | 24 |
|
24 | 25 | /** |
@@ -107,15 +108,10 @@ private String encryptRSA(File publicKeyFile, String srcString) throws WxPayExce |
107 | 108 | try { |
108 | 109 | Security.addProvider(new BouncyCastleProvider()); |
109 | 110 | Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding"); |
110 | | - PasswordFinder passwordFinder = new PasswordFinder() { |
111 | | - @Override |
112 | | - public char[] getPassword() { |
113 | | - return "".toCharArray(); |
114 | | - } |
115 | | - }; |
116 | | - |
117 | | - try (PEMReader reader = new PEMReader(new FileReader(publicKeyFile), passwordFinder)) { |
118 | | - JCERSAPublicKey publicKey = (JCERSAPublicKey) reader.readObject(); |
| 111 | + try (PEMParser reader = new PEMParser(new FileReader(publicKeyFile))) { |
| 112 | + final PublicKey publicKey = new JcaPEMKeyConverter().setProvider("BC") |
| 113 | + .getPublicKey((SubjectPublicKeyInfo) reader.readObject()); |
| 114 | + |
119 | 115 | cipher.init(Cipher.ENCRYPT_MODE, publicKey); |
120 | 116 | byte[] encrypt = cipher.doFinal(srcString.getBytes()); |
121 | 117 | return Base64.encodeBase64String(encrypt); |
|
0 commit comments