{"record":{"id":"0c551d516012a598","repo":"iflytek/astron-agent","slug":"40008-illegal-buffer-after-decryption","errorCode":"-40008","errorMessage":"Illegal buffer after decryption","messagePattern":"Illegal buffer after decryption","errorType":"error_code","errorClass":"AesException","httpStatus":null,"severity":"error","filePath":"console/backend/hub/src/main/java/com/iflytek/astron/console/hub/util/wechat/WXBizMsgCrypt.java","lineNumber":182,"sourceCode":"            throw new AesException(AesException.DecryptAESError);\n        }\n\n        String xmlContent, from_appid;\n        try {\n            // Remove padding\n            byte[] bytes = PKCS7Encoder.decode(original);\n\n            // Separate 16-bit random string, network byte order, and appId\n            byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20);\n\n            int xmlLength = recoverNetworkBytesOrder(networkOrder);\n\n            xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength), CHARSET);\n            from_appid = new String(Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length),\n                    CHARSET);\n        } catch (Exception e) {\n            e.printStackTrace();\n            throw new AesException(AesException.IllegalBuffer);\n        }\n\n        // Verify appid\n        if (!from_appid.equals(appId)) {\n            throw new AesException(AesException.ValidateAppidError);\n        }\n        return xmlContent;\n\n    }\n\n    /**\n     * Verify URL\n     *\n     * @param msgSignature Signature string\n     * @param timeStamp Timestamp\n     * @param nonce Random number\n     * @param echoStr Random string\n     * @return Decrypted echostr","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/console/backend/hub/src/main/java/com/iflytek/astron/console/hub/util/wechat/WXBizMsgCrypt.java#L164-L200","documentation":"After successful AES decryption, decrypt() unpacks the 16-byte random prefix, 4-byte message length, XML body, and receiving appid. Any exception while parsing these bytes (truncated plaintext, wrong padding producing nonsense lengths, charset issues) is wrapped into AesException(-40008, 'IllegalBuffer').","triggerScenarios":"The decrypted plaintext is shorter than 20 bytes or copyOfRange exceeds bounds because the wrong key produced garbage plaintext that fails PKCS7 padding, or the message body was truncated before decryption.","commonSituations":"A key mismatch that coincidentally decrypts (padding succeeds) but yields malformed bytes, cutting the callback body mid-way (read the full POST body), or a middleman re-encoding the ciphertext.","solutions":["Most often a symptom of the wrong encodingAesKey — verify it matches the sending app (same root cause as -40007)","Ensure the full encrypted POST body is read (use request.getInputStream fully, don't trust partial reads) and the Encrypt field extracted intact","Verify the signature (msg_signature) before decrypting to detect tampering","Check the stack trace from e.printStackTrace() for ArrayIndexOutOfBounds vs charset errors to distinguish truncation from key issues"],"exampleFix":"// before\nString encrypt = body.substring(body.indexOf(\"<Encrypt>\") + 9); // fragile slice\n// after\nDocument doc = parseXml(body);\nString encrypt = doc.getElementsByTagName(\"Encrypt\").item(0).getTextContent(); // intact ciphertext","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { String xml = crypt.decrypt(encrypt, signature, timestamp, nonce); } catch (AesException e) { if (e.getCode() == -40008) { log.warn(\"illegal buffer after decrypt for appId {} — verify key and full body read\", appId); } throw e; }","preventionTips":["Treat -40008 as a probable wrong-key symptom; fix the key first","Extract the Encrypt field via an XML parser, not string slicing","Verify the callback signature to rule out tampering"],"tags":["java","wechat","aes","parsing"],"backgroundTag":"aes-decrypt-failed","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}