代码环境,python3.5.2

RSA加密的过程是:使用公钥加密,私钥解密

RSA签名的过程是:使用私钥签名,公钥验证

所以核心代码就是,生成公钥私钥,使用公钥私钥分别进行加密解密。

在实际编码的时候,使用python自带的RSA库,需要特别处理编码问题,因为默认的加密解密函数是接收bytes类型的数据。

加密的核心代码如下:

# -*- coding: utf-8 -*-
import rsa
import base64
(public_key, private_key) = rsa.newkeys(1024)
msg = "M2Crypto python关于RSA的库还是蛮多的,当然也可以直接用openSSLqweasasd123"
msg = msg.encode(encoding="utf-8")
encrypt_msg = rsa.encrypt(msg, public_key) decrypt_msg = rsa.decrypt(encrypt_msg, private_key)
print("公钥:")
print(public_key.save_pkcs1().decode('utf8'))
print("\n私钥:")
print(private_key.save_pkcs1().decode('utf8'))
print("\n原始文本:")
print(msg.decode('utf8'))
print("\n加密后的文本:")
print(base64.encodebytes( encrypt_msg).decode('utf8'))
print("\n解密后的文本:")
print(decrypt_msg.decode('utf8'))

输出结果如下:

结合tkinter的代码如下,使用tkinter是因为绘制界面很简单:

# -*- coding:utf-8 -*-
from tkinter import *
import rsa
import base64 def GenerateKey():
(public_key, private_key) = rsa.newkeys(1024)
print("\n生成公钥:"+public_key.save_pkcs1().decode('utf8'))
print("\n生成私钥:"+private_key.save_pkcs1().decode('utf8'))
publicKeyText.delete(0.0, END)
publicKeyText.insert(END, public_key.save_pkcs1().decode('utf8')) privateKeyText.delete(0.0, END)
privateKeyText.insert(END, private_key.save_pkcs1().decode('utf8')) def EncryptionByPublickey(): #用公钥加密
public_key_str = publicKeyText.get("0.0", "end").encode(encoding="utf-8")
public_key = rsa.PublicKey.load_pkcs1(public_key_str) entry_str = entryText.get("0.0", "end").encode(encoding="utf-8") encrypt_msg = rsa.encrypt(entry_str, public_key)
print("公钥加密后的文本为:\n"+base64.encodebytes(encrypt_msg).decode('utf8'))
outputText.delete(0.0, END)
outputText.insert(END, base64.encodebytes(encrypt_msg).decode('utf8')) def EncryptionByPrivatekey():
private_key_str = privateKeyText.get("0.0", "end").encode(encoding="utf-8")
private_key = rsa.PrivateKey.load_pkcs1(private_key_str) entry_str = entryText.get("0.0", "end").encode(encoding="utf-8") encrypt_msg = rsa.encrypt(entry_str, private_key)
print("私钥加密后的文本为:\n"+base64.encodebytes(encrypt_msg).decode('utf8'))
outputText.delete(0.0, END)
outputText.insert(END, base64.encodebytes(encrypt_msg).decode('utf8')) def DeryptionByPublickey():
public_key_str = privateKeyText.get("0.0", "end").encode(encoding="utf-8")
public_key = rsa.PrivateKey.load_pkcs1(public_key_str) entry_str = entryText.get("0.0", "end").encode(encoding="utf-8")
encrypt_msg = base64.decodebytes( entry_str)
outputText.delete(0.0, END) try:
decrypt_msg = rsa.decrypt(encrypt_msg, public_key)
print("公钥解密后的文本为:\n"+decrypt_msg.decode('utf8'))
outputText.insert(END,decrypt_msg.decode('utf8'))
except:
decrypt_msg = "公钥解密失败"
print(decrypt_msg)
outputText.insert(END,decrypt_msg)
def DecryptionByPrivatekey():
private_key_str = privateKeyText.get("0.0", "end").encode(encoding="utf-8")
private_key = rsa.PrivateKey.load_pkcs1(private_key_str) entry_str = entryText.get("0.0", "end").encode(encoding="utf-8")
encrypt_msg = base64.decodebytes( entry_str)
outputText.delete(0.0, END) try:
decrypt_msg = rsa.decrypt(encrypt_msg, private_key)
print("私钥解密后的文本为:\n"+decrypt_msg.decode('utf8'))
outputText.insert(END,decrypt_msg.decode('utf8'))
except:
decrypt_msg = "私钥解密失败"
print(decrypt_msg)
outputText.insert(END,decrypt_msg) window = Tk()
window.title("RSA加密解密软件") frame = Frame(window)
frame.pack() label = Label(frame, text = "公钥:")
label.grid(row = 1, column = 1,columnspan= 4) publicKeyText = Text(frame,width=50,height=8)
publicKeyText.grid(row = 2, column = 1,columnspan = 4) label = Label(frame, text = "私钥:")
label.grid(row = 3, column = 1,columnspan= 4) privateKeyText = Text(frame,width=50,height=12)
privateKeyText.grid(row = 4, column = 1,columnspan = 4) btGenerateKey = Button(frame, text = "生成公钥/私钥",command=GenerateKey)
btGenerateKey.grid(row = 5, column = 1,columnspan = 4) label = Label(frame, text = "请输入加密/解密的文本:")
label.grid(row = 6, column = 1,columnspan = 4) entryText = Text(frame,width=50,height=5)
entryText.grid(row = 7, column = 1,columnspan = 4) btEncryptionByPublickey = Button(frame, text = "公钥加密",command=EncryptionByPublickey)
btEncryptionByPublickey.grid(row = 8, column = 1,pady = 10) btDeryptionByPublickey = Button(frame, text = "公钥解密",command=DeryptionByPublickey)
btDeryptionByPublickey.grid(row = 8, column = 2) btEncryptionByPrivatekey = Button(frame, text = "私钥加密",command=EncryptionByPrivatekey)
btEncryptionByPrivatekey.grid(row = 8, column = 3) btDecryptionByPrivatekey = Button(frame, text = "私钥解密",command=DecryptionByPrivatekey)
btDecryptionByPrivatekey.grid(row = 8, column = 4) outputText = Text(frame,width=50,height=5)
outputText.grid(row = 9, column = 1,columnspan = 4) print("欢迎使用本软件……")
GenerateKey();
mainloop() print("欢迎再次使用本软件……")

