Last active
October 24, 2022 09:50
-
-
Save lennyerik/0abca2d800c73bfd6a93ee5f566a4634 to your computer and use it in GitHub Desktop.
Decryption of ASCII text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s = 'NTHELY' | |
a = 15 | |
b = 3 | |
n = 26 | |
# Modular multiplicative inverse of a | |
a_inv = pow(a, -1, n) | |
for c in s: | |
index = ord(c) - 65 | |
new_c = (a_inv * (index - b)) % n | |
print(chr(new_c+65), end='') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sys import argv | |
import string | |
ALPHABET = string.ascii_uppercase | |
def caesar_decrypt(key, string): | |
ret = '' | |
for c in string.upper(): | |
if c in ALPHABET: | |
index = ord(c) - 65 | |
new_index = (index + k) % len(ALPHABET) | |
ret += chr(new_index + 65) | |
else: | |
ret += c | |
return ret | |
if __name__ == '__main__': | |
if len(argv) == 3: | |
k = int(argv[1]) | |
print(caesar_decrypt(k, argv[2])) | |
elif len(argv) == 2: | |
for k in range(1, len(ALPHABET)): | |
print(caesar_decrypt(k, argv[1])) | |
else: | |
print(f'Usage: {argv[0]} <key> <ciphertext>') | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment