{"record":{"id":"45d7934a87c13321","repo":"nilaoda/N_m3u8DL-RE","slug":"key-length-must-be-allowedkeylength-actual-key-length","errorCode":null,"errorMessage":"Key length must be {allowedKeyLength}. Actual: {key.Length}","messagePattern":"Key length must be (.+?)\\. Actual: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/N_m3u8DL-RE/Crypto/CSChaCha20.cs","lineNumber":130,"sourceCode":"        private static readonly byte[] sigma = Encoding.ASCII.GetBytes(\"expand 32-byte k\");\n        private static readonly byte[] tau = Encoding.ASCII.GetBytes(\"expand 16-byte k\");\n\n        /// <summary>\n        /// Set up the ChaCha state with the given key. A 32-byte key is required and enforced.\n        /// </summary>\n        /// <param name=\"key\">\n        /// A 32-byte (256-bit) key, treated as a concatenation of eight 32-bit little-endian integers\n        /// </param>\n        private void KeySetup(byte[] key)\n        {\n            if (key == null)\n            {\n                throw new ArgumentNullException(\"Key is null\");\n            }\n\n            if (key.Length != allowedKeyLength)\n            {\n                throw new ArgumentException($\"Key length must be {allowedKeyLength}. Actual: {key.Length}\");\n            }\n\n            state[4] = Util.U8To32Little(key, 0);\n            state[5] = Util.U8To32Little(key, 4);\n            state[6] = Util.U8To32Little(key, 8);\n            state[7] = Util.U8To32Little(key, 12);\n\n            byte[] constants = (key.Length == allowedKeyLength) ? sigma : tau;\n            int keyIndex = key.Length - 16;\n\n            state[8] = Util.U8To32Little(key, keyIndex + 0);\n            state[9] = Util.U8To32Little(key, keyIndex + 4);\n            state[10] = Util.U8To32Little(key, keyIndex + 8);\n            state[11] = Util.U8To32Little(key, keyIndex + 12);\n\n            state[0] = Util.U8To32Little(constants, 0);\n            state[1] = Util.U8To32Little(constants, 4);\n            state[2] = Util.U8To32Little(constants, 8);","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/nilaoda/N_m3u8DL-RE/blob/e113dee70c924ee08dae5460624909b04d84cb76/src/N_m3u8DL-RE/Crypto/CSChaCha20.cs#L112-L148","documentation":"CSChaCha20 only accepts a 256-bit key. KeySetup compares key.Length against allowedKeyLength (32) and throws ArgumentException(\"Key length must be 32. Actual: N\") on any other size, because the state initialization reads exactly 32 bytes in 8 little-endian words.","triggerScenarios":"Passing a 16-byte (AES-128) key, a hex string decoded to the wrong byte count, or a key buffer with a length prefix/BOM included to the ChaCha20 constructor.","commonSituations":"Users supply 16-byte SAMPLE-AES keys meant for AES-CTR; Base64/hex decode helpers produce padded or truncated buffers; key material read from a file includes trailing newline bytes.","solutions":["Ensure the key is exactly 32 bytes (64 hex chars) before constructing the cipher.","Trim whitespace/newlines from decoded key material.","If the source key is 16 bytes, confirm the encryption actually uses ChaCha20-256; otherwise use the matching cipher.","Add an upfront length assertion where keys are parsed."],"exampleFix":"// before\nvar key = Convert.FromHexString(hexKey); // may be 16 bytes\n// after\nif (key.Length != 32) Array.Resize(ref key, 32); // better: validate & reject\nvar cipher = new CSChaCha20(key, nonce, 0);","handlingStrategy":"validation","validationCode":"if (keyBytes == null || keyBytes.Length != 32) throw new InvalidOperationException($\"ChaCha20 key must be 32 bytes, got {keyBytes?.Length ?? 0}\");","typeGuard":"bool IsChaCha20Key(byte[] k) => k is { Length: 32 };","tryCatchPattern":"try { cipher = new CSChaCha20(key, nonce, 0); }\ncatch (ArgumentException ex) { Console.Error.WriteLine($\"Bad key: {ex.Message}\"); }","preventionTips":["Decode keys to byte[] and assert Length == 32 before use.","Trim whitespace/newlines from decoded key material.","Route 16-byte AES keys to the AES path, not ChaCha20."],"tags":["crypto","chacha20","key-length"],"backgroundTag":"invalid-argument-value","analyzedSha":"e113dee70c924ee08dae5460624909b04d84cb76","analyzedAt":"2026-09-13T02:43:21.078Z","contentChangedAt":"2026-09-13T02:43:21.078Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}