{"record":{"id":"a8462d292535a33c","repo":"paascloud/paascloud-master","slug":"error-a8462d","errorCode":null,"errorMessage":"加密失败","messagePattern":"加密失败","errorType":"exception","errorClass":"HttpAesException","httpStatus":null,"severity":"error","filePath":"paascloud-common/paascloud-common-util/src/main/java/com/paascloud/HttpAesUtil.java","lineNumber":67,"sourceCode":"\t\t\tbyte[] content = contentParam.getBytes(CHAR_SET);\n\t\t\tbyte[] key = keyParam.getBytes(CHAR_SET);\n\t\t\tbyte[] iv = ivParam.getBytes(CHAR_SET);\n\n\t\t\tif (md5Key) {\n\t\t\t\tMessageDigest md = MessageDigest.getInstance(\"MD5\");\n\t\t\t\tkey = md.digest(key);\n\t\t\t}\n\t\t\tSecretKeySpec skeySpec = new SecretKeySpec(key, \"AES\");\n\t\t\t//\"算法/模式/补码方式\"\n\t\t\tCipher cipher = Cipher.getInstance(\"AES/CBC/ISO10126Padding\");\n\t\t\t//使用CBC模式, 需要一个向量iv, 可增加加密算法的强度\n\t\t\tIvParameterSpec ivps = new IvParameterSpec(iv);\n\t\t\tcipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivps);\n\t\t\tbyte[] bytes = cipher.doFinal(content);\n\t\t\treturn new BASE64Encoder().encode(bytes);\n\t\t} catch (Exception ex) {\n\t\t\tlog.error(\"加密密码失败\", ex);\n\t\t\tthrow new HttpAesException(\"加密失败\");\n\t\t}\n\t}\n\n\t/**\n\t * 解密\n\t *\n\t * @param contentParam 需要加密的内容\n\t * @param keyParam     加密密码\n\t * @param md5Key       是否对key进行md5加密\n\t * @param ivParam      加密向量\n\t *\n\t * @return string\n\t */\n\tpublic static String decrypt(String contentParam, String keyParam, boolean md5Key, String ivParam) {\n\t\ttry {\n\t\t\tif (PubUtils.isNull(contentParam, keyParam, md5Key, ivParam)) {\n\t\t\t\treturn \"\";\n\t\t\t}","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/paascloud/paascloud-master/blob/781281a9503332ed3cef44ea618349d14230a127/paascloud-common/paascloud-common-util/src/main/java/com/paascloud/HttpAesUtil.java#L49-L85","documentation":"HttpAesUtil.encrypt wraps the AES encryption step in a broad catch and rethrows HttpAesException(\"加密失败\") (\"encryption failed\") for any Exception during cipher init or doFinal. Common underlying causes are a wrong key size for the AES transformation, an invalid IV length, or the JCE policy restricting key lengths.","triggerScenarios":"Calling HttpAesUtil.encrypt(content, key, iv) where the key is not 16/24/32 bytes for the configured AES mode, the IV is not 16 bytes, the content is empty/incompatible, or the JDK lacks the algorithm/ UnlimitedStrength policy.","commonSituations":"Config keys shorter than 16 characters, IV strings of the wrong length after a config change, mismatched key between environments, or older JDK 8 builds without unlimited-strength JCE for AES-256.","solutions":["Verify the key is exactly 16/24/32 bytes and the IV exactly 16 bytes for AES/CBC/PKCS5Padding.","Check the log entry logged as \"加密密码失败\" to see the underlying CryptographicException before the rethrow.","Confirm both sides use the same key/IV/mode; sync configs across environments.","On older JDKs, install the JCE Unlimited Strength policy files for AES-256."],"exampleFix":"// before\nString key = \"shortkey\"; // 8 chars -> InvalidKeyException -> 加密失败\n// after\nString key = \"1234567890abcdef\"; // 16 bytes for AES-128","handlingStrategy":"try-catch","validationCode":"if (key == null || !(key.length() == 16 || key.length() == 24 || key.length() == 32)) {\n    throw new IllegalArgumentException(\"AES key must be 16/24/32 bytes\");\n}\nif (iv == null || iv.length != 16) {\n    throw new IllegalArgumentException(\"AES IV must be 16 bytes\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    String encrypted = HttpAesUtil.encrypt(content, key, iv);\n} catch (HttpAesException e) {\n    log.error(\"AES encryption failed; check key/iv length and JCE policy\", e);\n}","preventionTips":["Validate key (16/24/32 bytes) and IV (16 bytes) lengths at config load time.","Keep encryption keys in sync across environments via a secret manager.","Install JCE Unlimited Strength policy files on older JDK 8 installs for AES-256.","Log and inspect the cause inside encrypt's catch block when diagnosing."],"tags":["encryption","aes","crypto","key-size"],"backgroundTag":"invalid-config-value","analyzedSha":"781281a9503332ed3cef44ea618349d14230a127","analyzedAt":"2026-09-10T10:59:02.070Z","contentChangedAt":"2026-09-10T10:59:02.070Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}