歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Python2.6下基於rsa的加密解密

Python2.6下基於rsa的加密解密

生成公鑰的私鑰:

# -*- coding: UTF-8 -*-
import rsa
import base64

(public_key, private_key) = rsa.newkeys(1024)
print(chr(10)+"公鑰:")
print(public_key.save_pkcs1())
print(chr(10)+"公鑰:")
print(public_key.save_pkcs1())

執行結果:

公鑰:
-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAI0g4ds76htToOb2nrI65L2S5slIe4ZEedbp+vrZq71juhiGZipmYWic
AfQFKrDiaAlVvT+GY7l9neXXpnjbpUVOV2O7BZy0KOtAKySyAxZyuXUA6KHn1xN+
wwjyttj2uXH4s74mgBaUiwvWTHO0CJEWFQ5xMOQyqQosiVFn9AqFAgMBAAE=
-----END RSA PUBLIC KEY-----


公鑰:
-----BEGIN RSA PRIVATE KEY-----
MIICYQIBAAKBgQCNIOHbO+obU6Dm9p6yOuS9kubJSHuGRHnW6fr62au9Y7oYhmYq
ZmFonAH0BSqw4mgJVb0/hmO5fZ3l16Z426VFTldjuwWctCjrQCsksgMWcrl1AOih
59cTfsMI8rbY9rlx+LO+JoAWlIsL1kxztAiRFhUOcTDkMqkKLIlRZ/QKhQIDAQAB
AoGAWxMOTgGzSWFFo+chcJlrBh/oCZtaA8Lq8dNbYUjZeMYD0VHiWphpYi3TYNt4
ul/Rjbyj/XvHnWvAWxQ4Se2kJT/R6y/KJ76DIJfIVEOvQ6ERcjCcFvWU3Ek4RrjH
5LGy/LxOnel00EQGm4qgNqcgJ2WBMCGUVNCHJ52yDlshXUECRQDkHiZKlBIveWj2
O//jSv7JuqvBX2oA/MGViCuzdQjyK2YV+ccWcmIwzOIwkbMdfGTAt7sGIGQLZ52q
iQiVvXNOHvBlUQI9AJ5g0cOHCo2Pc6YMwhKL1aCdEquQR66f+1l4FaOM/PSYQEfR
/qlfuQFi7rP047XhdOqZG3phaQRNKTzU9QJFAN7vWByqeNtMm/eIy5smP7qGZDSn
OsGcQtQQMCVBy2xgXXQ8lwSo5K0sjiFJQvMeDpkI9g4Wua8rpDp1kZ5Fd9pxAh8B
AjwbMRxSqbjSHTMJ617WuFNxRAaUliz4elc5QTjV37r+c7V1n0RN6DVnW4kINJ9z
9sWQEcF5/Yd+SZEu9JECRQCkFQYgrTV2CSohm47M170TdxpkiHije45IUBMIHFya
VNCq7ENF3P2OrqroYLZOkhw3ByxVWPPXxHbRzL38dcWnPHB3QQ==
-----END RSA PRIVATE KEY-----


 

使用公鑰和私鑰來加密和解密:

# -*- coding: UTF-8 -*-
import rsa
import base64

public_key_str = """-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAJ6m+aWR00650XWRNzAcy2ywCS5UAyPC4RVhlAP0BOtjegv5rHFgs/Wg
C+lHUCwQ6/vnS9uebcnlSCjldMQRqGLoCyHdvuN0USWBRsJkCsYXWNEuoiwZ3RUQ
EJuhGBjhy7bAN80SqTIUoPgMSfHCe1ymi3ppuskAOTfOR3KjhvlFAgMBAAE=
-----END RSA PUBLIC KEY-----"""

private_key_str = """-----BEGIN RSA PRIVATE KEY-----
MIICYQIBAAKBgQCepvmlkdNOudF1kTcwHMtssAkuVAMjwuEVYZQD9ATrY3oL+axx
YLP1oAvpR1AsEOv750vbnm3J5Ugo5XTEEahi6Ash3b7jdFElgUbCZArGF1jRLqIs
Gd0VEBCboRgY4cu2wDfNEqkyFKD4DEnxwntcpot6abrJADk3zkdyo4b5RQIDAQAB
AoGAHQciEEgxKGtZRrCOL3BlS/qdg2t9s5JZiobzBRIlwEfQMda51XjDFIL3CvSw
V4+1Db8RIxrGrbUU0d7Bsj2r9l31jCcZvf3ohqRklcWZ/OF7ndL1pHq1yOR3jqVY
JcVie7OZmAt6dqT7FqzbapdxoU5tMmILI9hQBhwhC+puXPUCRQCrD6APLQvgioTK
kkh3iaMBgH/sU2aRk50q7kQhS8XIs1G3tJihBxveXKkflllW/Nxr6EDKBDgmvzEr
SVeNOZGdXZ13lwI9AO1uAp+QopQ6nmsYCApCihnVCJ8hcvERokm9Wgcadfr/fW9d
HwDdCB/YKDwNHTObExhyERBeo1TMo0RRgwJEP5PxI3LUpUIWlMvFz1gCk75UzVs6
FgVNNvWTsORewHeVebfPupnPy9eYrDrPPbuBmUGbQvpKfGw3NCVwOvcYnep7akUC
PQCSb1sm1qmvCkhSfMvYqBlMvVtH6fVeQSX6nNI9t1A0sgbG/IP2oFw2Z7bI8r2j
6mzoktF7ayMJVf0MUckCRQCJPNzi+INd9rXghnDIpx49gMML719k8fkLlpwOD2Se
8PKB5W18W0u8ECrHS906JZFESKLqVYWqmM2jgBVTOp7XzdjWfA==
-----END RSA PRIVATE KEY-----"""

private_key = rsa.PrivateKey.load_pkcs1(private_key_str)
public_key = rsa.PublicKey.load_pkcs1(public_key_str)
msg = "abc12344"

encrypt_msg = base64.encodestring(rsa.encrypt(msg, public_key))
decrypt_msg = rsa.decrypt(base64.decodestring(encrypt_msg), private_key)
print(chr(10)+"原始文本:")
print(msg)
print(chr(10)+"加密後的文本:")
print(encrypt_msg)
print(chr(10)+"解密後的文本:")
print(decrypt_msg)

執行結果:

原始文本:
abc12344

加密後的文本:
cxQ/2bz6uwRsgcGhTzrP07BMu5wUAhmfqHgvXmDkCKgxZ2OOae2d20uGJXxhnhoNzyDCg4tRB8Eb
GlvpOD/uSUT6OD9ov5adPfilHV9t3Ve8R1CFMrAm37jrA8sjmdtQ8MSMgweyns5m2lN3aFoDCVcL
QRJAuKLa3+s3n/iUUeI=


解密後的文本:
abc12344

PS: 某些RSA模塊生成的公鑰的頭為“-----BEGIN PUBLIC KEY-----”, 這種RSA頭無法正常導入生成public key,需要標准的“-----BEGIN RSA PUBLIC KEY-----”。

通過RSA加密後的密文通常很難使用,使用base64來進行編碼處理,方便在各個系統之間傳遞。

BASE64是一種編碼方式,通常用於把二進制數據編碼為可寫的字符形式的數據。BASE64是一種可逆的編碼方式。

Copyright © Linux教程網 All Rights Reserved