Lesson 25: Simple encryption

posted May 10, 2018, 8:03 AM by Attila Lengyel   [ updated May 11, 2018, 8:00 AM ]
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
 
text = input("Give me a text:")
password = input("Give me a password:")
secretmsg = crypt(text,password, 1)
print(secretmsg)
print(crypt(secretmsg, password, -1))
Comments