{"record":{"id":"27af3c4726bb8680","repo":"TheAlgorithms/C-Sharp","slug":"the-length-of-key-should-be-divisible-by-16","errorCode":null,"errorMessage":"The length of key should be divisible by 16","messagePattern":"The length of key should be divisible by 16","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Encoders/FeistelCipher.cs","lineNumber":72,"sourceCode":"        }\n\n        return encodedText.ToString();\n    }\n\n    /// <summary>\n    ///     Decodes text that was encoded using specified key.\n    /// </summary>\n    /// <param name=\"text\">Text to be decoded.</param>\n    /// <param name=\"key\">Key that was used to encode the text.</param>\n    /// <exception cref=\"ArgumentException\">Error: key should be more than 0x00001111 for better encoding, key=0 will throw DivideByZero exception.</exception>\n    /// <exception cref=\"ArgumentException\">Error: The length of text should be divisible by 16 as it the block lenght is 16 bytes.</exception>\n    /// <returns>Decoded text.</returns>\n    public string Decode(string text, uint key)\n    {\n        // The plain text will be padded to fill the size of block (16 bytes)\n        if (text.Length % 16 != 0)\n        {\n            throw new ArgumentException($\"The length of {nameof(key)} should be divisible by 16\");\n        }\n\n        List<ulong> blocksListEncoded = GetBlocksFromEncodedText(text);\n        StringBuilder decodedTextHex = new();\n\n        foreach (ulong block in blocksListEncoded)\n        {\n            uint temp = 0;\n\n            // decompose a block to two subblocks 0x0123456789ABCDEF => 0x01234567 & 0x89ABCDEF\n            uint rightSubblock = (uint)(block & 0x00000000FFFFFFFF);\n            uint leftSubblock = (uint)(block >> 32);\n\n            // Feistel \"network\" - decoding, the order of rounds and operations on the blocks is reverted\n            uint roundKey;\n            for (int round = Rounds - 1; round >= 0; round--)\n            {\n                roundKey = GetRoundKey(key, round);","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Encoders/FeistelCipher.cs#L54-L90","documentation":"FeistelCipher.Decode requires the input text length to be a multiple of the 16-byte block size, since the ciphertext is split into fixed 16-byte blocks. If text.Length % 16 != 0, an ArgumentException is thrown noting the length must be divisible by 16 (the message text mentions `key` due to a nameof bug, but it is the text length that is wrong).","triggerScenarios":"Calling FeistelCipher.Decode with a string whose length is not a multiple of 16, e.g. Decode(\"short\", key) — 5 % 16 != 0 — throws. Also happens when encoded text was truncated or extra characters (whitespace, line breaks) were added in transport, as exercised by TestEncodedMessageSize/decoded.","commonSituations":"Copy-pasting ciphertext through systems that trimmed or wrapped it (email, logs, JSON), dropping characters; manually editing encoded output; decoding data encrypted by a different version or padding mode; confusing hex/encoded length with raw block size.","solutions":["Ensure Decode receives exactly the string produced by Encode — do not trim, truncate, or append characters.","Verify text.Length % 16 == 0 before calling Decode and fix the data source if not.","Strip transport-added whitespace/newlines from the encoded text before decoding.","Re-encode the plaintext with FeistelCipher.Encode to get properly padded ciphertext, then decode that."],"exampleFix":"// before\ncipher.Decode(corruptedCipherText, key); // length not multiple of 16\n// after\nif (corruptedCipherText.Length % 16 != 0)\n    throw new InvalidOperationException(\"Ciphertext was truncated; re-encode the original text.\");\nvar plain = cipher.Decode(corruptedCipherText, key);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(text) || text.Length % 16 != 0)\n    throw new ArgumentException(\"Ciphertext must be non-empty and its length a multiple of 16.\");","typeGuard":"static bool IsBlockAligned(string text) => text != null && text.Length % 16 == 0;","tryCatchPattern":"try\n{\n    var plain = feistel.Decode(cipherText, key);\n}\ncatch (ArgumentException ex)\n{\n    // ciphertext corrupted/truncated: re-obtain or re-encode the data\n    throw new InvalidOperationException(\"Ciphertext is not 16-byte block aligned; re-encode the source data.\", ex);\n}","preventionTips":["Never manually edit or hand-trim encoded ciphertext.","Transfer ciphertext through length-preserving, whitespace-safe channels (base64 wrapper).","Check text.Length % 16 == 0 before decoding.","Round-trip test (encode then decode) in CI to catch padding regressions."],"tags":["csharp","cryptography","block-size","input-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}