-2

所以我在 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 岁,这是我学习 python 的第二天。一些功劳归功于这个论坛上的资源,这些资源为我提供了一些代码片段,提前谢谢你们!

4

1 回答 1

-2

Before i say anything else you should be aware that minds much greater the mine have advised against writing your own cypher script for anything other then learning

If you want them to be able to decode your code then provide them with a key. so in your case:

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

I hope this code illustrates my suggestion:

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

For the code above i turned your original code into a function:

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

there are several great ways to do this in python. If your interested in cyptopgraphy then check out Udacities class cs387 applied cryptography

于 2012-12-24T05:32:26.193 回答