Class WXBizJsonMsgCrypt

java.lang.Object
com.bytedesk.wechat.common.aes_json.WXBizJsonMsgCrypt

public class WXBizJsonMsgCrypt 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
      • receiveid

        String receiveid
    • Constructor Details

      • WXBizJsonMsgCrypt

        public WXBizJsonMsgCrypt(String token, String encodingAesKey, String receiveid) throws AesException
        构造函数
        Parameters:
        token - 企业微信后台,开发者设置的token
        encodingAesKey - 企业微信后台,开发者设置的EncodingAESKey
        receiveid - , 不同场景含义不同,详见文档
        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>
      • 将消息密文和安全签名打包成json格式</li> </ol>
      • Parameters:
        replyMsg - 企业微信待回复用户的消息,json格式的字符串
        timeStamp - 时间戳,可以自己生成,也可以用URL参数的timestamp
        nonce - 随机串,可以自己生成,也可以用URL参数的nonce
        Returns:
        加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的json格式的字符串
        Throws:
        AesException - 执行失败,请查看该异常的错误码和具体的错误信息
      • DecryptMsg

        public String DecryptMsg(String msgSignature, String timeStamp, String nonce, String postData) throws AesException
        检验消息的真实性,并且获取解密后的明文. <ol>
      • 利用收到的密文生成安全签名,进行签名验证</li>
      • 若验证通过,则提取json中的加密消息</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 - 执行失败,请查看该异常的错误码和具体的错误信息