为什么使用 JCE 无法正确解密前 N 个字符?

Why are the first N characters improperly decrypted using JCE?

当我解密一条消息时,有一定数量的字符未正确解密。例如:

解密前:{"header":{"messageId":"ca18d015-9861-47b8-9e09-81fb561522e5"

解密后:M��s����g��{��T\ageId":"ca18d015-9861-47b8-9e09-81fb561522e5"

我正在使用以下代码初始化 JCE:

SecretKeyFactory factory = SecretKeyFactory
        .getInstance(KEY_DERIVATION_ALGORITHM);
    SecretKey tmp = factory.generateSecret(new PBEKeySpec(new String(dataKey).toCharArray(), SALT,
                                                          PBKDF_DEFAULT_ITERATIONS, 128));

    Key aesKey = new SecretKeySpec(tmp.getEncoded(), KEY_TYPE);
    Cipher cipher = Cipher.getInstance(CIPHER);
    cipher.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(initializationVector));
    byte[] encrypted = cipher.doFinal(payload.getBytes());

初始化向量在 encrypting/decrypting 时不同。