{"record":{"id":"30cdea31ea3fd639","repo":"TheAlgorithms/Python","slug":"key-is-empty","errorCode":null,"errorMessage":"key is empty","messagePattern":"key is empty","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ciphers/autokey.py","lineNumber":46,"sourceCode":"    ValueError: plaintext is empty\n    >>> encrypt(\"coffee is good as python\", \"\")\n    Traceback (most recent call last):\n        ...\n    ValueError: key is empty\n    >>> encrypt(527.26, \"TheAlgorithms\")\n    Traceback (most recent call last):\n        ...\n    TypeError: plaintext must be a string\n    \"\"\"\n    if not isinstance(plaintext, str):\n        raise TypeError(\"plaintext must be a string\")\n    if not isinstance(key, str):\n        raise TypeError(\"key must be a string\")\n\n    if not plaintext:\n        raise ValueError(\"plaintext is empty\")\n    if not key:\n        raise ValueError(\"key is empty\")\n\n    key += plaintext\n    plaintext = plaintext.lower()\n    key = key.lower()\n    plaintext_iterator = 0\n    key_iterator = 0\n    ciphertext = \"\"\n    while plaintext_iterator < len(plaintext):\n        if (\n            ord(plaintext[plaintext_iterator]) < 97\n            or ord(plaintext[plaintext_iterator]) > 122\n        ):\n            ciphertext += plaintext[plaintext_iterator]\n            plaintext_iterator += 1\n        elif ord(key[key_iterator]) < 97 or ord(key[key_iterator]) > 122:\n            key_iterator += 1\n        else:\n            ciphertext += chr(","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/autokey.py#L28-L64","documentation":"Raised by autokey encrypt() when the key is a str but empty (''). The autokey cipher extends the key with the plaintext itself (key += plaintext), and an empty starting key would make the first len(key)==0 characters unencryptable, so it is rejected.","triggerScenarios":"Calling encrypt('coffee is good as python', '') — the key argument is an empty string while plaintext is non-empty.","commonSituations":"Optional key parameters defaulting to '' or None-then-'' normalization, or config files with a blank key entry.","solutions":["Provide a non-empty keyword: encrypt(text, 'TheAlgorithms').","Default missing keys to a real value or raise your own config error before calling encrypt.","Sanitize config: key = key.strip(); if not key: raise ValueError('missing key')."],"exampleFix":"# before\nencrypt('hello', '')  # ValueError: key is empty\n\n# after\nencrypt('hello', 'coffee')","handlingStrategy":"validation","validationCode":"if not key:\n    raise ValueError('encryption key is missing/blank')","typeGuard":"def is_nonempty_str(v: object) -> bool:\n    return isinstance(v, str) and len(v) > 0","tryCatchPattern":null,"preventionTips":["Fail fast on blank keys at config load, not deep in the cipher.","Never default key parameters to ''. Use a required parameter."],"tags":["cipher","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}