python - 如何使我的代码与最初的目的相反
问题描述
我在 python 中有一个反向密码代码,我想反转它,以便它解码反转的字符串:
代码:
message = text.get()
translated = (" ")
i = len(message) - 1
while i >= 0:
translated = translated + message[i]
i = i - 1
print(translated)
编辑:
message = text.get()
translated = (" ")
i = len(message) - 1
while i >= 0:
translated = translated + message[i]
i = i - 1
print(translated)
# Creating a string
s = message
# Encoding the string into bytes
b = s.encode("UTF-8")
# Base32 Encode the bytes
e = base64.b32encode(b)
# Decoding the Base32 bytes to string
s1 = e.decode("UTF-8")
# Printing Base32 encoded string
print(s1)
messagebox.showinfo("Encrypted", s1)
def decrypt():
base64.b32decode(s1)
translated[::-1]
以上是加密和解密的全部代码
解决方案
您可以使用 python 切片
translated[::-1] # reverses the "translated"
例如,如果你输入“你好”,它变成了olleh
,如果你想再次反转它(解码)你所要做的就是使用拼接 "olleh"[::-1]
,它给出hello
message = input("enter msg: ")
translated = (" ")
i = len(message) - 1
while i >= 0:
translated = translated + message[i]
i = i - 1
print("Encoded: ", translated)
print("Decoded: ", translated[::-1]) # decode it back to normal by reversing it
结果:
Encoded: albalb
Decoded: blabla
编辑(操作在评论中发布了完整的代码):
我相信这就是你要找的。
#Encryption GUI
from tkinter import *
from tkinter import messagebox
import base64
"""
You have two functions right? one is `encrypt` and the other is `decrypt`.
Now variable `e` which contains the encypted msg it only visible to the scope of `encrypt` function.
Meaning that `decrypt` does not know that it exists at all.
"""
# a global variable to store
# both encrypt and decrypt will be able to see this variable
message = ""
#Sub-Routine
def encrypt():
# Tell python to find the global variable `message`
global message
# get input and reverse it using splice
# "blabla"[::-1] => "albalb"
message = text.get()[::-1]
# Encoding the message into bytes
message = message.encode("UTF-8")
# Base32 Encode the bytes
message = base64.b32encode(message)
# temporarily decode and print encrpyted msg
# decoding is to make it human-readable (it's only in this function, won't affect anything else)
e = message.decode("UTF-8")
messagebox.showinfo("Encrypted", e)
def decrypt():
# again, tell python to find the global variable
# we need it! it contains our secret message
global message
# decode message
message = base64.b32decode(message)
# finally print it
print("Decrypted", message[::-1].decode("UTF-8"))
#Main
root=Tk()
root.geometry("500x425")
root.title("HORIZON Encryption")
root.resizable(True,True)
root.configure(bg='gray95')
#Frame Heading
frame_heading=Frame(root)
frame_heading.grid(row=0,column=0,columnspan=3,padx=30,pady=5)
frame_entry=Frame(root)
frame_entry.grid(row=1,column=0,columnspan=3,padx=25,pady=10)
#Labels
Label(frame_heading,text="HORIZON Encryption").grid(row=0,column=0,padx=0,pady=5)
Label(frame_entry,text="Text: ").grid(row=0,column=0,padx=10,pady=10)
text=Entry(frame_entry,width=15,bg='white')
text.grid(row=0,column=1,padx=5,pady=5)
#Buttons
encrypt_button=Button(root,text="Encrypt",width=7,command=encrypt)
encrypt_button.grid(row=2,column=1,padx=0,pady=5)
decrypt_button=Button(root,text="Decrypt",width=7,command=decrypt)
decrypt_button.grid(row=2,column=2,padx=0,pady=5)
root.mainloop()
结果:
msg: test
Encrypted ORZWK5A=
Decrypted test
推荐阅读
- maven - 什么配置或变量导致 Aritfactory 用时间戳替换“-SNAPSHOT”
- reactjs - DeleteOne by _id Mongodb
- javascript - Why is my string getting repeatedly in this function?
- python - 我的浏览器和获取请求之间的 html 图像源不同
- mysql - 应用引擎+Postgresql 出现“连接被拒绝”错误
- mongodb - 是否可以查看在 MongoDB 云上运行的确切查询?
- haskell - Haskell 迭代 (f :: IO a -> IO a)
- javascript - 用js打开和关闭modals的更有效方法
- python - 扑克中的对子概率
- android - 致命错误:在 ubuntu 中找不到“libintl.h”文件