支付宝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;
}
總結
以上是生活随笔為你收集整理的支付宝php 验签 解密,支付宝敏感信息解密 - osc_fcq4wwqo的个人空间 - OSCHINA - 中文开源技术交流社区...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 刚在太空过完年 神舟十五号航天员乘将首次
- 下一篇: php实现两个大整数求和,PHP计算两个