{"record":{"id":"88fbd051a2ad23f3","repo":"TheAlgorithms/C-Sharp","slug":"key-must-be-non-empty-string","errorCode":null,"errorMessage":"key must be non-empty string","messagePattern":"key must be non-empty string","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Algorithms/Encoders/VigenereEncoder.cs","lineNumber":56,"sourceCode":"            if (!char.IsLetter(text[i]))\n            {\n                _ = encodedTextBuilder.Append(text[i]);\n                continue;\n            }\n\n            var letterZ = char.IsUpper(key[i]) ? 'Z' : 'z';\n            var encodedSymbol = symbolCipher(text[i].ToString(), letterZ - key[i]);\n            _ = encodedTextBuilder.Append(encodedSymbol);\n        }\n\n        return encodedTextBuilder.ToString();\n    }\n\n    private string AppendKey(string key, int length)\n    {\n        if (string.IsNullOrEmpty(key))\n        {\n            throw new ArgumentOutOfRangeException($\"{nameof(key)} must be non-empty string\");\n        }\n\n        var keyBuilder = new StringBuilder(key, length);\n        while (keyBuilder.Length < length)\n        {\n            _ = keyBuilder.Append(key);\n        }\n\n        return keyBuilder.ToString();\n    }\n}\n","sourceCodeStart":38,"sourceCodeEnd":68,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Encoders/VigenereEncoder.cs#L38-L68","documentation":"VigenereEncoder.AppendKey extends the key to the required cipher length by repetition, and rejects a null or empty key because repetition can never produce characters from nothing. An ArgumentOutOfRangeException with message \"key must be non-empty string\" is thrown when string.IsNullOrEmpty(key).","triggerScenarios":"Calling VigenereEncoder.Cipher (which calls AppendKey) with a null or \"\" key, e.g. Cipher(\"plaintext\", \"\") or Cipher(\"plaintext\", null) — throws ArgumentOutOfRangeException.","commonSituations":"Reading the key from configuration or an environment variable that is unset/empty; a UI or CLI passing an empty key field; deserializing a request where the key property defaulted to empty string.","solutions":["Supply a non-empty alphabetic key when calling VigenereEncoder.Cipher.","Validate the key at the configuration/request boundary before invoking the encoder.","Provide a fallback default key when configured key is missing, if acceptable for your use case.","Catch ArgumentOutOfRangeException around Cipher and surface a clear 'encryption key required' message to the user."],"exampleFix":"// before\nvar encoded = vigenere.Cipher(message, config.Key); // config.Key == \"\"\n// after\nif (string.IsNullOrEmpty(config.Key))\n    throw new InvalidOperationException(\"Encryption key must be configured (Vigenere:Key).\");\nvar encoded = vigenere.Cipher(message, config.Key);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(key))\n    throw new InvalidOperationException(\"A non-empty Vigenere key is required.\");","typeGuard":"static bool IsValidVigenereKey(string key) => !string.IsNullOrEmpty(key) && key.All(char.IsLetter);","tryCatchPattern":"try\n{\n    var encoded = vigenere.Cipher(message, key);\n}\ncatch (ArgumentOutOfRangeException ex)\n{\n    throw new InvalidOperationException(\"Encryption key is missing or empty; configure Vigenere:Key.\", ex);\n}","preventionTips":["Validate keys at startup/configuration load, not at encryption time.","Fail fast when env vars or config entries backing the key are unset.","Trim and check user/CLI input for the key field.","Add a startup sanity check that required crypto keys are non-empty."],"tags":["csharp","cryptography","empty-input","validation"],"backgroundTag":"empty-required-field","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"}