Skip to content

Instantly share code, notes, and snippets.

@pry0cc
Created May 8, 2015 12:11
Show Gist options
  • Save pry0cc/c4581924160f63bf3430 to your computer and use it in GitHub Desktop.
Save pry0cc/c4581924160f63bf3430 to your computer and use it in GitHub Desktop.
Vinegarlol.py
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def main():
# This text can be copy/pasted from http://invpy.com/vigenereCipher.py
myMessage = input("Input a message >>> ")
myKey = input("Input a keyword >> ")
opt = input(" E for Encrypt or D for Decrypt? >> ") # set to 'encrypt' or 'decrypt'
if (opt == "e"):
myMode = "encrypt"
else:
myMode = "decrypt"
if myMode == "encrypt":
translated = encryptMessage(myKey, myMessage)
elif myMode == "decrypt":
translated = decryptMessage(myKey, myMessage)
print('%sed message:' % (myMode.title()))
print(translated)
print()
def encryptMessage(key, message):
return translateMessage(key, message, "encrypt")
def decryptMessage(key, message):
return translateMessage(key, message, 'decrypt')
def translateMessage(key, message, mode):
translated = [] # stores the encrypted/decrypted message string
keyIndex = 0
key = key.upper()
for symbol in message: # loop through each character in message
num = LETTERS.find(symbol.upper())
if num != -1: # -1 means symbol.upper() was not found in LETTERS
if mode == 'encrypt':
num += LETTERS.find(key[keyIndex]) # add if encrypting
elif mode == 'decrypt':
num -= LETTERS.find(key[keyIndex]) # subtract if decrypting
num %= len(LETTERS) # handle the potential wrap-around
# add the encrypted/decrypted symbol to the end of translated.
if symbol.isupper():
translated.append(LETTERS[num])
elif symbol.islower():
translated.append(LETTERS[num].lower())
keyIndex += 1 # move to the next letter in the key
if keyIndex == len(key):
keyIndex = 0
else:
# The symbol was not in LETTERS, so add it to translated as is.
translated.append(symbol)
return ''.join(translated)
def runProgram():
mode = main ()
if __name__ == '__main__':
main()
while True:
runProgram()
opt = input("Do you want to quit? y/n : ")
if opt.lower() == "y":
break;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment