2016-11-16 85 views
0

我對我們的任務非常困惑:「開發程序的一部分,該程序生成程序用於加密消息的八個關鍵字符鍵」。我們正在嘗試創建一個加密程序。該代碼我到目前爲止有:我現在需要做什麼?

文件

def fileopen(): 
    filename=input('What is the name of the file: ') 

    with open(filename) as yarr: 
     contents=yarr.read() 
     return contents 

菜單

print ("Hello, welcome to the encryption/decryption program") 
print ("1 - Encryption") 
print ("2 - Decryption") 
print ("3 - Exit") 
choice=input ("Enter a Number") 
while True: 
    if choice=="1": 
     print("Encryption Selected") 
     filecontents=fileopen() 
     print(filecontents) 
     break 

    elif choice=="2": 
     print("Decryption Selected") 
     break 

    elif choice=="3": 
     print("Thank you, come again") 
     break 

    else: 
     choice=input("Incorrect number") 
     continue 
+0

我也很困惑。什麼是「八鍵鑰匙」?你可能是指「八個字符的鍵」(即八個字符的隨機字符串)? –

回答

0

嘗試使用愷撒密碼。

這是我爲它來源:

愷撒密碼

MAX_KEY_SIZE = 26 

def getMode(): 
    while True: 
     print('Do you wish to encrypt or decrypt or brute force a message?') 
     mode = input().lower() 
     if mode in 'encrypt e decrypt d brute b'.split(): 
      return mode 
     else: 
      print('Enter either "encrypt" or "e" or "decrypt" or "d" or "brute" or "b".') 

def getMessage(): 
    print('Enter your message:') 
    return input() 

def getKey(): 
    key = 0 
    while True: 
     print('Enter the key number (1-%s)' % (MAX_KEY_SIZE)) 
     key = int(input()) 
     if (key >= 1 and key <= MAX_KEY_SIZE): 
      return key 

def getTranslatedMessage(mode, message, key): 
    if mode[0] == 'd': 
     key = -key 
    translated = '' 

    for symbol in message: 
     if symbol.isalpha(): 
      num = ord(symbol) 
      num += key 

      if symbol.isupper(): 
       if num > ord('Z'): 
        num -= 26 
       elif num < ord('A'): 
        num += 26 
      elif symbol.islower(): 
       if num > ord('z'): 
        num -= 26 
       elif num < ord('a'): 
        num += 26 

      translated += chr(num) 
     else: 
      translated += symbol 
    return translated 

mode = getMode() 
message = getMessage() 
if mode[0] != 'b': 
    key = getKey() 
print ('Your translated text is:') 
if mode[0] != 'b': 
    print(getTranslatedMessage(mode,message,key)) 
else: 
    for key in range(1, MAX_KEY_SIZE + 1): 
     print(key,getTranslatedMessage('decrypt',message,key)) 
key = getKey() 

print('Your translated text is:') 
print(getTranslatedMessage(mode, message, key)) 

是這樣工作的:

首先,如果你要解密或加密文本(寫E或d,然後按回車確定)。接下來輸入您的信息,然後按回車確認。接下來輸入一個數字,並在您想要解密消息時記住它。然後按回車,你會得到你的加密/解密消息。

您可以重命名問題並在if語句中添加輸入字段,以便它也轉換文本文件。

希望這有助於:)

相關問題