2014-12-04 75 views
2

我目前正在嘗試使用關鍵字進行加密。我已經採取了用戶輸入和關鍵字輸入並獲得了字母表中每個字母的值。 (a = 1,b = 2,c = 3等)我現在需要將這兩個值加在一起。正如我在代碼中使用了一個while循環來取每個字母並取值,我無法獲取每個單獨的值並添加它。有人能給我一個關於如何增加每個值的正確方向嗎? 謝謝。一起添加2個字母值?

def keyEnc(): 
    alpha = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] 
    gcselist = ['g','c','s','e'] 
    code = input("Please enter the word you would like to encrypt: ") 
    print("User's word is " + code) 
    print("The number of letters in the code is: ") 
    print(len(code)) 
    x=0 
    while x < len(code): 
     currLetterA=code[x] 
     print("Letter: ",currLetterA) 
     myii=alpha.index(currLetterA) 
     myii=myii+1 
     print("The Value Is: ",myii) 
     x=x+1 
############################################################################################# 
    key = input("Please enter the keyword you would like to encrypt your word by: ") 
    x=0 
    while x < len(key): 
     currLetter=key[x] 
     print("Letter: ",currLetter) 
     myi=alpha.index(currLetter) 
     myi=myi+1 
     print("The Value Is: ",myi) 
     finWord = myi+myii 
     print(finWord) 
     x=x+1 
keyEnc() 
+0

注意,這是一個不可逆的密碼!無論哪種方式,最好使用'ord'而不是嘗試爲每個字母編制索引。 – 2014-12-04 20:31:45

+0

我對Python很新,但我想如果我可以得到每個值然後我可以finWord =(myi + myii + 1) – user3411623 2014-12-04 20:35:55

+0

我會看看intto使用ord函數。感謝提示亞當。 – user3411623 2014-12-04 20:36:17

回答

0

這聽起來像你想做到這一點:

cleartext: somewords = 19 15 13 5 23 15 18 4 19 
key:  something = 19 15 13 5 20 8 9 14 7 
         ++++++++++++++++++++++++++++ 
ciphertext:    38 30 26 10 43 23 27 18 26 

是這樣嗎?在這種情況下,您需要一個數字列表,而不僅僅是一個輸出。另外請注意,如果您的明文和密鑰長度不完全相同,則這不起作用。試想一下:

cleartext: somewords = 19 15 13 5 23 15 18 4 19 
key:  banana = 2 1 14 1 14 1 
         ++++++++++++++++++++++++++++ 
ciphertext:    21 16 27 6 37 16 ? ? ? 

cleartext: somewords  = 19 15 13 5 23 15 18 4 19 
key:  bananahammock = 2 1 14 1 14 1 8 1 13 13 15 3 11 
          ++++++++++++++++++++++++++++++++++++++++ 
ciphertext:     21 16 27 6 37 16 36 5 32 ? ? ? ? 

然而在這些情況下,它看起來像你會希望這個代替:

def key_enc(): 
    cleartext_word = input("Enter the word you want to encrypt: ") 
    key = input("Enter the word you want as a key: ") 
    if len(cleartext_word) != len(key): 
     # I'm not sure how to handle this, so that's your design decision 
     # but possibly just: 
     raise ValueError("Clear text and key must have equal length") 
    result = list() 
    def lookup_letter(letter): 
     """Helper function to return a proper index. 

     a = 1, b = 2, c = 3, ...""" 

     return ord(letter) - 96 

    for letter, key_letter in zip(cleartext_word, key): 
     result.append(lookup_letter(letter) + lookup_letter(key_letter)) 

    return result 

採用zip這裏的關鍵是你」重新失蹤。你並不需要在技術上要做到這一點,你可以只使用一個while循環就像你在你的代碼做這樣做:

def key_enc(): 
    # prompt for the cleartext_word and key as above 
    result = [] 
    i = 0 
    while i < len(cleartext_word): # which MUST BE EQUAL TO len(key) 
     clear_letter = cleartext_word[i] 
     key_letter = key[i] 
     result_letter = ord(clear_letter)-96 + ord(key_letter)-96 
     result.append(result_letter) 

但它難以閱讀的方式。

和難以閱讀來講,基本上是你的整個功能是:

result = list(map(lambda letters: sum(map(ord,letters))-192, zip(cleartext_word, key_word))) 
# YUCK!!! 
+0

好的芽。感謝您的幫助,我真的很感激它! – user3411623 2014-12-04 21:12:49