mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-25 01:14:36 +08:00
重构RSA加密方法
This commit is contained in:
parent
5dcd06b620
commit
15a33a7d46
@ -41,8 +41,8 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.bouncycastle</groupId>
|
<groupId>org.bouncycastle</groupId>
|
||||||
<artifactId>bcprov-jdk16</artifactId>
|
<artifactId>bcpkix-jdk15on</artifactId>
|
||||||
<version>1.46</version>
|
<version>1.59</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -8,10 +8,10 @@ import com.github.binarywang.wxpay.service.EntPayService;
|
|||||||
import com.github.binarywang.wxpay.service.WxPayService;
|
import com.github.binarywang.wxpay.service.WxPayService;
|
||||||
import com.github.binarywang.wxpay.util.SignUtils;
|
import com.github.binarywang.wxpay.util.SignUtils;
|
||||||
import org.apache.commons.codec.binary.Base64;
|
import org.apache.commons.codec.binary.Base64;
|
||||||
|
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
|
||||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||||
import org.bouncycastle.jce.provider.JCERSAPublicKey;
|
import org.bouncycastle.openssl.PEMParser;
|
||||||
import org.bouncycastle.openssl.PEMReader;
|
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
|
||||||
import org.bouncycastle.openssl.PasswordFinder;
|
|
||||||
|
|
||||||
import javax.crypto.Cipher;
|
import javax.crypto.Cipher;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -19,6 +19,7 @@ import java.io.FileReader;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.security.PublicKey;
|
||||||
import java.security.Security;
|
import java.security.Security;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -107,15 +108,10 @@ public class EntPayServiceImpl implements EntPayService {
|
|||||||
try {
|
try {
|
||||||
Security.addProvider(new BouncyCastleProvider());
|
Security.addProvider(new BouncyCastleProvider());
|
||||||
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
|
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
|
||||||
PasswordFinder passwordFinder = new PasswordFinder() {
|
try (PEMParser reader = new PEMParser(new FileReader(publicKeyFile))) {
|
||||||
@Override
|
final PublicKey publicKey = new JcaPEMKeyConverter().setProvider("BC")
|
||||||
public char[] getPassword() {
|
.getPublicKey((SubjectPublicKeyInfo) reader.readObject());
|
||||||
return "".toCharArray();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
try (PEMReader reader = new PEMReader(new FileReader(publicKeyFile), passwordFinder)) {
|
|
||||||
JCERSAPublicKey publicKey = (JCERSAPublicKey) reader.readObject();
|
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||||
byte[] encrypt = cipher.doFinal(srcString.getBytes());
|
byte[] encrypt = cipher.doFinal(srcString.getBytes());
|
||||||
return Base64.encodeBase64String(encrypt);
|
return Base64.encodeBase64String(encrypt);
|
||||||
|
Loading…
Reference in New Issue
Block a user