Skip to content

Instantly share code, notes, and snippets.

@lennyerik
Last active October 24, 2022 09:50
Show Gist options
  • Save lennyerik/0abca2d800c73bfd6a93ee5f566a4634 to your computer and use it in GitHub Desktop.
Save lennyerik/0abca2d800c73bfd6a93ee5f566a4634 to your computer and use it in GitHub Desktop.
Decryption of ASCII text
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='')
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