Skip to content

Instantly share code, notes, and snippets.

@k4kfh
Last active November 20, 2017 15:25
Show Gist options
  • Save k4kfh/d6a5e01f18834b3740d2f9849ef75314 to your computer and use it in GitHub Desktop.
Save k4kfh/d6a5e01f18834b3740d2f9849ef75314 to your computer and use it in GitHub Desktop.
Cipher a friend made up
key1 = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
key2 = ['z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
import re
def encrypt(string):
#first split it into a list of words
wordList = re.sub("[^\w]", " ", string).split()
useKey = key1
encodedString = "|"
for word in wordList:
encodedWord = ""
for character in word:
encodedWord = encodedWord + str(useKey.index(character)) + "."
encodedWord = encodedWord.rstrip(".") + "|"
encodedString = encodedString + encodedWord
if (useKey == key1):
useKey = key2
elif (useKey == key2):
useKey = key1
return encodedString
def decrypt(string):
wordList = string.split("|")
useKey = key1
decodedString = ""
wordList.remove("")
wordList.remove("")
for word in wordList:
charList = word.split(".")
decodedWord = ""
print(charList)
for char in charList:
if (char != " " or char != ""):
decodedWord = decodedWord + useKey[int(char)]
decodedString = decodedString + decodedWord + " "
if (useKey == key1):
useKey = key2
print("Switching to key 2...")
elif (useKey == key2):
useKey = key1
print("Switching to key 1...")
decodedString = decodedString.rstrip(" ")
return decodedString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment