Skip to content

Add running key cipher #10834

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

Merged
merged 5 commits into from
Oct 29, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions ciphers/running_key_cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def running_key_encrypt(key, plaintext):

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file ciphers/running_key_cipher.py, please provide doctest for the function running_key_encrypt

Please provide return type hint for the function: running_key_encrypt. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: key

Please provide type hint for the parameter: plaintext

plaintext = plaintext.replace(" ", "").upper()
key = key.replace(" ", "").upper()
key_length = len(key)
ciphertext = []

for i in range(len(plaintext)):
p = ord(plaintext[i]) - ord("A")
k = ord(key[i % key_length]) - ord("A")
c = (p + k) % 26
ciphertext.append(chr(c + ord("A")))

return "".join(ciphertext)


def running_key_decrypt(key, ciphertext):

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file ciphers/running_key_cipher.py, please provide doctest for the function running_key_decrypt

Please provide return type hint for the function: running_key_decrypt. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: key

Please provide type hint for the parameter: ciphertext

ciphertext = ciphertext.replace(" ", "").upper()
key = key.replace(" ", "").upper()
key_length = len(key)
plaintext = []

for i in range(len(ciphertext)):
c = ord(ciphertext[i]) - ord("A")
k = ord(key[i % key_length]) - ord("A")
p = (c - k) % 26
plaintext.append(chr(p + ord("A")))

return "".join(plaintext)


if __name__ == "__main__":
key = "How does the duck know that? said Victor"
plaintext = input("Enter the plaintext: ").upper()
encrypted_text = running_key_encrypt(key, plaintext)
decrypted_text = running_key_decrypt(key, encrypted_text)

print("\nPlaintext:", plaintext)
print("Encrypted:", encrypted_text)
print("Decrypted:", decrypted_text)