首页 > 解决方案 > Encryption functions in python

问题描述

I have made this program in python that encrypts and decrypts strings you input into a scrolled textbox in Tkinter. The encryption goes by 13 characters, such as "A" is to "N" and "B" is to "O" etc. etc. The one thing this program does not do is include numbers and special symbols such as ($ & ^ *) and much more. I want to include all of the displayable characters in the ASCII table starting with a space " " through to the right curly bracket }.

Here is my program so far:

from tkinter import *
from tkinter.scrolledtext import *
root = Tk()
canvas = Canvas(root, width=800, height=600)

Here are the encrypt and decrypt functions that I have so far (this is most likely where I need to change something):

def encrypt_symbols(s):
    s = s.upper()
    output = ""
    for ch in s:
        if ch != " ":
            num = ord(ch) + 13
            if num > 90:
                num = num - 26
            output = output + chr(num)
        else:
            output = output + " "
    return output

def decrypt_symbols(s):
    s = s.upper()
    output = ""
    for ch in s:
        if ch != " ":
            num = ord(ch) - 13
            if num < 65:
                num = num + 26
            output = output + chr(num)
        else:
            output = output + " "
    return output

This part is just the Tkinter functions(they don't relate to my problem... I think :D):

label_plain = Label(root, text = "Plain Text")
label_encrypt = Label(root, text = "Encrypted Text")
label_plain.grid(row=0, column=0, padx=10, pady=10)
label_encrypt.grid(row=0, column=2, padx=10, pady=10)
textbox_plain = ScrolledText(root, width=30, height = 10,
                         bg = "#DDFFDD")
textbox_encrypt = ScrolledText(root, width=30, height = 10,
                           bg = "#DDDDFF")
textbox_plain.grid(row=1, column=0, padx=10, pady=10)
textbox_encrypt.grid(row=1, column=2, padx=10, pady=10)

Here are other parts of the encrypt/decrypt process (again I am fairly sure they aren't part of my problem):

def encrypt():
    plain = textbox_plain.get(1.0, "end-1c")
    e = encrypt_symbols(plain)
    textbox_encrypt.delete(1.0, "end")
    textbox_encrypt.insert(1.0, e)

def decrypt():
    e = textbox_encrypt.get(1.0, "end-1c")
    d = decrypt_symbols(e)
    textbox_plain.delete(1.0, "end")
    textbox_plain.insert(1.0, d)

frame1 = Frame(root)
frame1.grid(row=1, column=1)
be = Button(frame1, text = "Encrypt >>", command = encrypt)
bd = Button(frame1, text="<< Decrypt", command = decrypt)
be.grid(row = 0, column = 0, padx = 10, pady = 10)
bd.grid(row = 1, column = 0, padx = 10, pady = 10)

How can I go about this? I don't think it is a complicated fix but I have no clue at how to go about it...

Feel free to copy-paste and mess around with the program yourself!

Thank you!

标签: pythonencryptionascii

解决方案


It works basically just the same way you do it now, you just have to adapt your range of characters to encrypt. Also, it might be a bit easier using modulo, and you can define a common function for both directions.

def shift(char, amount, low, high):
    return chr((ord(char) - low + amount) % high + low)

def encrypt_symbols(s):
    return ''.join(shift(c, 13, 32, 126) for c in s)

def decrypt_symbols(s):
    return ''.join(shift(c, -13, 32, 126) for c in s)

a = "Shift me by 13 chars, please!"
b = encrypt_symbols(a)
c = decrypt_symbols(b)
print(b)
# `uvs-zr-o->@-pun9-}yrnr.
print(c == a)
# True

Note that this will shift characters into different "classes", e.g. letter to number, or punctuation to letter. If you want characters to remain in their respective class, this will be a bit more involved.


推荐阅读