TheAlgorithms/Python · error · ValueError

ciphertext is empty

Error message

ciphertext is empty

What it means

Raised by autokey decrypt() when the ciphertext is a str but empty (''). With no ciphertext there is nothing to decrypt and the key-extension loop would never run; the function rejects empty input explicitly rather than silently returning ''.

Source

Thrown at ciphers/autokey.py:109

    Traceback (most recent call last):
        ...
    TypeError: ciphertext must be a string
    >>> decrypt("", "TheAlgorithms")
    Traceback (most recent call last):
        ...
    ValueError: ciphertext is empty
    >>> decrypt("vvjfpk wj ohvp su ddylsv", 2)
    Traceback (most recent call last):
        ...
    TypeError: key must be a string
    """
    if not isinstance(ciphertext, str):
        raise TypeError("ciphertext must be a string")
    if not isinstance(key, str):
        raise TypeError("key must be a string")

    if not ciphertext:
        raise ValueError("ciphertext is empty")
    if not key:
        raise ValueError("key is empty")

    key = key.lower()
    ciphertext_iterator = 0
    key_iterator = 0
    plaintext = ""
    while ciphertext_iterator < len(ciphertext):
        if (
            ord(ciphertext[ciphertext_iterator]) < 97
            or ord(ciphertext[ciphertext_iterator]) > 122
        ):
            plaintext += ciphertext[ciphertext_iterator]
        else:
            plaintext += chr(
                (ord(ciphertext[ciphertext_iterator]) - ord(key[key_iterator])) % 26
                + 97
            )

View on GitHub (pinned to f5988cc097)

Solutions

  1. Short-circuit empty input in the caller: return '' immediately if not ciphertext.
  2. Filter empty entries before batch processing: [c for c in batch if c].
  3. Log and skip rather than letting the ValueError abort a whole batch.

Example fix

# before
decrypt('', 'TheAlgorithms')  # ValueError: ciphertext is empty

# after
plaintext = decrypt(ciphertext, 'TheAlgorithms') if ciphertext else ''
Defensive patterns

Strategy: validation

Validate before calling

if not ciphertext:
    return ''  # nothing to decrypt

Try / catch

try:
    decrypt(c, k)
except ValueError as e:
    if 'empty' in str(e):
        plaintext = ''
    else:
        raise

Prevention

When it happens

Trigger: Calling decrypt('', 'TheAlgorithms'). Type checks pass; only emptiness triggers the ValueError.

Common situations: Blank messages in test fixtures, reading past EOF into an empty string, or empty rows in a batch of encrypted records.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/7bd198ac3e1ed92c. Report an issue: GitHub.