同步操作将从 编程语言算法集/Python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
"""https://en.wikipedia.org/wiki/Autokey_cipherAn autokey cipher (also known as the autoclave cipher) is a cipher thatincorporates the message (the plaintext) into the key.The key is generated from the message in some automated fashion,sometimes by selecting certain letters from the text or, more commonly,by adding a short primer key to the front of the message."""def encrypt(plaintext: str, key: str) -> str:"""Encrypt a given `plaintext` (string) and `key` (string), returning theencrypted ciphertext.>>> encrypt("hello world", "coffee")'jsqqs avvwo'>>> encrypt("coffee is good as python", "TheAlgorithms")'vvjfpk wj ohvp su ddylsv'>>> encrypt("coffee is good as python", 2)Traceback (most recent call last):...TypeError: key must be a string>>> encrypt("", "TheAlgorithms")Traceback (most recent call last):...ValueError: plaintext is empty>>> encrypt("coffee is good as python", "")Traceback (most recent call last):...ValueError: key is empty>>> encrypt(527.26, "TheAlgorithms")Traceback (most recent call last):...TypeError: plaintext must be a string"""if not isinstance(plaintext, str):raise TypeError("plaintext must be a string")if not isinstance(key, str):raise TypeError("key must be a string")if not plaintext:raise ValueError("plaintext is empty")if not key:raise ValueError("key is empty")key += plaintextplaintext = plaintext.lower()key = key.lower()plaintext_iterator = 0key_iterator = 0ciphertext = ""while plaintext_iterator < len(plaintext):if (ord(plaintext[plaintext_iterator]) < 97or ord(plaintext[plaintext_iterator]) > 122):ciphertext += plaintext[plaintext_iterator]plaintext_iterator += 1elif ord(key[key_iterator]) < 97 or ord(key[key_iterator]) > 122:key_iterator += 1else:ciphertext += chr(((ord(plaintext[plaintext_iterator]) - 97 + ord(key[key_iterator]))- 97)% 26+ 97)key_iterator += 1plaintext_iterator += 1return ciphertextdef decrypt(ciphertext: str, key: str) -> str:"""Decrypt a given `ciphertext` (string) and `key` (string), returning the decryptedciphertext.>>> decrypt("jsqqs avvwo", "coffee")'hello world'>>> decrypt("vvjfpk wj ohvp su ddylsv", "TheAlgorithms")'coffee is good as python'>>> decrypt("vvjfpk wj ohvp su ddylsv", "")Traceback (most recent call last):...ValueError: key is empty>>> decrypt(527.26, "TheAlgorithms")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 = 0key_iterator = 0plaintext = ""while ciphertext_iterator < len(ciphertext):if (ord(ciphertext[ciphertext_iterator]) < 97or ord(ciphertext[ciphertext_iterator]) > 122):plaintext += ciphertext[ciphertext_iterator]else:plaintext += chr((ord(ciphertext[ciphertext_iterator]) - ord(key[key_iterator])) % 26+ 97)key += chr((ord(ciphertext[ciphertext_iterator]) - ord(key[key_iterator])) % 26+ 97)key_iterator += 1ciphertext_iterator += 1return plaintextif __name__ == "__main__":import doctestdoctest.testmod()operation = int(input("Type 1 to encrypt or 2 to decrypt:"))if operation == 1:plaintext = input("Typeplaintext to be encrypted:\n")key = input("Type the key:\n")print(encrypt(plaintext, key))elif operation == 2:ciphertext = input("Type the ciphertext to be decrypted:\n")key = input("Type the key:\n")print(decrypt(ciphertext, key))decrypt("jsqqs avvwo", "coffee")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。