WXBizMsgCrypt { String encrypt(String randomStr, String text) throws AesException { ByteGroup byteCollector = new ByteGroup(); byte[] randomStrBytes = randomStr.getBytes(CHARSET); byte[] textBytes = text.getBytes(CHARSET); byte[] networkBytesOrder = getNetworkBytesOrder(textBytes.length); byte[] corpidBytes = corpId.getBytes(CHARSET); byteCollector.addBytes(randomStrBytes); byteCollector.addBytes(networkBytesOrder); byteCollector.addBytes(textBytes); byteCollector.addBytes(corpidBytes); byte[] padBytes = PKCS7Encoder.encode(byteCollector.size()); byteCollector.addBytes(padBytes); byte[] unencrypted = byteCollector.toBytes(); try { Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES"); IvParameterSpec iv = new IvParameterSpec(aesKey, 0, 16); cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); byte[] encrypted = cipher.doFinal(unencrypted); String base64Encrypted = base64.encodeToString(encrypted); return base64Encrypted; } catch (Exception e) { e.printStackTrace(); throw new AesException(AesException.EncryptAESError); } } WXBizMsgCrypt(String token, String encodingAesKey, String CorpId); String EncryptMsg(String replyMsg, String timeStamp, String nonce); String DecryptMsg(String msgSignature, String timeStamp, String nonce, String postData); String VerifyURL(String msgSignature, String timeStamp, String nonce, String echoStr);  }
@Test public void testAesEncrypt() { try { WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, corpId); assertEquals(afterAesEncrypt, pc.encrypt(randomStr, replyMsg)); } catch (AesException e) { e.printStackTrace(); fail("no异常"); } }
@Test public void testAesEncrypt2() { try { WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, corpId); assertEquals(afterAesEncrypt2, pc.encrypt(randomStr, replyMsg2)); } catch (AesException e) { e.printStackTrace(); fail("no异常"); } }