2015-06-11 34 views
-2

我寫的代碼是Vignere Cipher加密程序,它使用關鍵字來加密消息。我寫了這段代碼,當我完成它的時候,我運行它,它完成了它應該做的所有事情,但輸出了加密的消息。見我下面的代碼,任何幫助感激地接受:我的程序運行但它不輸出加密的消息

ans = False 
print(""" *****Hello. Welcome to the Vignère Cipher Encryption Program***** 
    ***This program uses a keyword that is repeated until it 
    matches the same lenght of the message and then adds its 
    numerical value to the numerical value of the message and 
    outputs the encrypted message in alpha. 
    Please press: 
    E to Encrypt 
    D to Decrypt 
    or double tap enter to quit. 
    """) 

ans=input("What would you like to do now???") 

if ans == "E": 
    plaintext = input("Please enter a message to be encrypted: ").upper() 
    keyword = input("Please enter a keyword to be used to encrypt a message (alpha only): ").upper() 
    ciphered = " " 
    for i in range (len(plaintext)): 
     char = plaintext[i] 
     alphakeywordvalue = ord(keyword[i%len(keyword)]) - ord("A")+1 
     if char.isupper(): 
      if ans == "E" : 
       value = ord(char) + alphakeywordvalue 
       if value > ord("Z"): 
        value -= 26 
        print ("Your encrypted text is:", ciphered) 


elif ans == "D": 
    plaintext = input("Please enter a message to be dencrypted: ").upper() 
    keyword = input("Please enter a keyword to be used to dencrypt a message (alpha only(make sure that it is the same keyword used to encrypt the message)): ").upper() 
    ciphered = " " 
    for i in range (len(plaintext)): 
     char = plaintext[i] 
     alphakeywordvalue = ord(keyword[i%len(keyword)]) - ord("A")+1 
     if char.isupper(): 
      if ans == "D" : 
       value = ord(char) - alphakeywordvalue 
       if value <ord("A"): 
        value += 26 
       ciphered += chr(value) 
       print ("Your decrypted text is:", ciphered) 
+2

在加密情況下,'加密'永遠不會被修改,它的值總是''「'。 – stellasia

+0

您的打印位於for循環內。如果ans ==「E」(內部無用),則不需要測試兩次。 –

回答

2

,怎麼會打印加密郵件,加密程序不會從一個空字符串改變ciphered

if ans == "E": 
     plaintext = input("Please enter a message to be encrypted: ").upper() 
     keyword = input("Please enter a keyword to be used to encrypt a message (alpha only): ").upper() 
1)-> ciphered = " " 
     for i in range (len(plaintext)): 
      char = plaintext[i] 
      alphakeywordvalue = ord(keyword[i%len(keyword)]) - ord("A")+1 
2)->  if char.isupper(): 
3)->   if ans == "E" : 
        value = ord(char) + alphakeywordvalue 
        if value > ord("Z"): 
         value -= 26 
4)->     print ("Your encrypted text is:", ciphered) 
  1. ciphered設置爲空字符串,則從來沒有改變過。
  2. 你總是知道char是上,因爲你所做的所有明文上()
  3. 你知道ans == "E",因爲你測試了它早些時候
  4. 此打印()是迄今爲止縮進它試圖通過循環打印每一次
3

這不是一種很好的書寫方式。非常破舊難讀。如果需要,您應該爲各個部分創建方法並創建一個單獨的main()類部分以與用戶進行交互。發動機應該隱藏,車身應該打磨。保持它們分開。是的,不要重複自己

好吧,這是我重寫的代碼。重要部分包含評論。錯誤在後面解釋..

def encrypt(message, key, direction='E'): 
    # Look here. There are three arguments. Third one takes 'E' to encrypt 
    # and anything else to decrypt. You can modify to handle more cases 

    ciphered = "" # Initialize. You did it almost well 
    for i in range (len(message)): 
     char = message[i] 
     alphakeywordvalue = ord(key[i%len(key)]) - ord("A")+1 # Perfect. We took the key 
     if direction=='E': # To encrypt 
      value = ord(char) + alphakeywordvalue 
     else: # To decrypt 
      value = ord(char) - alphakeywordvalue 
     ciphered += chr(value) # chr is the inverse of ord. It gets the character back 
     # You missed this line 
    return ciphered 

就是這樣。現在您可以編寫代碼與用戶或其他模塊進行交互。這裏有一個樣本測試: -

message = "Hello World" 
key = "abc" 
print "ORIGINAL : "+message 

encoded_message = encrypt(message, key, 'E') 
print "ENCRYPTED : "+encoded_message 

plain_message = encrypt(encoded_message, key, 'D') 
print "DECRYPTED : "+plain_message 

下面是輸出: enter image description here

現在,您可以編輯這個方法來處理更多的情況下,像出的ASCII字符範圍等

相關問題