Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autoclave cipher #8029

Merged
merged 11 commits into from
Dec 18, 2022
111 changes: 111 additions & 0 deletions ciphers/autoclave.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
def encrypt(plaintext, key) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def encrypt(plaintext, key) -> str:
def encrypt(plaintext, key) -> str:

These parameters need to be typehinted

Copy link
Member

@cclauss cclauss Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clave is Latin for key so the more popular name for this algorithm is autokey.

Suggested change
def encrypt(plaintext, key) -> str:
”””
https://en.wikipedia.org/wiki/Autokey_cipher
> An autokey cipher (also known as the autoclave cipher) is a cipher that incorporates 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:

"""
Function that encrypt a given plaintext (string)
and key (string), returning the encrypted ciphertext

@params
plaintext - a normal text to be encrypted (string)
key - a small text or word to start the replacing (sFtring)

@return
A string with the ciphertext

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@params
plaintext - a normal text to be encrypted (string)
key - a small text or word to start the replacing (sFtring)
@return
A string with the ciphertext

Typically, we want the parameter names to be self-documenting so this isn't really necessary

>>> encrypt("hello world", "coffee")
'jsqqs avvwo'
"""
if type(plaintext) != str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8: Use isinstance(plaintext, str) instead of directly comparing types.

raise TypeError("plaintext must be a string")
if type(key) != str:
raise TypeError("key must be a string")

if plaintext == "":
raise ValueError("plaintext is empty")
if key == "":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if plaintext == "":
raise ValueError("plaintext is empty")
if key == "":
if not plaintext:
raise ValueError("plaintext is empty")
if not key:

raise ValueError("key is empty")

key = key + plaintext
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
key = key + plaintext
key += plaintext

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


def decrypt(ciphertext: str, key: str) -> str:
"""
Function that decrypt a given ciphertext (string)
and key (string), returning the decrypted ciphertext

@params
ciphertext - a normal text to be decrypted (string)
key - a small text or word used to encrypt (string)

@return
A string with the ciphertext

>>> decrypt("jsqqs avvwo", "coffee")
'hello world'
"""
if type(ciphertext) != str:
raise TypeError("ciphertext must be a string")
if type(key) != str:
raise TypeError("key must be a string")

if ciphertext == "":
raise ValueError("ciphertext is empty")
if key == "":
raise ValueError("key is empty")
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
)
key += chr(
(ord(ciphertext[ciphertext_iterator]) - ord(key[key_iterator])) % 26
+ 97
)
key_iterator += 1
ciphertext_iterator += 1
return plaintext


operation = int(input("Type 1 to encrypt or 2 to decrypt:"))
if operation == 1:
plaintext = str(input("Typeplaintext to be encrypted:\n"))
key = str(input("Type the key:\n"))
print(encrypt(plaintext, key))
elif operation == 2:
ciphertext = str(input("Type the ciphertext to be decrypted:\n"))
key = str(input("Type the key:\n"))
print(decrypt(ciphertext, key))
decrypt("jsqqs avvwo", "coffee")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move this into an if __name__ == "__main__": block