2016-01-28 52 views
0

我目前正在研究一個python項目以獲得一串文本,通過向其添加關鍵字並輸出結果來加密文本。除了將數值轉換回文本外,我目前具有此程序的所有功能。將數字轉換爲字母表中的相應字母 - Python 2.7

例如,原始文本將被轉換爲數值,例如[a, b, c]將變爲[1, 2, 3]

目前,我有不知道如何解決此問題的想法,並歡迎任何幫助,我當前的代碼如下:

def encryption(): 
    print("You have chosen Encryption") 
    outputkeyword = [] 
    output = [] 

    input = raw_input('Enter Text: ') 
    input = input.lower() 

    for character in input: 
     number = ord(character) - 96 
     output.append(number) 

    input = raw_input('Enter Keyword: ') 
    input = input.lower() 
    for characterkeyword in input: 
     numberkeyword = ord(characterkeyword) - 96 
     outputkeyword.append(numberkeyword) 
     first = output 
     second = outputkeyword 

    print("The following is for debugging only") 
    print output 
    print outputkeyword 

    outputfinal = [x + y for x, y in zip(first, second)] 
    print outputfinal 

def decryption(): 
    print("You have chosen Decryption") 
    outputkeyword = [] 
    output = [] 
    input = raw_input('Enter Text: ') 
    input = input.lower() 
    for character in input: 
     number = ord(character) - 96 
     output.append(number) 

    input = raw_input('Enter Keyword: ') 
    input = input.lower() 
    for characterkeyword in input: 
     numberkeyword = ord(characterkeyword) - 96 
     outputkeyword.append(numberkeyword) 
     first = output 
     second = outputkeyword 

    print("The following is for debuging only") 
    print output 
    print outputkeyword 

    outputfinal = [y - x for x, y in zip(second, first)] 
    print outputfinal 

mode = raw_input("Encrypt 'e' or Decrypt 'd' ") 

if mode == "e": 
    encryption() 
elif mode == "d": 
    decryption() 
else: 
    print("Enter a valid option") 
    mode = raw_input("Encrypt 'e' or Decrypt 'd' ") 

    if mode == "e": 
     encryption() 
    elif mode == "d": 
     decryption() 
+0

謝謝馬丁。我希望稍後我能看到這個問題。我不必自己修復縮進。 :) –

回答

0

雖然你的問題不是很清楚我寫了一個腳本使用字典

plaintext = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') #Alphabet String for Input 
Etext = list('1A2B3C4D5E6F7G8H90IJKLMNOP') """Here is string combination of numbers and alphabets you can replace it with any kinda format and your dictionary will be build with respect to the alphabet sting"""    

def messageEnc(text,plain, encryp): 
    dictionary = dict(zip(plain, encryp)) 
    newmessage = '' 
    for char in text: 
    try: 
     newmessage = newmessage + dictionary[char.upper()] 
    except: 
     newmessage += '' 
print(text,'has been encryptd to:',newmessage) 

def messageDec(text,encryp, plain): 
dictionary = dict(zip(encryp,plain)) 
newmessage = '' 
for char in text: 
    try: 

     newmessage = newmessage + dictionary[char.upper()] 

    except: 
     newmessage += '' 
print(text,'has been Decrypted to:',newmessage) 


while True: 
print(""" 
    Simple Dictionary Encryption : 
    Press 1 to Encrypt 
    Press 2 to Decrypt 
    """) 
    try: 
    choose = int(input()) 
    except: 
    print("Press Either 1 or 2") 
    continue 

    if choose == 1: 

    text = str(input("enter something: ")) 
    messageEnc(text,plaintext,Etext) 
    continue 
    else: 
    text = str(input("enter something: ")) 
    messageDec(text,Etext,plaintext) 
0

假設你有一個像a = [1, 2, 3]數字和字母的列表: alpha = "abcdef"。然後你將其轉換如下:

def NumToStr(numbers, alpha): 
    ret = "" 
    for num in numbers: 
     try: 
      ret += alpha[num-1] 
     except: 
      # handle error 
      pass 
    return ret 
+0

感謝您的意見,但我不確定如何將此代碼實施到我現有的項目中,您可以幫我嗎?謝謝 –

0

回答你的問題,你想把數字轉換回文本。

可以宣佈兩者的數字和字母兩個列表,比如:

NUM = [1,2,3 .... 26]

ALPA = [ '一個',」 b','c'...'z']

並且之後從num列表中找到索引/位置並在alpa列表中找到該索引的字母表。

0

一對夫婦的事情,做加密。首先是將編碼值轉換回可以使用chr(i)命令的字符。只要記住要重新打開96。

for code in outputfinal: 
     print chr(code + 96), 

我嘗試「AAA」作爲我的明文和「BBB」作爲我的關鍵字,這個印有「CCC」,這是我認爲你正在嘗試做的。

另一件事。 zip命令在兩個列表中迭代,但是(在我的播放中)只有其中一個列表中的成員用完。如果您的明文爲10個字符,而您的關鍵字只有5個,那麼只有您的明文的前5個字符被加密。您需要將密鑰擴展或膨脹至明文消息的長度。我玩這樣的事情:

plaintext = ['h','e','l','l','o',' ','w','o','r','l','d'] 
keyword = ['f','r','e','d'] 

index = len(keyword) # point to the end of the key word 
keyLength = len(keyword) 
while index < len(plaintext): # while there are still letters in the plain text 
    keyword.append(keyword[index - keyLength]) # expand the key 
    index += 1 

print keyword 

for a,b in zip(plaintext, keyword): 
    print a, b 

我希望這可以幫助。如果我誤解了,請告訴我。

+0

擴展關鍵字的一種方法是將它傳遞給['itertools.cycle'](https://docs.python.org/2/library/itertools.html#itertools.cycle)。 'itertools'中有各種有趣的東西,包括'izip_longest',除了它返回一個迭代器外,其工作方式與'zip'相似,只是當參數用盡時,它提供了一個填充值。當然,'izip_longest'對這個問題沒有多大用處,但是知道它很方便。 –

+0

另一個玩具讓我玩。謝謝。 –

相關問題