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是一种可逆的编码方式。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/476124b3e332bef38a5b3d5cfa474741.html