-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Add running key cipher #10834
Changes from 1 commit
960ff5d
0de53b9
1bf84c4
ed55cfc
d41b9da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
def running_key_encrypt(key, 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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
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) |
There was a problem hiding this comment.
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 functionrunning_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