python 密码学实验RSA加密解密自动生成密钥含交互界面

实验目的

(1) 帮助学生掌握RSA公钥系统的密钥生成、加密和解密过程, 能够利用所学过的编程语言, 熟悉RSA公钥加密算法流程与编程实现加密算法. 掌握编程实现实际问题中的方法, 提高解决问题的能力.

(2) 要求学生掌握算法的程序实现的方法,能应用密码算法的特点, 设计合适的交互界面, 并能正确实现应用编程.

(3) 要求学生掌握用规范的方法书写实验报告.

实验仪器设备/实验环境

(1) PC Windows操作系统, 使用python语言编程, 或者使用数学软件MATLAB、Maple编程.

RSA公钥密码原理

任务: Alice要求Bob将信息m用RSA方法加密传送回来.

(1) 密钥生成: Alice找到大素数p,q, 令n=pq, 取e>1满足, 再找d使得, 然后Alice将n, e作为加密密钥(公钥)发送给Bob, 这里p, q, d, 都是私钥, 要求保密, 用作解密;

(2) 加密: Bob 将明文m<n加密得到密文, 并将密文Em传送给 Alice;

(3) 解密: Alice收到后密文Em, 计算 , 恢复明文m.

实验内容

实验原理已包含实验内容, 下面所说的只是实验内容实现的难点和要点:

 设计输入密钥生成界面;

开始时明文可不用字符直接用数字.

代码:

import random
from tkinter import *
from tkinter.font import *
import tkinter.messagebox
import re


def is_sushu(Num):
    for i in range(2,int(Num**0.5)+1):
        if Num % i ==0:
            return False
    return True

def is_husu(a,b):
    if a > b:
        index=b
    elif a <b :
        index=a
    else:
        return False
    for i in range(2,index):
        if a%i==0 and b%i==0:
            return False
    return True

def RSA():
    while True:
        p=random.randint(200,399)
        if is_sushu(p):
            break
    while True:
        q=random.randint(200,399)
        if is_sushu(q):
            break
    n=p*q
    nn=(p-1)*(q-1)
    while True:
        e=random.randint(2,nn)
        if is_husu(e,nn):
            break
    d=1
    while True:
        if (d*e)%nn==1:
            break
        d+=1
    return "公钥KU = ({},{})".format(e,n),"私钥KR = ({},{})".format(d,n)

