Python 3.3密码脚本

时间:2012-12-24 03:37:58

标签: python-3.x

所以我在python 3.3中有这个代码,它使用ceaser cypher加密文本。 我需要知道的是我如何创建一个脚本,将其从原始文件转换回来,以便我发送它的人也可以阅读它。

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岁,这是我第二天学习python。 这个论坛的消息来源提供了一些代码片段 先谢谢你们!

1 个答案:

答案 0 :(得分:-2)

在我说出任何其他内容之前你应该意识到,我的建议更强大了反对编写自己的cypher脚本

如果您希望他们能够解码您的代码,请为他们提供密钥。所以在你的情况下:

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