com.github.sd4324530.fastweixin.message.aes
类 WXBizMsgCrypt

java.lang.Object
  继承者 com.github.sd4324530.fastweixin.message.aes.WXBizMsgCrypt

public class WXBizMsgCrypt
extends Object

提供接收和推送给公众平台消息的加解密接口(UTF8编码的字符串).

  1. 第三方回复加密消息给公众平台
  2. 第三方收到公众平台发送的消息,验证消息的安全性,并对消息进行解密。
说明:异常java.security.InvalidKeyException:illegal Key Size的解决方案
  1. 在官方网站下载JCE无限制权限策略文件(JDK7的下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
  2. 下载后解压,可以看到local_policy.jar和US_export_policy.jar以及readme.txt
  3. 如果安装了JRE,将两个jar文件放到%JRE_HOME%\lib\security目录下覆盖原来的文件
  4. 如果安装了JDK,将两个jar文件放到%JDK_HOME%\jre\lib\security目录下覆盖原来文件


构造方法摘要
WXBizMsgCrypt(String token, String encodingAesKey, String appId)
          构造函数
 
方法摘要
 String decryptMsg(String msgSignature, String timeStamp, String nonce, String postData)
          检验消息的真实性,并且获取解密后的明文.
 String encryptMsg(String replyMsg, String timeStamp, String nonce)
          将公众平台回复用户的消息加密打包.
 String verifyUrl(String msgSignature, String timeStamp, String nonce, String echoStr)
          验证URL
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

WXBizMsgCrypt

public WXBizMsgCrypt(String token,
                     String encodingAesKey,
                     String appId)
              throws AesException
构造函数

参数:
token - 公众平台上,开发者设置的token
encodingAesKey - 公众平台上,开发者设置的EncodingAESKey
appId - 公众平台appid
抛出:
AesException - 执行失败,请查看该异常的错误码和具体的错误信息
方法详细信息

encryptMsg

public String encryptMsg(String replyMsg,
                         String timeStamp,
                         String nonce)
                  throws AesException
将公众平台回复用户的消息加密打包.
  1. 对要发送的消息进行AES-CBC加密
  2. 生成安全签名
  3. 将消息密文和安全签名打包成xml格式

参数:
replyMsg - 公众平台待回复用户的消息,xml格式的字符串
timeStamp - 时间戳,可以自己生成,也可以用URL参数的timestamp
nonce - 随机串,可以自己生成,也可以用URL参数的nonce
返回:
加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串
抛出:
AesException - 执行失败,请查看该异常的错误码和具体的错误信息

decryptMsg

public String decryptMsg(String msgSignature,
                         String timeStamp,
                         String nonce,
                         String postData)
                  throws AesException
检验消息的真实性,并且获取解密后的明文.
  1. 利用收到的密文生成安全签名,进行签名验证
  2. 若验证通过,则提取xml中的加密消息
  3. 对消息进行解密

参数:
msgSignature - 签名串,对应URL参数的msg_signature
timeStamp - 时间戳,对应URL参数的timestamp
nonce - 随机串,对应URL参数的nonce
postData - 密文,对应POST请求的数据
返回:
解密后的原文
抛出:
AesException - 执行失败,请查看该异常的错误码和具体的错误信息

verifyUrl

public String verifyUrl(String msgSignature,
                        String timeStamp,
                        String nonce,
                        String echoStr)
                 throws AesException
验证URL

参数:
msgSignature - 签名串,对应URL参数的msg_signature
timeStamp - 时间戳,对应URL参数的timestamp
nonce - 随机串,对应URL参数的nonce
echoStr - 随机串,对应URL参数的echostr
返回:
解密之后的echostr
抛出:
AesException - 执行失败,请查看该异常的错误码和具体的错误信息


Copyright © 2017. All rights reserved.