| | |
| | | import javax.crypto.spec.IvParameterSpec; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import org.apache.commons.codec.binary.Base64; |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 将企业微信智能机器人回复用户的消息加密打包. |
| | | * <ol> |
| | | * <li>对要发送的消息进行AES-CBC加密</li> |
| | | * <li>生成安全签名</li> |
| | | * <li>将消息密文和安全签名打包成json格式</li> |
| | | * </ol> |
| | | * |
| | | * @param replyMsg 企业微信待回复用户的消息,json格式的字符串 |
| | | * @param timeStamp 时间戳,可以自己生成,也可以用URL参数的timestamp |
| | | * @param nonce 随机串,可以自己生成,也可以用URL参数的nonce |
| | | * |
| | | * @return 加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的json格式的字符串 |
| | | * @throws AesException 执行失败,请查看该异常的错误码和具体的错误信息 |
| | | */ |
| | | public JSONObject EncryptRobotJsonMsg(String replyMsg, String timeStamp, String nonce) throws AesException { |
| | | // 加密 |
| | | String encrypt = encrypt(getRandomStr(), replyMsg); |
| | | |
| | | // 生成安全签名 |
| | | if (timeStamp == "") { |
| | | timeStamp = Long.toString(System.currentTimeMillis()); |
| | | } |
| | | |
| | | String signature = SHA1.getSHA1(token, timeStamp, nonce, encrypt); |
| | | |
| | | // System.out.println("发送给平台的签名是: " + signature[1].toString()); |
| | | // 生成发送的json''' |
| | | JSONObject json = new JSONObject(); |
| | | json.put("encrypt", encrypt); |
| | | json.put("msgsignature", signature); |
| | | json.put("timestamp", timeStamp); |
| | | json.put("nonce", nonce); |
| | | return json; |
| | | } |
| | | |
| | | /** |
| | | * 检验智能机器人消息的真实性,并且获取解密后的明文. |
| | | * <ol> |
| | | * <li>利用收到的密文生成安全签名,进行签名验证</li> |