如何使我的代码与原始目的相反

时间:2019-01-06 20:50:25

标签: python python-3.x

我在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]

以上是加密和解密的整个代码

2 个答案:

答案 0 :(得分:0)

您可以使用python slices

translated[::-1] # reverses the "translated"

例如,如果您输入“ hello”,它将变为olleh,而如果您想再次反转(解码),则只需使用splicing "olleh"[::-1]即可得到{{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

编辑(op在评论中张贴了完整的代码):

我相信这就是您想要的。

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()

答案 1 :(得分:0)

更紧凑的方法可能是:

def reverse(mssg):
    return mssg[::-1]

print reverse("hello")

输出:

>> "olleh"