Selaa lähdekoodia

//ttpayv2

formao-live
xhxu 3 vuotta sitten
vanhempi
commit
5aebf46bb7
1 muutettua tiedostoa jossa 31 lisäystä ja 0 poistoa
  1. +31
    -0
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/util/PemUtils.java

+ 31
- 0
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/util/PemUtils.java Näytä tiedosto

@@ -1,6 +1,7 @@
package com.github.binarywang.wxpay.v3.util;

import me.chanjar.weixin.common.error.WxRuntimeException;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -8,9 +9,11 @@ import java.io.InputStream;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class PemUtils {
@@ -55,4 +58,32 @@ public class PemUtils {
throw new WxRuntimeException("无效的证书", e);
}
}

public static PublicKey loadPublicKey(InputStream inputStream) {

try {
ByteArrayOutputStream array = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
array.write(buffer, 0, length);
}

String publicKey = array.toString("utf-8")
.replace("-----BEGIN PUBLIC KEY-----", "")
.replace("-----END PUBLIC KEY-----", "")
.replaceAll("\\s+", "");

KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(
new X509EncodedKeySpec(new BASE64Decoder().decodeBuffer(publicKey)));

} catch (NoSuchAlgorithmException e) {
throw new WxRuntimeException("当前Java环境不支持RSA", e);
} catch (InvalidKeySpecException e) {
throw new WxRuntimeException("无效的密钥格式");
} catch (IOException e) {
throw new WxRuntimeException("无效的密钥");
}
}
}

Ladataan…
Peruuta
Tallenna