Skip to content

Instantly share code, notes, and snippets.

@cowdinosaur
Last active August 8, 2022 10:45
Show Gist options
  • Save cowdinosaur/4504ab9705f0926c8cf8 to your computer and use it in GitHub Desktop.
Save cowdinosaur/4504ab9705f0926c8cf8 to your computer and use it in GitHub Desktop.
Monoalphabetic Cipher
# Simple Substitution Cipher
# http://inventwithpython.com/hacking (BSD Licensed)
import sys, random
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def main():
myMessage = 'When you do the common things in life in an uncommon way, you will command the attention of the world'
myKey = 'QWERTYUIOPASDFGHJKLZXCVBNM'
myMode = 'encrypt' # set to 'encrypt' or 'decrypt'
checkValidKey(myKey)
if myMode == 'encrypt':
translated = encryptMessage(myKey, myMessage)
elif myMode == 'decrypt':
translated = decryptMessage(myKey, myMessage)
print('Using key %s' % (myKey))
print('The %sed message is:' % (myMode))
print(translated)
def checkValidKey(key):
keyList = list(key)
lettersList = list(LETTERS)
keyList.sort()
lettersList.sort()
if keyList != lettersList:
sys.exit('There is an error in the key or symbol set.')
def encryptMessage(key, message):
return translateMessage(key, message, 'encrypt')
def decryptMessage(key, message):
return translateMessage(key, message, 'decrypt')
def translateMessage(key, message, mode):
translated = ''
charsA = LETTERS
charsB = key
if mode == 'decrypt':
# For decrypting, we can use the same code as encrypting. We
# just need to swap where the key and LETTERS strings are used.
charsA, charsB = charsB, charsA
# loop through each symbol in the message
for symbol in message:
if symbol.upper() in charsA:
# encrypt/decrypt the symbol
symIndex = charsA.find(symbol.upper())
if symbol.isupper():
translated += charsB[symIndex].upper()
else:
translated += charsB[symIndex].lower()
else:
# symbol is not in LETTERS, just add it
translated += symbol
return translated
def getRandomKey():
key = list(LETTERS)
random.shuffle(key)
return ''.join(key)
main()
@basmahalsohbany
Copy link

basmah

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment