简体   繁体   中英

Encryption code in python, call upon a function, python returns nothing. No error messages show

Okay, so I've written the following series of functions in Python 3.6.0:

def code_char(c, key):
  result = ord(c) + key
  if c.isupper():
    while result > ord("Z"):
        result -= 26
    while result < ord("A"):
        result += 26
    return chr(result)
  else:
    while result > ord("z"):
        result -= 26
    while result < ord("a"):
        result += 26
  result = chr(result)
  return result

def isletter(char):
    if 65 <= ord(char) <= 90 or 97<= ord(char) <= 122:
            return True
    else:
            return False

def encrypt(string, key):
    result = ""
    length = len(string)
    key = key * (length // len(key)) + key[0:(length % len(key))]
    for i in range(0,length):
            if (isletter for i in string):
                    c = string[i]
                    num = int("".join("".join(i) for i in key))
                    result += code_char(c, num)
            else:
                    c = string[i]
                    result += i
    return result

Then I try to call on the functions with:

encrypt("This is a secret message!!", "12345678")

When python runs the program absolutely nothing happens. Nothing gets returned, and in the shell python forces me onto a blank line without indents, or >>>. i don't know what is right or wrong with the code as no error messages appear, and no results appear. Any kind of advice would be appreciated.

Thank you.

Should you be having while loop here , or are you intening if loop? I don't see any exit for while loop. That may be where your code is hanging.

if c.isupper():
    while result > ord("Z"):
        result -= 26
    while result < ord("A"):
        result += 26
    return chr(result)
  else:
    while result > ord("z"):
        result -= 26
    while result < ord("a"):
        result += 26

Also, if I replace while with if above, it's giving me overflow error.

OverflowError: Python int too large to convert to C long

EDIT After looking at @polo's comment and taking a second look at code, I believe @polo is correct. I put while loop back and added print statements. I have commented them, but you can uncomment at your end.
I've also reduced key's complexity to just key = key and reduced key from 12345678 to just 1234 to see if the code works and if it completes in reasonable time.. You can make it as complex as you want once code runs smoothly.

Here is result I got after:

>>> 
key =1234
coding char = T
coding char = h
coding char = i
coding char = s
coding char =  
coding char = i
coding char = s
coding char =  
coding char = a
coding char =  
coding char = s
coding char = e
coding char = c
coding char = r
coding char = e
coding char = t
coding char =  
coding char = m
coding char = e
coding char = s
coding char = s
coding char = a
coding char = g
coding char = e
coding char = !
coding char = !
encrypted_message = Ftuezuezmzeqodqfzyqeemsqaa 

Modified code below:

def code_char(c, key):
  result = ord(c) + key
  if c.isupper():
    while result > ord("Z"):
        #print("result1 = {}",format(result))
        result -=  26 
    while result < ord("A"):
        #print("result2 = {}",format(result))
        result += 26
    return chr(result)
  else:
    while result > ord("z"):
        #print("result3 = {}",format(result))
        result -= 26
    while result < ord("a"):
        #print("result4 = {}",format(result))
        result += 26
  result = chr(result)
  return result

def isletter(char):
    if 65 <= ord(char) <= 90 or 97<= ord(char) <= 122:
            return True
    else:
            return False

def encrypt(string, key):
    result = ""
    length = len(string)
    #key = key * (length // len(key)) + key[0:(length % len(key))]    
    key = key
    print "key ={}".format(key)
    for i in range(0,length):
            if (isletter for i in string):
                    c = string[i]
                    num = int("".join("".join(i) for i in key))
                    print("coding char = {}".format(c))
                    result += code_char(c, num)
            else:
                    c = string[i]
                    result += i
    return result

#encrypt("This is a secret message!!", "12345678")
encrypted_message = encrypt("This is a secret message!!", "1234")
print("encrypted_message = {}".format(encrypted_message)) 

Looking at your code, I don't think this is an infinite loop. I think your loop will not be infinite but will run for a very long time since the value of key is very big, and so, subtracting 26 at a time, until it gets to an English letter ascii value, will just take forever (but not really forever)

>>> key = '12345678'
>>> length = len("This is a secret message!!")
>>> key * (length // len(key)) + key[0:(length % len(key))]
'12345678123456781234567812'

It might be a problem in the your logic, maybe in the logic generating the key, but if this is indeed the logic you want, how about using modulus rather than iterating:

def code_char(c, key):
    result = ord(c) + key
    if c.isupper():
        if result > ord("Z"):
            result = ord("Z") + result % 26 - 26
        if result < ord("A"):
            result = ord("A") - result % 26 + 26
        return chr(result)
    else:
        if result > ord("z"):
            result = ord("z") + result % 26 - 26
        if result < ord("a"):
            result = ord("a") - result % 26 + 26

    return chr(result)

def isletter(char):
    if 65 <= ord(char) <= 90 or 97<= ord(char) <= 122:
            return True
    else:
            return False

def encrypt(string, key):
    result = ""
    length = len(string)
    key = key * (length // len(key)) + key[0:(length % len(key))]
    for i in range(0,length):
            if (isletter for i in string):
                    c = string[i]
                    num = int("".join("".join(i) for i in key))
                    result += code_char(c, num)
            else:
                    c = string[i]
                    result += i
    return result


>>> encrypt("This is a secret message!!", "12345678")
'Rlmwrmwrerwigvixrqiwwekiss'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM