TheAlgorithms/C-Sharp · error · ArgumentOutOfRangeException
key must be non-empty string
Error message
key must be non-empty string
What it means
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).
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.
Example fix
// before
var encoded = vigenere.Cipher(message, config.Key); // config.Key == ""
// after
if (string.IsNullOrEmpty(config.Key))
throw new InvalidOperationException("Encryption key must be configured (Vigenere:Key).");
var encoded = vigenere.Cipher(message, config.Key); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(key))
throw new InvalidOperationException("A non-empty Vigenere key is required."); Type guard
static bool IsValidVigenereKey(string key) => !string.IsNullOrEmpty(key) && key.All(char.IsLetter);
Try / catch
try
{
var encoded = vigenere.Cipher(message, key);
}
catch (ArgumentOutOfRangeException ex)
{
throw new InvalidOperationException("Encryption key is missing or empty; configure Vigenere:Key.", ex);
} Prevention
- 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.
When it happens
Trigger: Calling VigenereEncoder.Cipher (which calls AppendKey) with a null or "" key, e.g. Cipher("plaintext", "") or Cipher("plaintext", null) — throws ArgumentOutOfRangeException.
Common situations: 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.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- No padding found.
- message
- Invalid block size
- Not enough space in input array for padding
- Input length must be a multiple of block size
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/88fbd051a2ad23f3.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Encoders/VigenereEncoder.cs:56
if (!char.IsLetter(text[i]))
{
_ = encodedTextBuilder.Append(text[i]);
continue;
}
var letterZ = char.IsUpper(key[i]) ? 'Z' : 'z';
var encodedSymbol = symbolCipher(text[i].ToString(), letterZ - key[i]);
_ = encodedTextBuilder.Append(encodedSymbol);
}
return encodedTextBuilder.ToString();
}
private string AppendKey(string key, int length)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentOutOfRangeException($"{nameof(key)} must be non-empty string");
}
var keyBuilder = new StringBuilder(key, length);
while (keyBuilder.Length < length)
{
_ = keyBuilder.Append(key);
}
return keyBuilder.ToString();
}
}
View on GitHub (pinned to 96e2905cab)