{"record":{"id":"2fa01761156ee569","repo":"didi/DoKit","slug":"key-must-be-between-1-and-256-bytes","errorCode":null,"errorMessage":"key must be between 1 and 256 bytes","messagePattern":"key must be between 1 and 256 bytes","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/EncryptUtils.java","lineNumber":1145,"sourceCode":"            } else {\n                return cipher.doFinal(data);\n            }\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n        return null;\n    }\n\n    /**\n     * Return the bytes of RC4 encryption/decryption.\n     *\n     * @param data The data.\n     * @param key  The key.\n     */\n    public static byte[] rc4(byte[] data, byte[] key) {\n        if (data == null || data.length == 0 || key == null) return null;\n        if (key.length < 1 || key.length > 256) {\n            throw new IllegalArgumentException(\"key must be between 1 and 256 bytes\");\n        }\n        final byte[] iS = new byte[256];\n        final byte[] iK = new byte[256];\n        int keyLen = key.length;\n        for (int i = 0; i < 256; i++) {\n            iS[i] = (byte) i;\n            iK[i] = key[i % keyLen];\n        }\n        int j = 0;\n        byte tmp;\n        for (int i = 0; i < 256; i++) {\n            j = (j + iS[i] + iK[i]) & 0xFF;\n            tmp = iS[j];\n            iS[j] = iS[i];\n            iS[i] = tmp;\n        }\n\n        final byte[] ret = new byte[data.length];","sourceCodeStart":1127,"sourceCodeEnd":1163,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/EncryptUtils.java#L1127-L1163","documentation":"EncryptUtils.rc4 implements the RC4 stream cipher, whose key scheduling algorithm builds two 256-entry tables by repeating the key (iK[i] = key[i % keyLen]). The algorithm requires a 1..256 byte key; an empty key would divide by zero in the modulo and a >256 key is outside the spec, so both are rejected.","triggerScenarios":"Passing an empty byte[] key (data non-empty, key length 0); passing a key longer than 256 bytes such as a raw RSA/EC public key or an X.509 certificate blob instead of a symmetric secret; null keys return null earlier and do not throw.","commonSituations":"Loading keys from misconfigured files/assets that resolve to empty arrays; using a hex/base64 string as key without decoding so its byte length balloons past 256; key-generation code that failed and returned an empty array.","solutions":["Verify 1 <= key.length <= 256 before calling; log the actual length when it fails.","If the key comes from encoded text, decode first (e.g. Base64.decode) rather than using getBytes() of the encoded form.","For empty keys, fail at configuration/load time with a clear error instead of reaching the cipher."],"exampleFix":"// before\nbyte[] key = readFileOrEmpty(\"rc4.key\"); // may be byte[0]\nbyte[] out = EncryptUtils.rc4(data, key); // throws\n\n// after\nbyte[] key = readFile(\"rc4.key\");\nif (key == null || key.length < 1 || key.length > 256) throw new IllegalStateException(\"bad RC4 key\");\nbyte[] out = EncryptUtils.rc4(data, key);","handlingStrategy":"validation","validationCode":"boolean okKey = key != null && key.length >= 1 && key.length <= 256;\nbyte[] out = okKey ? EncryptUtils.rc4(data, key) : null;","typeGuard":null,"tryCatchPattern":"try { out = EncryptUtils.rc4(data, key); } catch (IllegalArgumentException e) { throw new SecurityException(\"Invalid RC4 key length\", e); }","preventionTips":["Fail at key-load time on empty or oversized keys with a clear message.","Decode base64/hex key material before use; never pass encoded strings' raw bytes blindly.","Prefer modern AES-GCM over RC4 for new code."],"tags":["encryption","rc4","validation"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}