1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python rsa加密解密和base64编解码介绍

python rsa加密解密和base64编解码介绍

时间:2023-10-19 16:51:51

相关推荐

python rsa加密解密和base64编解码介绍

后端开发|Python教程

python rsa加密解密 base64编解码

后端开发-Python教程

最近有需求,需要研究一下RSA加密解密安全;在网上百度了一下例子文章,很少有文章介绍怎么保存、传输、打印加密后的文本信息,都是千篇一律的。直接在一个脚本,加密后的文本信息赋于变量,然后立马调用解密。仔细想了一下RSA加密解密的过程,确定有二端,一端为:加密端,一端为解密端,一般不在同一台机器。在这里,我只模拟了保存在文件,然后再读出来;关于怎以通过网络传输,也是大同小异。

传奇端源码下载,文件不能拖入ubuntu,如何配置tomcat7环境,兰花根部有爬虫,php上传了一个txt文档,长春seo付费lzw

用RSA加密后的密文,是无法直接用文本显示,因为存在一些无法用文本信息编码显示的二进制数据。对于保存,网络传输,打印不乱码,需要通base64编码进行转换;base64编解码能把一些无法直接用文件本信息编码的二进制数据,转换成常规的二进制数据。

验证系统 源码,ubuntu进程崩溃日志,mac使用数据爬虫,soapvar php,弗兰克先生seolzw

#/usr/bin/env python# -*- coding: utf-8 -*-import rsaimport sysimport base64# 打印 python 版本 与 windows 系统编码print("---- 1 ----")print(sys.version)print(sys.getdefaultencoding())print(sys.getfilesystemencoding())# 先生成一对密钥,然后保存.pem格式文件,当然也可以直接使用print("---- 2 ----")(pubkey, privkey) = rsa.newkeys(1024)pub = pubkey.save_pkcs1()print(type(pub))pubfile = open(public.pem,w+)pubfile.write(pub.decode(utf-8))pubfile.close()print("---- 3 ----")pri = privkey.save_pkcs1()print(type(pri))prifile = open(private.pem,w+)prifile.write(pri.decode(utf-8))prifile.close()# load公钥和密钥print("---- 4 ----")message = dPabdbGDpFTrwwgydVafdlsadlfsal%46645645sprint(message:,type(message))with open(public.pem) as publickfile: p = publickfile.read() print(type(p)) pubkey = rsa.PublicKey.load_pkcs1(p.encode(utf-8))with open(private.pem) as privatefile: p = privatefile.read() print(type(p)) privkey = rsa.PrivateKey.load_pkcs1(p.encode(utf-8))# 用公钥加密、再用私钥解密crypto = rsa.encrypt(message.encode(utf-8),pubkey)print(crypto)print("---- 5 ----")print(crypto:,type(crypto))print(cry_base64:,base64.encodestring(crypto))print(cry_base64_utf8:,base64.encodestring(crypto).decode(utf-8))# 保存到本地文件cry_file = open(cry_file.txt,w+)cry_file.write(base64.encodestring(crypto).decode(utf-8))cry_file.close()print("---- 6 ----")# 从本地文件读取cry_file = open(cry_file.txt, )cry_text = \for i in cry_file.readlines(): cry_text += iprint(cry_text_type:,type(cry_text))print(cry_text:,cry_text)print(cry_base64:,cry_text.encode(utf-8))crypto_tra = base64.decodestring(cry_text.encode(utf-8))print("---- 7 ----")assert crypto == crypto_traprint(crypto)print("---- 8 ----")plaintext = rsa.decrypt(crypto,privkey)assert message == plaintext.decode(utf-8)print(plaintext.decode(utf-8))

h5网络游源码,ubuntu装系统黑屏,tomcat闪退是怎么回事,梦见爬虫在咬我,php实现登录和注册超详细,游戏公司seolzw

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。