Lesson 26: Riddle

Post date: May 17, 2018 2:49:44 PM

riddle = "hVObYomcioTcfoZSOfbWbUodfcUfOaaWbU"

abc = "abcdefghijklmnopqrstuvwxyz !@#$%^&*()_+:?><ABCDEFGHIJKLMNOPQRSTUVWXYZ"

length = len(abc)

def position(c):

return abc.find(c)

def getkey(password):

key = 0

for c in password: key += position(c)

return key

def shift(c,x):

x = x % length

y = x + abc.find(c)

if y > length - 1: y = y % length

return abc[y]

def crypt(text, password, x):

key = getkey(password)

encryptedtext = ""

for c in text:

encryptedtext += shift(c, x * key)

return encryptedtext

def crack(text, num, x):

encryptedtext = ""

for c in text:

encryptedtext += shift(c, x * num)

return encryptedtext

text = input("Give me a text:")

password = input("Give me a password:")

secretmsg = crypt(text,password, 1)

print(secretmsg)

print(crypt(secretmsg, password, -1))

riddle = input("Give me a text to crack:")

for x in range(0, len(abc)):

input()

print(str(x) + ": " + crack(riddle, x, 1))