加密过程:

  使用公钥加密:

  使用私钥解密:

数字签名过程:

  使用私钥签名:

  使用公钥验证:

python tkinter 实现 带界面(GUI)的RSA加密、签名的更多相关文章

  1. 150+行Python代码实现带界面的数独游戏

    150行代码实现图形化数独游戏 Github地址,欢迎各位大佬们fork.star啥的,感谢: 今天闲着没事干,以前做过html+js版的数独,这次做个python版本的,界面由pygame完成,数独 ...

  2. python使用tkinter写带界面的工具

    python一般用来写纯脚本的居多,但也可以做有视图的产品出来,例如做网页和客户端工具.做成工具的好处是,让不懂代码的人也能使用,不需要去修改代码里面的参数,如果使用次数频繁,甚至比纯脚本跟节约时间: ...

  3. 【Python】 用户图形界面GUI wxpython I 基本用法和组件

    wxpython - 基本用法和组件 wxpython是python对跨平台GUI库wxWidgets的封装.wxWidgets是由C++写成的. wxpython被包装进了wx模块中,用它设计GUI ...

  4. python——Tkinter图形化界面及threading多线程

    Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.Tk和Tkinter可以在大多数的Unix平台下使用,同样可以应用在Windows和Macinto ...

  5. 【Python】 用户图形界面GUI wxpython IV 菜单&对话框

    更多组件 ■ 菜单栏 Menu 菜单是很多GUI必不可少的一部分.要建立菜单,必须先创建菜单栏: menuBar = MenuBar() menu = Menu() item1 = menu.Appe ...

  6. 【Python】 用户图形界面GUI wxpython III 更多组件

    wxpython - 更多组件 我写到的这些组件可能一来不是很详细,二来不是最全的,想要更好地用这些组件,应该还是去看看教程和别的示例.比较简单的,推荐http://download.csdn.net ...

  7. 【Python】 用户图形界面GUI wxpython II 布局和事件

    wxpython - 布局和事件 这章主要记录布局器Sizer以及事件的用法. // 目前还需要记录的:Sizer的Add方法加空白,Sizer的Layout,Sizer的Remove如何有效 ■ 布 ...

  8. Python开发【模块】:M2Crypto RSA加密、解密

    M2Crypto 模块 快速安装: # 环境centos7.0,提前装好openssl(自行百度安装),windows装不上,暂不考虑了 [root@localhost ~]# pip install ...

  9. JAVA RSA私钥 加密(签名) 对应 C# RSA私钥 加密(签名)

    非对称密钥RSA算法加解密在C#和Java之间交互的问题,这两天看了很多其他人写的文章,碰到了几个问题,最终解决问题. 参考地址:http://xw-z1985.iteye.com/blog/1837 ...

随机推荐

  1. 基于虚拟用户登录的ftp服务配置

    文章结构:             一.使用逻辑卷配置ftp数据存放目录             二.安装和配置vsftpd服务             三.使用不通权限的用户访问ftp服务器 系统环 ...

  2. 【数据驱动】python之mysql的操作

    1.准备工作 在本篇中,我们使用python版本为python3.7.在python3中,连接mysql数据库我们需要使用pymysql这个第三方库.我们可以直接使用pip命令来安装,安装的命令为: ...

  3. FZU 1876 JinYueTuan(排列组合)

    Description Let’s have a look at the picture below Now, do you know what it’s? Yeah , O(∩_∩)O~ , It ...

  4. object of type 'Response' has no len()

    看见没,这里括号弄错了! 网上解释是requests.get()得到的是一个response对象,无法用BeautifulSoup解析,如果要解析,解析对象应该是requests.get().cont ...

  5. ng2-file-upload插件在ionic3中的使用方法

    本文主要说明在ionic3中使用ng2-file-upload插件,上传多种类型文件(图片.word.excel.ppt.mp4等)的使用方法. 1.html代码: <button ion-bu ...

  6. 深入学习Redis主从复制

    一.主从复制概述 主从复制,是指将一台Redis服务器的数据,复制到其他的Redis服务器.前者称为主节点(master),后者称为从节点(slave):数据的复制是单向的,只能由主节点到从节点. 默 ...

  7. 四、附加到进程调试(.NET Framework)

    附加到进程调试: 1.需要在IIS配置环境并可运行即通过浏览器可打开. 2.找到项目w3wp.exe进程并附加到进程调试,点击项目添加断点,直接访问浏览器即可. 优点:w3wp.exe是已经运行的,调 ...

  8. oracle10G rac 10.2.0.1升级10.2.0.4

    前言 ocr版本查询指令:  ocrcheck vote盘路径查询指令: crsctl query css votedisk 相关指令参考来源:http://hzhg12345.blog.163.co ...

  9. 微信小程序学习一 微信小程序的四个基本文件

    微信小程序有四种类型的文件 js 类型文件 小程序的逻辑代码文件 小程序对js es6的处理比较友好,基本上我们的es6语法都需要使用babel插件去转化成es5(具体是什么原因,自己可以去了解一下) ...

  10. java--CharAt,StartWith

    public class CharAtStartWithDemo { public static void main(String[] args){ //jdk8 testCharAt();//1 t ...