评论

微信支付V3接口敏感信息解密示例

微信支付V3接口敏感信息解密java、go、php示例

此示例只针对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);


最后一次编辑于  04-28  
点赞 0
收藏
评论
登录 后发表内容