Skip to content

Instantly share code, notes, and snippets.

@eli2and40
Created August 3, 2020 14:35
Show Gist options
  • Save eli2and40/2d7564b83b02c8311b0642f5f742e99f to your computer and use it in GitHub Desktop.
Save eli2and40/2d7564b83b02c8311b0642f5f742e99f to your computer and use it in GitHub Desktop.
"""
While I'm sure there's a better word for this than cryptec, I'm afraid I don't know it... yet...
"""
def monoalphabetic(string, shif, dir):
"""Returns an un/en-crypted string using a monoalphabetic cryptosystem
Dependencies: crypt(nested), alphabet
In: (message, shift coefficient, direction ("de" for decryption, "en" for encryption))
Out: string"""
alphabet = " aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789!?&@#$%^*+_-=~.:,;`'\"|/\\(){}[]<>"
def crypt(word, shift, dire):
l0 = []
for letter in word:
if dire == "en":
v0 = alphabet[ (alphabet.index(letter) + shift) % (len(alphabet)) ]
l0.append(v0)
elif dire == "de":
v0 = alphabet[ (alphabet.index(letter) - shift) % (len(alphabet)) ]
l0.append(v0)
else:
return ValueError("""Direction of cryption not correctly specified.
'en' for Encryption.
'de' for Decryption""")
v1 = "".join(l0)
return v1
strLines = string.split("\n")
stringLines = [i.strip() for i in strLines]
numLines = len(stringLines)
# wordLists = [statement.split(" ") for statement in stringLines]
# cryps = [[crypt(word,shif,dir) for word in line] for line in wordLists]
cryps = [[crypt(char,shif,dir) for char in line] for line in stringLines]
v1 = """"""
for line in range(numLines):
newLine = "".join(cryps[line])
if line < numLines-1:
v1 += newLine.strip() + '\n'
else:
v1 += newLine.strip()
return v1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment