一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python rsa 加密解密

python rsa 加密解密

2020-09-24 14:34cranezhou Python

本篇文章主要介紹了python rsa加密解密 (編解碼,base64編解碼)的相關知識。具有很好的參考價值,下面跟著小編一起來看下吧

最近有需求,需要研究一下RSA加密解密安全;在網(wǎng)上百度了一下例子文章,很少有文章介紹怎么保存、傳輸、打印加密后的文本信息,都是千篇一律的。直接在一個腳本,加密后的文本信息賦于變量,然后立馬調用解密。仔細想了一下RSA加密解密的過程,確定有二端,一端為:加密端,一端為解密端,一般不在同一臺機器。在這里,我只模擬了保存在文件,然后再讀出來;關于怎以通過網(wǎng)絡傳輸,也是大同小異。

用RSA加密后的密文,是無法直接用文本顯示,因為存在一些無法用文本信息編碼顯示的二進制數(shù)據(jù)。對于保存,網(wǎng)絡傳輸,打印不亂碼,需要通base64編碼進行轉換;base64編解碼能把一些無法直接用文件本信息編碼的二進制數(shù)據(jù),轉換成常規(guī)的二進制數(shù)據(jù)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#/usr/bin/env python
# -*- coding: utf-8 -*-
import rsa
import sys
import base64
# 打印 python 版本 與 windows 系統(tǒng)編碼
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%46645645s'
print('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','r')
cry_text = ''
for i in cry_file.readlines():
 cry_text += i
print('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_tra
print(crypto)
print("---- 8 ----")
plaintext = rsa.decrypt(crypto,privkey)
assert message == plaintext.decode('utf-8')
print(plaintext.decode('utf-8'))

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!

原文鏈接:http://www.cnblogs.com/52python/p/6589869.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: caoporn超碰最新地址进入 | 午夜理伦片免费 | 亚洲欧美另类第一页 | 互换身体全集免费观看 | 久久亚洲国产成人影院 | 成人高清网站 | 日本三不卡 | 亚洲国产精品自在自线观看 | 波多野结衣在线中文 | 奇米影视7777久久精品 | 久久午夜夜伦痒痒想咳嗽P 久久无码AV亚洲精品色午夜麻豆 | sao虎影院桃红视频在线观看 | 亚洲AV永久无码精品澳门 | 女人日男人 | 外国老少性配 | 日本一区二区三区久久精品 | 羞羞答答免费人成黄页在线观看国产 | 很黄的孕妇a级黄毛片 | h片在线看 | 久久AV国产麻豆HD真实乱 | 免费看国产一级特黄aa大片 | 成人亚洲欧美日韩中文字幕 | 国产三级精品91三级在专区 | 午夜久久影院 | blackedhd 18sex| 日本中文字幕高清 | 和日本免费不卡在线v | 久久精品亚洲牛牛影视 | 精品久久久噜噜噜久久久app | 高h文道具| 9久热久爱免费精品视频在线观看 | 国产精品成人网红女主播 | 成人免费一区二区三区在线观看 | 男人天堂资源 | 9久re在线观看视频精品 | 免费一级毛片完整版在线看 | 免费黄色小说 | 欧美一级特黄特色大片 | a级毛片毛片免费很很综合 a级黄色视屏 | 欧美亚洲综合另类 | 1024免费永久福利视频 |