介绍
使用Python怎么生成一个rsa密钥对吗?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
具体如下:
#, - *安康;编码:utf-8 - * - rsa import #,先生成一对密钥,然后保存.pem格式文件,当然也可以直接使用 (pubkey, privkey),=, rsa.newkeys (1024) 时间=pub pubkey.save_pkcs1 () 时间=pubfile 开放(& # 39;public.pem& # 39;, & # 39; w + & # 39;) pubfile.write(酒吧) pubfile.close () 时间=pri privkey.save_pkcs1 () 时间=prifile 开放(& # 39;private.pem& # 39;, & # 39; w + & # 39;) prifile.write (pri) prifile.close () #,负载公钥和密钥 时间=message & # 39; lovesoo.org& # 39; with 开放(& # 39;public.pem& # 39;), as publickfile: 时间=p 才能;publickfile.read () 时间=pubkey 才能;rsa.PublicKey.load_pkcs1 (p) with 开放(& # 39;private.pem& # 39;), as privatefile: 时间=p 才能;privatefile.read () 时间=privkey 才能;rsa.PrivateKey.load_pkcs1 (p) #,用公钥加密,再用私钥解密 crypto =, rsa.encrypt(消息,pubkey) 时间=message rsa.decrypt(加密,privkey) print 消息 #,sign 用私钥签名认证,再用公钥验证签名 signature =, rsa.sign(消息,privkey,, & # 39; sha - 1 # 39;) rsa.verify (& # 39; lovesoo.org& # 39;,,签名,,pubkey)
对文件进行RSA加密解密
得到rsa.bigfile import * rsa import with 开放(& # 39;public.pem& # 39;), as publickfile: 时间=p 才能;publickfile.read () 时间=pubkey 才能;rsa.PublicKey.load_pkcs1 (p) with 开放(& # 39;private.pem& # 39;), as privatefile: 时间=p 才能;privatefile.read () 时间=privkey 才能;rsa.PrivateKey.load_pkcs1 (p) with 开放(& # 39;mysec.txt& # 39;,, & # 39; rb # 39;), as infile,,开放(& # 39;outputfile& # 39;,, & # 39; wb # 39;), as 输出文件:,#加密输出 encrypt_bigfile才能(infile,输出文件,,pubkey) with 开放(& # 39;outputfile& # 39;,, & # 39; rb # 39;), as infile2,,开放(& # 39;结果# 39;,,& # 39;wb # 39;), as outfile2:, #解密输出 decrypt_bigfile才能(infile2, outfile2,, privkey)
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。