2012-12-24 50 views
-2

所以我在Python 3.3中有此代碼,它與ceaser密碼文件密碼。 我需要知道的是我如何製作一個腳本,將它從原始轉換回來,以便我發送它的人也可以讀取它。Python 3.3密碼腳本

message = input("Input: ") 
key = 11 
coded_message = "" 

for ch in message: 
    code_val = ord(ch) + key 
    if ch.isalpha(): 
     if code_val > ord('z'): 
      code_val -= ord('z') - ord('a') 
     coded_message = coded_message + chr(code_val) 
    else: 
     coded_message = coded_message + ch 
# print ("Input: " + message) 
print ("Output: " + coded_message) 

還有一件事,我計劃在投入,這是一個Tkinter的消息框,用於輸入和輸出兩個輸入字段。應該使用一個字段來鍵入我想要轉換的內容,另一個字段應該用於顯示文本在加密後的樣子。該按鈕應該開始加密。這裏是代碼:

import sys 
from tkinter import * 
def mHello(): 
    mLabel = Label(mGui,text = input("Hello World")) 
    mLabel.grid(row=3, column=0,) 

mGui = Tk() 
ment = StringVar() 
mGui.geometry("450x450+250+250") 
mGui.title("My TKinter") 
# input label 
mLabel = Label(mGui,text = "Input",) 
mLabel.grid(row=1,column=0,) 
# output label 
mLabeltwo = Label(mGui,text = "Input",) 
mLabeltwo.grid(row=2,column=0,) 
# convert button 
mButton = Button(text = "Convert",command = mHello) 
mButton.grid(row=3,column=0) 
# input entry 
mEntry = Entry(mGui,textvariable=ment) 
mEntry.grid(row=1,column=1) 
# output entry 
mEntryTwo = Entry(mGui,textvariable=ment) 
mEntryTwo.grid(row=2,column=1) 





mGui.mainloop() 

通過我只有15的方式,這是我第2天的學習蟒蛇。 一些功勞來源於這個論壇,提供了一些代碼片段 提前謝謝你們!

+0

代碼的第二部分與您的問題無關嗎?如果不是,您的問題將更清楚刪除它和任何其他信息的額外信息。 –

回答

-2

之前,我說別的,你應該知道,頭腦要大得多礦井已告知編寫自己的暗號腳本其他什麼那麼學習

如果你希望他們能夠解碼的代碼然後爲它們提供一個密鑰。所以你的情況:

s maps to h 
t maps to i 
f maps to t 

我希望這個代碼說明了我的建議:

In [1]: from cyro import your_cyrptic_function 

In [2]: key = {'s':'h', 't':'i', 'f':'t'} 

In [3]: secret_word = your_cyrptic_function('hit') 

In [4]: decyrpted_secret_word = '' 

In [5]: for letter in secret_word: 
    decyrpted_secret_word += key[letter] 
    ...:     

In [6]: print(decyrpted_secret_word) 
hit 

對於上面的代碼,我把你原來的代碼放到一個函數:

def your_cyrptic_function(secret): 

    message = secret 
    key = 11 
    coded_message = "" 

    for ch in message: 
     code_val = ord(ch) + key 
     if ch.isalpha(): 
      if code_val > ord('z'): 
       code_val -= ord('z') - ord('a') 
      coded_message = coded_message + chr(code_val) 
     else: 
      coded_message = coded_message + ch 
    # print ("Input: " + message) 
    return coded_message 

有幾個在Python中做這件事的好方法。如果您對cyptopgraphy感興趣,請查看Udacities class cs387 applied cryptography