def Create_Window():    #窗口
    window = Tk()
    window.title("Python")
    window.geometry('400x400')
    ft = Font(family='黑体', size=8, )
    F_bg="white"
    T_bg="#FFFFCC"
    H_bg="gold"

    def Create_RSA():
        Public_key,Secret_key = RSA()
        T_RSA.delete(1.0,END)
        T_RSA.insert(INSERT,Public_key)
        T_RSA.insert(INSERT,"\n")
        T_RSA.insert(INSERT, Secret_key)

    F_RSA = Frame(window, bg=F_bg,highlightbackground=H_bg,highlightthickness=0)
    F_RSA.place(x=0, y=0, width=400, height=750)
    T_RSA = Text(F_RSA, bg=T_bg, bd=0, font=ft, highlightcolor=H_bg, spacing1=10, spacing2=5,highlightbackground=H_bg,highlightthickness=1)
    T_RSA.place(x=100, y=5, width=200, height=50)
    B_RSA = Button(F_RSA, text="生成", relief=RAISED,font=ft, command=Create_RSA,highlightbackground=H_bg,highlightthickness=1)
    B_RSA.place(x=182, y=57, width=35, height=15)

    def Encryption():
        Plaintext=Encryption_Plaintext.get('1.0', END)[:-1]
        Public_key=Encryption_key.get('1.0', END)[:-1]
        if Plaintext:
            if Public_key:
                print(Public_key)
                Public_key = re.findall(r'\([0-9]+\,[0-9]+\)', Public_key)
                ciphertext = []
                if Public_key:
                    Public_key = Public_key[0].replace("(", "").replace(")", "").split(",")
                    e = int(Public_key[0])
                    n = int(Public_key[1])
                    for i in Plaintext:
                        ciphertext.append(str(int((ord(i) ** e) % n)))
                    Encryption_ciphertext.insert(INSERT, " ".join(ciphertext))
                else:
                    tkinter.messagebox.showerror('错误', '公钥格式为(e,n)', parent=window)
            else:
                tkinter.messagebox.showerror('错误', '公钥不能为空!', parent=window)
        else:
            tkinter.messagebox.showerror('错误', '明文不能为空!', parent=window)

    F_encryption = Frame(window, bg=F_bg,highlightbackground=H_bg,highlightthickness=0)
    F_encryption.place(x=0, y=75, width=400, height=162)
    Encryption_Plaintext = Text(F_encryption, bg=T_bg, bd=0, font=ft, highlightcolor=H_bg, spacing1=10, spacing2=5,highlightbackground=H_bg,highlightthickness=1)
    Encryption_Plaintext.place(x=72, y=5, width=250, height=50)
    Encryption_key = Text(F_encryption, bg=T_bg, bd=0, font=ft, highlightcolor=H_bg, spacing1=10, spacing2=5,highlightbackground=H_bg,highlightthickness=1)
    Encryption_key.place(x=72, y=70, width=250, height=30)
    Encryption_ciphertext = Text(F_encryption, bg=T_bg, bd=0, font=ft, highlightcolor=H_bg, spacing1=10, spacing2=5,highlightbackground=H_bg,highlightthickness=1)
    Encryption_ciphertext.place(x=72, y=105, width=250, height=50)
    B_encryption = Button(F_encryption, text="加密", relief=RAISED, font=ft, command=Encryption,highlightbackground=H_bg,highlightthickness=1)
    B_encryption.place(x=182, y=85, width=35, height=15)

    def Decrypt():
        Ciphertext = Decrypt_Ciphertext.get('1.0', END).split()
        Secret_key = Decrypt_key.get('1.0', END)
        if Ciphertext:
            if Secret_key:
                print(Secret_key)
                Secret_key = re.findall(r'\([0-9]+\,[0-9]+\)', Secret_key)
                Plaintext = []
                if Secret_key:
                    Secret_key = Secret_key[0].replace("(", "").replace(")", "").split(",")
                    d = int(Secret_key[0])
                    n = int(Secret_key[1])
                    for i in Ciphertext:
                        Plaintext.append(chr((int(i)**d)%n))
                    Decrypt_Plaintext.insert(INSERT, "".join(Plaintext))
                else:
                    tkinter.messagebox.showerror('错误', '密钥格式为(e,n)', parent=window)
            else:
                tkinter.messagebox.showerror('错误', '密钥不能为空!', parent=window)
        else:
            tkinter.messagebox.showerror('错误', '密文不能为空!', parent=window)


    F_decrypt = Frame(window, bg=F_bg,highlightbackground=H_bg,highlightthickness=0)
    F_decrypt.place(x=0, y=237, width=400, height=162)
    Decrypt_Ciphertext = Text(F_decrypt, bg=T_bg, bd=0, font=ft, highlightcolor=H_bg, spacing1=10, spacing2=5,highlightbackground=H_bg,highlightthickness=1)
    Decrypt_Ciphertext.place(x=72, y=5, width=250, height=50)
    Decrypt_key = Text(F_decrypt, bg=T_bg, bd=0, font=ft, highlightcolor=H_bg, spacing1=10, spacing2=5,highlightbackground=H_bg,highlightthickness=1)
    Decrypt_key.place(x=72, y=60, width=250, height=30)
    Decrypt_Plaintext = Text(F_decrypt, bg=T_bg, bd=0, font=ft, highlightcolor=H_bg, spacing1=10, spacing2=5,highlightbackground=H_bg,highlightthickness=1)
    Decrypt_Plaintext.place(x=72, y=105, width=250, height=50)
    B_Decrypt = Button(F_decrypt, text="解密", relief=RAISED, font=ft, command=Decrypt,highlightbackground=H_bg,highlightthickness=1)
    B_Decrypt.place(x=182, y=85, width=35, height=15)


    window.mainloop()

Create_Window()

 代码执行:

  • 7
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hares_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值