支付宝php 验签 解密,支付宝敏感信息解密 - osc_fcq4wwqo的个人空间 - OSCHINA - 中文开源技术交流社区...

/*** 敏感信息解密*/@PostMapping(value= "/decryptor", produces =MediaType.APPLICATION_JSON_UTF8_VALUE)publicRestResponse decryptor(@RequestBody String encryptContent) {

RestResponse restResponse= newRestResponse();try{

Map openapiResult =JSON.parseObject(encryptContent,new TypeReference>() {

},

Feature.OrderedField);

String signType= StringUtils.defaultIfBlank(openapiResult.get("sign_type"), "RSA2");

String charset= StringUtils.defaultIfBlank(openapiResult.get("charset"), "UTF-8");

String encryptType= StringUtils.defaultIfBlank(openapiResult.get("encrypt_type"), "AES");

String sign= openapiResult.get("sign");

String content= openapiResult.get("response");

LOGGER.info(String.format("准备验签和解密,sign=[%s], signType=[%s], encryptType=[%s], encryptContent=[%s]", sign, signType, encryptType, encryptContent));//如果密文的

boolean isDataEncrypted = !content.startsWith("{");booleansignCheckPass;//2. 验签

String signContent =content;//支付宝应用公钥

String signVeriKey =ALIPAY_APPLET_PUBLIC_SECRET;//如果是加密的报文则需要在密文的前后添加双引号

if(isDataEncrypted) {

signContent= "\"" + signContent + "\"";

}try{

signCheckPass=AlipaySignature.rsaCheck(signContent, sign, signVeriKey, charset, signType);

}catch(AlipayApiException e) {//验签异常, 日志

LOGGER.error("验签异常,encryptContent=" +encryptContent, e);

restResponse.setRestStatus(RestStatus.FAIL_100021);returnrestResponse;

}if (!signCheckPass) {//验签不通过(异常或者报文被篡改),终止流程(不需要做解密)

LOGGER.error("验签失败,encryptContent=" +encryptContent);

restResponse.setRestStatus(RestStatus.FAIL_100021);returnrestResponse;

}//支付宝小程序AES秘钥

String encryptKey =ALIPAY_APPLET_AES_SECRET;//3. 解密

String plainData;if(isDataEncrypted) {try{

plainData=AlipayEncrypt.decryptContent(content, encryptType, encryptKey, charset);

}catch(AlipayApiException e) {//解密异常, 日志

LOGGER.error("解密异常,encryptContent=" +encryptContent, e);

restResponse.setRestStatus(RestStatus.FAIL_100021);returnrestResponse;

}

}else{

plainData=content;

}

restResponse.setData(plainData);

restResponse.setRestStatus(RestStatus.SUCCESS);

}catch(Exception e) {

LOGGER.error("验签解密异常,encryptContent=" +encryptContent, e);

restResponse.setRestStatus(RestStatus.FAIL_100021);

}returnrestResponse;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值