Last active
March 30, 2025 08:41
-
-
Save GiwbyAlbatross/52fe84ffb19ada3f50fddf340b3e2593 to your computer and use it in GitHub Desktop.
This is a modified version of the xkcd scream cipher https://xkcd.com/3054
This file contains hidden or 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
"xkcd scream cipher implementation" | |
import string | |
As = ['\u0041', '\u00c0', '\u00c1', '\u00c2', '\u00c3', '\u00c4', '\u00c5', '\u0100', '\u0102', '\u0104', '\u01cd', '\u01de', '\u01e0', '\u01fa', '\u0200', '\u0202', | |
'\u0226', '\u023a', '\u1e00', '\u1ea0', '\u1ea2', '\u1ea4', '\u1ea6', '\u1ea8', '\u1eaa', '\u1eac', '\u1eae', '\u1eb0', '\u1eb2', '\u1eb4', '\u1eb6' | |
] | |
letters = [c for c in ',.()-'] + [c for c in string.ascii_lowercase] | |
def encrypt(s: str) -> str: | |
r = '' | |
for c in s.lower(): | |
if c in letters: | |
r += As[letters.index(c)] | |
else: | |
r += c | |
return r | |
def decrypt(s: str) -> str: | |
r = '' | |
for c in s: | |
if c in As: | |
r += letters[As.index(c)] | |
else: | |
r += c | |
return r | |
if __name__ == '__main__': | |
# command line usage... | |
import sys | |
if '-h' in sys.argv or '--help' in sys.argv: | |
print("""XKCD Scream Cipher encryptor/decryptor | |
usage: {0} [encrypt|decrypt]""".format(sys.argv[0])) | |
if len(sys.argv) == 1: | |
i = input('Encrypt or decrypt: ').lower() | |
j = input('Input: ') | |
elif len(sys.argv) <= 2: | |
i = sys.argv[1] | |
if len(sys.argv) <= 3: | |
j = ' '.join(sys.argv[2:]) | |
else: | |
j = input('Input: ') | |
if i == 'encrypt': | |
print(encrypt(j)) | |
elif i == 'decrypt': | |
print(decrypt(j)) | |
else: | |
print(repr(i), 'is not either \'encrypt\' or \'decrypt\'.') |
Author
GiwbyAlbatross
commented
Mar 3, 2025
And it is now tested: all of string.printable both encrypts and decrypts successfully. Only issue is that the program decrypts everything lowercase…
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment