/** * 将公众平台回复用户的消息加密打包. * * <ol> * <li>对要发送的消息进行AES-CBC加密 * <li>生成安全签名 * <li>将消息密文和安全签名打包成xml格式 * </ol> * * @param replyMsg 公众平台待回复用户的消息,xml格式的字符串 * @param timeStamp 时间戳,可以自己生成,也可以用URL参数的timestamp * @param nonce 随机串,可以自己生成,也可以用URL参数的nonce * @return 加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串 * @throws AesException 执行失败,请查看该异常的错误码和具体的错误信息 */ public String encryptMsg(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()); // 生成发送的xml String result = XMLParse.generate(encrypt, signature, timeStamp, nonce); return result; }
/** * 检验消息的真实性,并且获取解密后的明文. * * <ol> * <li>利用收到的密文生成安全签名,进行签名验证 * <li>若验证通过,则提取xml中的加密消息 * <li>对消息进行解密 * </ol> * * @param msgSignature 签名串,对应URL参数的msg_signature * @param timeStamp 时间戳,对应URL参数的timestamp * @param nonce 随机串,对应URL参数的nonce * @param postData 密文,对应POST请求的数据 * @return 解密后的原文 * @throws AesException 执行失败,请查看该异常的错误码和具体的错误信息 */ public String decryptMsg(String msgSignature, String timeStamp, String nonce, String postData) throws AesException { // 密钥,公众账号的app secret // 提取密文 Object[] encrypt = XMLParse.extract(postData); // 验证安全签名 String signature = SHA1.getSHA1(token, timeStamp, nonce, encrypt[1].toString()); // 和URL中的签名比较是否相等 // System.out.println("第三方收到URL中的签名:" + msg_sign); // System.out.println("第三方校验签名:" + signature); if (!signature.equals(msgSignature)) { throw new AesException(AesException.ValidateSignatureError); } // 解密 String result = decrypt(encrypt[1].toString()); return result; }
public ArrayList<String> getCommandType(String command) throws IOException, InterruptedException { XMLParse xP = new XMLParse(); xP.xMLParser("/invXMLs/commands"); ArrayList<String> typeOfCommand = new ArrayList<String>(); for (int x = 0; x < xP.getTMMat().size(); x++) { try { if (typeOfCommand.isEmpty() == false) { break; } } catch (NullPointerException npe) { } if (xP.getTStrD().contains(command)) { if (xP.getTStrD().get(x).equalsIgnoreCase(command)) { try { if (typeOfCommand.isEmpty() == false) { break; } } catch (NullPointerException npe) { } int x2 = x; if (xP.getTStrD().get(xP.getTMMat().get(x2)).equalsIgnoreCase(command)) { } else { do { x2++; } while ((xP.getTStrD().get(xP.getTMMat().get(x2)).equalsIgnoreCase(command) == false)); } int y = x2; do { if (xP.getMarkerID().get(y).equalsIgnoreCase("type")) { try { if (xP.getMarkerData().get(y).equals(null)) { break; } else { if (xP.getMarkerData().get(y).contains("_")) { StringTokenizer st = new StringTokenizer(xP.getMarkerData().get((y))); while (st.hasMoreTokens()) { try { typeOfCommand.add(st.nextToken("_")); } catch (NullPointerException npe) { break; } } } else { typeOfCommand.add(xP.getMarkerData().get((y))); } } } catch (NullPointerException npe) { break; } } try { if (typeOfCommand.isEmpty() == false) { break; } } catch (NullPointerException npe) { } y++; } while (xP.getTStrD().get(xP.getTMMat().get(y)).equalsIgnoreCase(command)); } } else { break; } } return typeOfCommand; }
public String setToLinkingToUse(ArrayList<String> commandTypes, ArrayList<String> toLinkings) throws IOException, InterruptedException { RanGen rG = new RanGen(); XMLParse xP = new XMLParse(); xP.xMLParser("/invXMLs/toTarLinking"); ArrayList<String> useableLinkings = new ArrayList<String>(); String linkingToUse = null; for (int x = 0; x < xP.getTStrD().size(); x++) { for (int y = 0; y < toLinkings.size(); y++) { if (xP.getTStrD().get(x).equalsIgnoreCase(toLinkings.get(y))) { int x2 = x; while (xP.getTStrD().get(xP.getTMMat().get(x2)).equalsIgnoreCase(toLinkings.get(y)) == false) { x2++; } int z = x2; do { if (xP.getMarkerID().get(z).equalsIgnoreCase("type")) { if (xP.getMarkerData().get(z).contains("_")) { StringTokenizer st = new StringTokenizer(xP.getMarkerData().get(z)); String toCheck; while (st.hasMoreTokens()) { toCheck = st.nextToken("_"); if (commandTypes.contains(toCheck)) { useableLinkings.add(toLinkings.get(y)); } try { if (st.hasMoreTokens() == false) { break; } } catch (Exception e) { break; } } } else { if (commandTypes.contains(xP.getMarkerData().get(z))) { useableLinkings.add(toLinkings.get(y)); } } } z++; } while (xP.getTStrD().get(xP.getTMMat().get(z)).equalsIgnoreCase(toLinkings.get(y))); } } } if (useableLinkings.size() > 1) { rG.setRanNum(useableLinkings.size()); linkingToUse = useableLinkings.get(rG.getRanNum()); } else { linkingToUse = useableLinkings.get(0); } return linkingToUse; }