实验目的

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

 代码执行:

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