Skip to content

Instantly share code, notes, and snippets.

@MarieAshley
Forked from nohe427/encryptDecrypt.py
Last active August 31, 2015 18:17
Show Gist options
  • Select an option

  • Save MarieAshley/21c39127de0a57cc6780 to your computer and use it in GitHub Desktop.

Select an option

Save MarieAshley/21c39127de0a57cc6780 to your computer and use it in GitHub Desktop.
import random, unittest
def generateMagicKey():
magicKey = {}
list1 = ["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"]
list2 = ["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"]
while len(list2) > 0:
z = random.randint(0, len(list2)-1)
y = random.randint(0, len(list2)-1)
a, b = list1[z], list2[y]
magicKey[a], magicKey[b] = b, a
list1.remove(a)
list2.remove(a)
try: list2.remove(b)
except: pass
try: list1.remove(b)
except: pass
print "Your magic key is:\n"
print magicKey
return magicKey
def encrypt(word, magicKey):
stringBuild = ""
for i in word:
i = i.lower()
if i in magicKey.keys(): stringBuild += magicKey[i.lower()]
else: stringBuild += i
return stringBuild
def decrypt(word, magicKey):
stringBuild = ""
for i in word:
if i in magicKey.keys(): stringBuild += magicKey[i]
else: stringBuild += i
return stringBuild
class Test_phrases(unittest.TestCase):
s = "Sunshine, daisies, butter mellow, turn this stupid, fat rat yellow."
print(s)
magicKey = generateMagicKey()
newWord = encrypt(s, magicKey)
print(newWord)
def test_poem(self):
decrypted = decrypt(self.newWord, self.magicKey)
print(decrypted)
self.assertEqual(decrypted, self.s.lower())
if __name__ == "__main__":
unittest.main()
@MarieAshley

Copy link
Copy Markdown
Author

Added ability to encrypt and decrypt phrases.

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