paascloud/paascloud-master · error · HttpAesException

加密失败

Error message

加密失败

What it means

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.

Solutions

  1. Verify the key is exactly 16/24/32 bytes and the IV exactly 16 bytes for AES/CBC/PKCS5Padding.
  2. Check the log entry logged as "加密密码失败" to see the underlying CryptographicException before the rethrow.
  3. Confirm both sides use the same key/IV/mode; sync configs across environments.
  4. On older JDKs, install the JCE Unlimited Strength policy files for AES-256.

Example fix

// before
String key = "shortkey"; // 8 chars -> InvalidKeyException -> 加密失败
// after
String key = "1234567890abcdef"; // 16 bytes for AES-128
Defensive patterns

Strategy: try-catch

Validate before calling

if (key == null || !(key.length() == 16 || key.length() == 24 || key.length() == 32)) {
    throw new IllegalArgumentException("AES key must be 16/24/32 bytes");
}
if (iv == null || iv.length != 16) {
    throw new IllegalArgumentException("AES IV must be 16 bytes");
}

Try / catch

try {
    String encrypted = HttpAesUtil.encrypt(content, key, iv);
} catch (HttpAesException e) {
    log.error("AES encryption failed; check key/iv length and JCE policy", e);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of paascloud/paascloud-master@781281a950 (2026-09-10). Data as JSON: /api/errors/a8462d292535a33c. Report an issue: GitHub.

Appendix: source

Thrown at paascloud-common/paascloud-common-util/src/main/java/com/paascloud/HttpAesUtil.java:67

			byte[] content = contentParam.getBytes(CHAR_SET);
			byte[] key = keyParam.getBytes(CHAR_SET);
			byte[] iv = ivParam.getBytes(CHAR_SET);

			if (md5Key) {
				MessageDigest md = MessageDigest.getInstance("MD5");
				key = md.digest(key);
			}
			SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
			//"算法/模式/补码方式"
			Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding");
			//使用CBC模式, 需要一个向量iv, 可增加加密算法的强度
			IvParameterSpec ivps = new IvParameterSpec(iv);
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivps);
			byte[] bytes = cipher.doFinal(content);
			return new BASE64Encoder().encode(bytes);
		} catch (Exception ex) {
			log.error("加密密码失败", ex);
			throw new HttpAesException("加密失败");
		}
	}

	/**
	 * 解密
	 *
	 * @param contentParam 需要加密的内容
	 * @param keyParam     加密密码
	 * @param md5Key       是否对key进行md5加密
	 * @param ivParam      加密向量
	 *
	 * @return string
	 */
	public static String decrypt(String contentParam, String keyParam, boolean md5Key, String ivParam) {
		try {
			if (PubUtils.isNull(contentParam, keyParam, md5Key, ivParam)) {
				return "";
			}

View on GitHub (pinned to 781281a950)