此示例只针对https://wechatpay-api.gitbook.io/wechatpay-api-v3/qian-ming-zhi-nan-1/min-gan-xin-xi-jia-mi#jie-mi-shi-li所提及的解密方式。
java示例:
/**
*
* @param waitDecrypt 待解密字符串
* @param privateKey 私钥
* @return
* @throws BadPaddingException
* @throws IOException
*/
public static String rsaDecryptOAEP(String waitDecrypt, PrivateKey privateKey)
throws BadPaddingException, IOException {
try {
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] data = Base64.getDecoder().decode(waitDecrypt);
return new String(cipher.doFinal(data), "utf-8");
} catch (NoSuchPaddingException | NoSuchAlgorithmException e) {
throw new RuntimeException("当前Java环境不支持RSA v1.5/OAEP", e);
} catch (InvalidKeyException e) {
throw new IllegalArgumentException("无效的私钥", e);
} catch (BadPaddingException | IllegalBlockSizeException e) {
throw new BadPaddingException("解密失败");
}
}
/**
* 获取私钥。
*
* @param filename 私钥文件路径 (required)
* @return 私钥对象
*/
public static PrivateKey getPrivateKey(String filename) throws IOException {
String content = new String(Files.readAllBytes(Paths.get(filename)), "utf-8");
try {
String privateKey = content.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "")
.replaceAll("\\s+", "");
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(
new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("当前Java环境不支持RSA", e);
} catch (InvalidKeySpecException e) {
throw new RuntimeException("无效的密钥格式");
}
}
//调用
PrivateKey privatekey = getPrivateKey(privateKeyFileName);
String a = rsaDecryptOAEP(waitDecrypt,privatekey);
System.out.println(a);
go
// GetPrivateKey 获取私钥
// filename 私钥的地址
func GetPrivateKey(filename string) (*rsa.PrivateKey, error) {
keybuffer, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
block, _ := pem.Decode([]byte(keybuffer))
if block == nil {
return nil, errors.New("private key error!")
}
privatekey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return privatekey.(*rsa.PrivateKey), nil
}
// Decrypt 解密加密信息
// waitDecrypt 待解密的字符串
// ilename 私钥的地址
func Decrypt(waitDecrypt string, privateKeyFilename string) (string, error) {
rsaPrivateKey, err := GetPrivateKey(privateKeyFilename)
if err != nil {
return "", err
}
waitDecryptData, _ := base64.StdEncoding.DecodeString(waitDecrypt)
rng := rand.Reader
if err != nil {
return "", err
}
plaintext, err := rsa.DecryptOAEP(sha1.New(), rng, rsaPrivateKey, waitDecryptData, nil)
if err != nil {
return "", err
}
return string(plaintext), nil
}
//调用
plaintext, err := Decrypt(waitDecrypt, privateKeyFilename)
if err != nil {
fmt.Fprintf(os.Stderr, "Error:%s", err)
}
fmt.Printf("Plaintext: %s\n", plaintext)
php示例:
$privateKey = openssl_get_privatekey(file_get_contents($privateKeyFile));
if (openssl_private_decrypt(base64_decode($waitDecrypt),$decrypted,$privateKey, OPENSSL_PKCS1_OAEP_PADDING)){
echo '成功';
} else {
echo "失败";
}
print_r($decrypted);
哎~ 搞死了 逻辑都一样~
执行完你的代码 , 我的旧代码也解密出来了 搞了一晚上
这是为什么 有毒吗?
"开发者应当使用微信支付平台证书中的公钥,对上送的敏感信息进行加密。这样只有拥有私钥的微信支付才能对密文进行解密,从而保证了信息的机密性。",文档上说不是应该使用公钥解密吗?
你这个自己试过吗