Class WXBizMsgCrypt

java.lang.Object
com.bytedesk.wechat.common.aes_xml.WXBizMsgCrypt

public class WXBizMsgCrypt extends Object
提供接收和推送给公众平台消息的加解密接口(UTF8编码的字符串). <ol&amp;gt;
  • 第三方回复加密消息给公众平台</li>
  • 第三方收到公众平台发送的消息,验证消息的安全性,并对消息进行解密。</li> </ol> 说明:异常java.security.InvalidKeyException:illegal Key Size的解决方案 <ol>
  • 在官方网站下载JCE无限制权限策略文件(JDK7的下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html</li&amp;gt;
  • 下载后解压,可以看到local_policy.jar和US_export_policy.jar以及readme.txt</li>
  • 如果安装了JRE,将两个jar文件放到%JRE_HOME%\lib\security目录下覆盖原来的文件</li>
  • 如果安装了JDK,将两个jar文件放到%JDK_HOME%\jre\lib\security目录下覆盖原来文件</li> </ol>
    • Field Details

      • CHARSET

        static Charset CHARSET
      • base64

        org.apache.commons.codec.binary.Base64 base64
      • aesKey

        byte[] aesKey
      • token

        String token
      • appId

        String appId
    • Constructor Details

      • WXBizMsgCrypt

        public WXBizMsgCrypt(String token, String encodingAesKey, String appId) throws AesException
        构造函数
        Parameters:
        token - 公众平台上,开发者设置的token
        encodingAesKey - 公众平台上,开发者设置的EncodingAESKey
        appId - 公众平台appid
        Throws:
        AesException - 执行失败,请查看该异常的错误码和具体的错误信息
    • Method Details

      • getNetworkBytesOrder

        byte[] getNetworkBytesOrder(int sourceNumber)
      • recoverNetworkBytesOrder

        int recoverNetworkBytesOrder(byte[] orderBytes)
      • getRandomStr

        String getRandomStr()
      • encrypt

        String encrypt(String randomStr, String text) throws AesException
        对明文进行加密.
        Parameters:
        text - 需要加密的明文
        Returns:
        加密后base64编码的字符串
        Throws:
        AesException - aes加密失败
      • decrypt

        String decrypt(String text) throws AesException
        对密文进行解密.
        Parameters:
        text - 需要解密的密文
        Returns:
        解密得到的明文
        Throws:
        AesException - aes解密失败
      • encryptMsg

        public String encryptMsg(String replyMsg, String timeStamp, String nonce) throws AesException
        将公众平台回复用户的消息加密打包. <ol>
      • 对要发送的消息进行AES-CBC加密</li>
      • 生成安全签名</li>
      • 将消息密文和安全签名打包成xml格式</li> </ol>
      • Parameters:
        replyMsg - 公众平台待回复用户的消息,xml格式的字符串
        timeStamp - 时间戳,可以自己生成,也可以用URL参数的timestamp
        nonce - 随机串,可以自己生成,也可以用URL参数的nonce
        Returns:
        加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串
        Throws:
        AesException - 执行失败,请查看该异常的错误码和具体的错误信息
      • decryptMsg

        public String decryptMsg(String msgSignature, String timeStamp, String nonce, String postData) throws AesException
        检验消息的真实性,并且获取解密后的明文. <ol>
      • 利用收到的密文生成安全签名,进行签名验证</li>
      • 若验证通过,则提取xml中的加密消息</li>
      • 对消息进行解密</li> </ol>
      • Parameters:
        msgSignature - 签名串,对应URL参数的msg_signature
        timeStamp - 时间戳,对应URL参数的timestamp
        nonce - 随机串,对应URL参数的nonce
        postData - 密文,对应POST请求的数据
        Returns:
        解密后的原文
        Throws:
        AesException - 执行失败,请查看该异常的错误码和具体的错误信息
      • verifyUrl

        public String verifyUrl(String msgSignature, String timeStamp, String nonce, String echoStr) throws AesException
        验证URL
        Parameters:
        msgSignature - 签名串,对应URL参数的msg_signature
        timeStamp - 时间戳,对应URL参数的timestamp
        nonce - 随机串,对应URL参数的nonce
        echoStr - 随机串,对应URL参数的echostr
        Returns:
        解密之后的echostr
        Throws:
        AesException - 执行失败,请查看该异常的错误码和具体的错误信息