0

This is a classwork assignment, in which I have to create a program that is able to encrypt and decrypt a message using a keyword (which will be repeated and trimmed to match the length of the original message). The program uses the ASCII character set, and adds and subtracts the values accordingly.

I have had an attempt at the code, and have two variations, which are very close to working and have a few issues that I am unable to diagnose, I have included the code below for reference, and am wondering if anyone will be able to make suggestions in order to make the program more efficient, as well as helping me diagnose the problem.

Variation 1:

    def encrypt(message, keyWrd):
    characters = "abcdefghijklmnopqrstuvwxyz"
    enc = ""
    for x in message:
        if x in characters:
            for y in keyWrd:
                crShift = ord(y)
            num = ord(x)
            num += crShift
            if num > ord("z"):
                num -= 26
            elif num < ord("a"):
                num += 26
            enc = enc+chr(num)
        else:
            enc = enc + x
    return enc

def encrypt(message, keyWrd):
    characters = "abcdefghijklmnopqrstuvwxyz"
    enc = ""
    for x in message:
        if x in characters:
            for y in keyWrd:
                crShift = -ord(y)
            num = ord(x)
            num += crShift
            if num > ord("z"):
                num -= 26
            elif num < ord("a"):
                num += 26
            enc = enc+chr(num)
        else:
            enc = enc + x
    return enc

def functHand(passThr, message, keyWrd):
        message = message.lower()
        if passThr == 1:
                return encrypt(message, keyWrd)
        elif passThr == 2:
                return decrypt(message, keyWrd)

def error(errMSG):
        if errMSG == 1:
            print("---------------------\nInvalid Data entry")
            start()

def keyPlaster(msg, keyWrd):
    keyword = ''
    while len(keyWrd) < len(msg):
        keyword += keyWrd
    keyword = keyword[:len(msg)]
    return keyword


def start():
    print("---------------------")
    try: 
        option = int(input("What would you like to do?:\n1)Encrypt\n2)Decrypt\n3)Exit\n>> "))
    except ValueError:
            error(1)
    if option == 1:
        msg = input("What message would you like to encrypt?: ").replace(' ', '').lower()
        keyWrd = input("What is the keyword?: ").replace(' ', '').lower()
        keyword = ''
        while len(keyWrd) < len(msg):
            keyword += keyWrd
        keyword = keyword[:len(msg)]
        enc = functHand(1, msg, keyword)
        print("The encrypted message is: ", enc)
        start()
    elif option == 2:
        msg = input("What message would you like to decrypt?: ").replace(' ', '').lower()
        keyWrd = input("What is the keyword?: ").replace(' ', '').lower()
        keyword = ''
        while len(keyWrd) < len(msg):
            keyword += keyWrd
        keyword = keyword[:len(msg)]
        msg = functHand(2, msg, keyword)
        print("The decrypted message is: ", msg) 
        start()
    elif option == 3: 
        print("---------------------\nEXITING\n---------------------")
        raise SystemExit
    else:
        error(1)

start()

Variation 2:

def menu():
    try:
        option = int(input("Please select an option:\n1)Encrypt\n2)Decrypt\n3)Exit\n>> "))
    except ValueError:
        print("-----ERROR-----")
        ################
        raise SystemExit
    while option >= 1 and option <= 3:
        if option == 1:
            menuOpt = 1
            handler(menuOpt)
        elif option == 2:
            menuOpt = 2
            handler(menuOpt)
        elif option == 3:
            menuOpt = 3
            handler(menuOpt)
    else:
         print("-----ERROR-----")
         ################
         raise SystemExit

def handler(menuOpt):
    if menuOpt == 1 or menuOpt == 2:
        origInput = input("Please enter a string: ").replace(' ', '').lower()
        manipInput = input("Please enter a keyword: ").replace(' ', '').lower()
        keyPlaster(manipInput, origInput)
        if menuOpt == 1:
            enc(menuOpt, origInput, keyword)
            print("The encrypted message is: ", enc)
        elif menuOpt == 2:
            dec(menuOpt, origInput, keyword)
            print("The decrypted message is: ", dec)
        elif menuOpt == 3:
            print("-----EXITING-----")
            raise SystemExit

def keyPlaster(manipInput, origInput):
    global keyword
    keyword = ''
    while len(keyword) < len(origInput):
        keyword += manipInput
    keyword = keyword[:len(origInput)]
    return keyword



def enc(menuOpt, origInput, keyword):
    characters = "abcdefghijklmnopqrstuvwxyz"
    enc = ""
    for x in origInput:
        if x in characters:
            for y in keyword:
                crShift = ord(y)
            num = ord(x)
            num += crShift
            if num > ord("z"):
                num -= 26
            elif num < ord("a"):
                num += 26
            enc = enc+ chr(num)
        else:
            enc = enc + x
    return enc

def dec(menuOpt, origInput, keyword):
    characters = "abcdefghijklmnopqrstuvwxyz"
    dec = ""
    for x in origInput:
        if x in characters:
            for y in keyword:
                crShift = -ord(y)
            num = ord(x)
            num += crShift
            if num > ord("z"):
                num -= 26
            elif num < ord("a"):
                num += 26
            dec = dec+ chr(num)
        else:
            dec = dec + x
    return dec

menu()

Thanks in advance.

  • What is your expected output? What is your actual output? –  Feb 02 '15 at 17:14
  • @Reticality An example input for this is 'Computingisfun' and keyword of 'GCSE', this should give an output of 'JRFUBWBSNLLKBQ' < This is for the encrypt function, and the decrypt should work the inverse way ("JRFUBWBSNLLKBQ" as message, "GCSE" as keyword). But the issue I'm having, is the program won't seem to output correctly, and I'm having difficulty diagnosing it, so any help would be greatly appreciated :) – Kieron Holmes Feb 02 '15 at 17:20
  • See also: [simple-way-to-encode-a-string-according-to-a-password](http://stackoverflow.com/questions/2490334/simple-way-to-encode-a-string-according-to-a-password) –  Feb 02 '15 at 19:00

0 Answers0