RSA:非对称加密算法
加解密原理如下:
已知:p,q,n,e,d,m,c
其中:p与q互为大质数,n=p*q
公钥Pk(n,e):加密使用,是公开的
私钥Sk(n,d):解密使用,不公开
c:明文 m:密文
一般情况下取e=65537
要求: e与(p-1)*(q-1)互质
(e*d)mod((p-1)*(q-1))=1
那么: m = c**e mod n
c = m**d mod n
例如: Bob:公钥(50429,65537)
Alice:私钥(50429,46793)
其中:n=50429,e=65537,d=46793,p=239,q=211,c=37
求:(1)Bob发送给Alice的密文m
(2)Alice收到m解密后的明文c
解:(1)m = c**e mod 50429 = 37**65537 mod 50429 = 25804
(2)c = m**d mod 50429 = 25804**46793 mod 50429 = 37
Python实现:
#-*- coding: UTF-8 -*- __author__ = '007' __date__ = '2016/4/18' from Tkinter import * class RSA_hash(): rsa_hash_en = [] rsa_hash_de = [] def __init__(self): self.root = Tk() self.root.title("RSA加/解密器") self.root.geometry() Label(self.root, text="RSA加/解密器", font=("Arial",15), width=15).pack() self.frm = Frame(self.root) self.frm_T = Frame(self.frm) self.frm_Tp = Frame(self.frm_T) Label(self.frm_Tp, text="素数p:", bg="red", font=("Arial",15), width=6).pack(side=LEFT) self.var_p = Variable() Entry(self.frm_Tp,textvariable=self.var_p, font=("Arial",12), width=24).pack(side=RIGHT) self.frm_Tp.pack() self.frm_Tq = Frame(self.frm_T) Label(self.frm_Tq, text="素数q:", bg="orange", font=("Arial",15), width=6).pack(side=LEFT) self.var_q = Variable() Entry(self.frm_Tq,textvariable=self.var_q, font=("Arial",12), width=24).pack(side=RIGHT) self.frm_Tq.pack() self.frm_Te = Frame(self.frm_T) Label(self.frm_Te, text="公钥e:", bg="yellow", font=("Arial",15), width=6).pack(side=LEFT) self.var_Pke = Variable() Entry(self.frm_Te,textvariable=self.var_Pke, font=("Arial",12), width=24).pack(side=RIGHT) self.frm_Te.pack() self.frm_Td = Frame(self.frm_T) Label(self.frm_Td, text="私钥d:", bg="green", font=("Arial",15), width=6).pack(side=LEFT) self.var_Skd = Variable() Entry(self.frm_Td,textvariable=self.var_Skd, font=("Arial",12), width=24).pack(side=RIGHT) self.frm_Td.pack() self.frm_T.pack() self.frm_B = Frame(self.frm) self.frm_TB = Frame(self.frm_B) Label(self.frm_TB, text="请输入:", bg="cyan", font=("Arial",15), width=6).pack(side=LEFT) self.var_char = Variable() Entry(self.frm_TB,textvariable=self.var_char, font=("Arial",12), width=24).pack(side=RIGHT) self.frm_TB.pack() self.frm_MB = Frame(self.frm_B) self.rsa_show = Text(self.frm_MB, bg="lavender", font=("Arial",12), width=32, height=6) self.rsa_show.insert("1.0","") self.rsa_show.pack() self.frm_MB.pack() self.frm_BB = Frame(self.frm_B) Button(self.frm_BB, text="清除", command=self.clear, font=("Arial",12), bd=3).pack(side=LEFT) Button(self.frm_BB, text="加密", command=self.encrypt, font=("Arial",12), bd=3).pack(side=RIGHT) Button(self.frm_BB, text="解密", command=self.decrypt, font=("Arial",12), bd=3).pack(side=RIGHT) self.frm_BB.pack() self.frm_B.pack() self.frm.pack() def clear(self): self.var_char.set("") self.var_p.set("") self.var_q.set("") self.var_Pke.set("") self.var_Skd.set("") self.rsa_show.delete("1.0",END) def encrypt(self): self.rsa_show.delete("1.0",END) p = int(self.var_p.get()) q = int(self.var_q.get()) if self.isprimer(p,q): phi = (p - 1) * (q - 1) n = p * q else: self.rsa_show.insert("1.0","请重新输入p和q(要求p与q互素)") e = int(self.var_Pke.get()) if self.isprimer(e,phi): var_c = self.var_char.get() if var_c.isdigit(): c = int(self.var_char.get()) m = c ** e % n self.rsa_show.insert("1.0","密文为:%d" % m) else: for i in var_c: c = ord(i) if self.isprimer(e,phi): m = c ** e % n self.rsa_show.insert(END,"密文依次为:%d\n" % m) else: self.rsa_show.insert("1.0","e与phi互相不为素数,请重新输入公钥e") def decrypt(self): self.rsa_show.delete("1.0",END) p = int(self.var_p.get()) q = int(self.var_q.get()) if self.isprimer(p,q): phi = (p - 1) * (q - 1) n = p * q else: self.rsa_show.insert("1.0","请重新输入p和q(要求p与q互素)") d = int(self.var_Skd.get()) m = int(self.var_char.get()) e = int(self.var_Pke.get()) if self.isprimer(e*d,phi): c = m ** d % n if c in range(0,128): self.rsa_show.insert("1.0","明文为: %d 或者 %s" % (c,chr(c))) else: self.rsa_show.insert("1.0","明文为: %d" % c) else: self.rsa_show.insert("1.0","您输入的密钥d不满足要求,请重新输入私钥d") #判断是否互素 def isprimer(self,g,f): if g > f: s = f else: s = g L = [] for i in range(s,0,-1): if g % i==0 and f %i ==0: L.append(i) if max(L) == 1: return True else: self.var_p.set("") self.var_q.set("") def main(): rsa = RSA_hash() mainloop() if __name__ == "__main__": main()
下载 rsa_hash.exe(提取码:aef1)