2016-03-05 38 views
-1

我一直有麻煩試圖讓我的代碼導出正確的加密文本。我的代碼,而不是導出文本已被選中和加密它導出文件只包含'[]'輸出不正確的文本

我將如何去解決這個問題,因爲我是新來的Python?

下面

全碼:

import io 
import sys 
import random 
import time 
import math 


lowbound = 33 
highbound = 126 

key=str 


def offset_factor(key): 
    total = 0 
    for index in range(8): 
     numm = ord(key[index]) 
     total += numm 
    total = total//8 
    total = total - 32 
    return total 

def generateoffset(key): 
    total = 0 
    for i in range(0,8): 
     offsetFactor = offsetFactor +ord(key[i]) 
    offsetFactor = offsetFactor//8-32 
    return offsetFactor 

def generatekey(): 
    key=" " 
    for line in range(8): 
     randomnumber = random.randint(lowbound,highbound) 
     randomchar = chr(randomnumber) 
     key += randomchar 
    return key 

def decrypt(): 
    file = open(input("Please enter the name of the text file you want " 
         "to decrypt \n(include the extension): "), mode="r").read() 
    encryption_key = input("Please enter the encryption key that was used to encrypt this text file: ") 
    offset_factor, decrypted_message = 0, "" 
    for char in encryption_key: 
     offset_factor += int(ord(char)) 
    offset_factor = math.floor(offset_factor/8) - 32 
    for char in file: 
     if char == " ": 
      decrypted_message += " " 

     else: 
      decrypted_character = (ord(char)) - offset_factor 
      if decrypted_character < 33: 
       decrypted_character += 94 
      decrypted_message += str(chr(decrypted_character)) 
    print(decrypted_message) 




def runProgram(): 
    def choice(): 
     userChoice = input("1. Encrypt Message\n" 
          "2. Decrypt Message\n" 
          "3. Exit The Program\n" 
          "Please enter the number of what you want to do:\n>>") 
     if userChoice == "1": 
      return userChoice 
     elif userChoice == "2": 
      return decrypt() 
     elif userChoice == "3": 
      print("Exiting Program") 
      sys.exit() 
     else: 
      print("Invalid Response. Please try again.") 
      choice() 

    def getMessage(): #Beginning of where the encryption starts 
     print("Enter the file name which you want to Encrypt\n") 
     userMessage = input("") 
     try: 
      filetoencrypt = open(userMessage,"r") 
      print (filetoencrypt) 
     except: 
      print("You have given a file which does not exist. please try again.\n") 
      time.sleep(1) 


      choice() 
     else: 
      print("File has been Successfully found.") 
      toencrypt = [] 
      for line in filetoencrypt: 
       toencrypt.append(line.strip()) #  
      key = generatekey() 
      print ("Key has been generated:" + key) 
      offset = offset_factor(key) 
      encrypted = [] 

      for line in toencrypt: 
       encryptedline = "" 
       for char in line: 
        key1, number, getMessage = generatekey(), 0, "" 
        offset_factor1 = math.floor(number/8) - 32 
        if char == '': 
         encryptedline +='' 


      else: 
       encrypt_letter = ord(char) + (offset_factor1) 
       if encrypt_letter > 126: 
        encrypt_letter -= 94 
       getMessage += str(chr(encrypt_letter)) 
       for line in range(1): 
        print("") 
      NewEncrypt = input("Would you like to make it more secure, enter Yes or No: ") 
      time.sleep(1) 
      if NewEncrypt.upper() == "YES": 
       getMessage = getMessage.replace(" ", "") 
       getMessage = ' '.join([getMessage[i:i+5] for i in range(0, len(getMessage), 5)]) 
      elif NewEncrypt.upper == "NO": 
       getMessage = getMessage 
      else: 
       print("You entered an invalid option.") 
       exit() 
      open(input("Text encrypted successfully. \nWhat would you like to save the new " 
         "file as (include the extension): "), mode="w").write(getMessage) 
      print ("File saved.") 
      sys.exit() 






    userChoice = choice() 
    message = getMessage() 
    fl = decrypt() 
runProgram() 

具體地,這部分代碼:

def getMessage(): 
    print("Enter the file name which you want to Encrypt\n") 
    userMessage = input("") 
    try: 
     filetoencrypt = open(userMessage,"r") 
     print (filetoencrypt) 
    except: 
     print("You have given a file which does not exist. please try again.\n") 
     time.sleep(1) 


     choice() 
    else: 
     print("File has been Successfully found.") 
     toencrypt = [] 
     for line in filetoencrypt: 
      toencrypt.append(line.strip()) #  
     key = generatekey() 
     print ("Key has been generated:" + key) 
     offset = offset_factor(key) 
     encrypted = [] 

     for line in toencrypt: 
      encryptedline = "" 
      for char in line: 
       key1, number, getMessage = generatekey(), 0, "" 
       offset_factor1 = math.floor(number/8) - 32 
       if char == '': 
        encryptedline +='' 


     else: 
      encrypt_letter = ord(char) + (offset_factor1) 
      if encrypt_letter > 126: 
       encrypt_letter -= 94 
      getMessage += str(chr(encrypt_letter)) 
      for line in range(1): 
       print("") 
     NewEncrypt = input("Would you like to make it more secure, enter Yes or No: ") 
     time.sleep(1) 
     if NewEncrypt.upper() == "YES": 
      getMessage = getMessage.replace(" ", "") 
      getMessage = ' '.join([getMessage[i:i+5] for i in range(0, len(getMessage), 5)]) 
     elif NewEncrypt.upper == "NO": 
      getMessage = getMessage 
     else: 
      print("You entered an invalid option.") 
      exit() 
     open(input("Text encrypted successfully. \nWhat would you like to save the new " 
        "file as (include the extension): "), mode="w").write(getMessage) 
     print ("File saved.") 
     sys.exit() 
+0

哪部分代碼不工作?這個問題太籠統了 – Farside

回答

0

選擇1是 「加密消息」

if userChoice == "1": 
    return userChoice 

但它不調用一個加密方法,實際上沒有encrypt()方法,它只是返回「1」。