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
- Short-circuit empty input in the caller: return '' immediately if not ciphertext.
- Filter empty entries before batch processing: [c for c in batch if c].
- 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
- Skip empty records in batch decryption loops.
- Test fixtures should include an empty-string case to lock in your chosen policy.
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
- plain must contain only lowercase letters (a-z)
- plaintext is empty
- key is empty
- encode() accepts only letters of the alphabet and spaces
- decode() accepts only 'A', 'B' and spaces
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/7bd198ac3e1ed92c.
Report an issue: GitHub.