一、RSA加密算法
RSA加密算法是一种非对称加密算法,加密的秘钥是由公钥和私钥两部分组成秘钥对,公钥用来加密消息,私钥用来解密消息,公钥是公开的,给对方进行加密,私钥则是用户自己保留,用来对加密的数据进行解密。
公钥pem文件格式:以-----BEGIN PUBLIC KEY-----标记打头,以-----END PUBLIC KEY-----标记结尾,中间部分即为公钥
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDM6dGIA2naE3nGXi6O9vHskRSj
uC5hn30msl+bk1yJl4NUqH1et7S3C0NWMDHPkC0vyWtp8HTpV1bFN747zToegLOs
n1+zsf1LSObVce+UU76w1J3oAHBPCHSTt+WuTe+FMbQzvX1CQAdzdqCvYFvb+b9Y
+L+ajto9raO0cfdGawIDAQAB
-----END PUBLIC KEY-----
私钥pem文件格式:以-----BEGIN RSA PRIVATE KEY-----打头,以-----END RSA PRIVATE KEY-----结尾,中间部分即为私钥
-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQDM6dGIA2naE3nGXi6O9vHskRSjuC5hn30msl+bk1yJl4NUqH1e
t7S3C0NWMDHPkC0vyWtp8HTpV1bFN747zToegLOsn1+zsf1LSObVce+UU76w1J3o
AHBPCHSTt+WuTe+FMbQzvX1CQAdzdqCvYFvb+b9Y+L+ajto9raO0cfdGawIDAQAB
AoGACjUBb22AGdFLiRKK7isl3NtTNF6JzOoVYVaURZsfCmmGLLLh39R1x4Jjncjz
yjuLzKwTUikcobzsgRFoQ/FSmjiMkQcIFoE20qJiYOOnB8T346OZfyWSMQqTAWTa
KnT1Ih74x2aft4Sf3NwkDpqEqU4nksEWmebZ+ynStaCaHKkCQQDj1FN0D8b2knbl
m0SXvDwhKX4R6U+ZZtDR1XwLwlFuz9xSFfuQ797O91pJwHagB8jWkCSYYDeIDyYd
hQCDcRIzAkEA5kAcnyryeORaMbuvHqq+fulff+u0bp4Crply4qKI5PV2wVskQ1z0
DzRyao8n9rED+kxYM5E5HXc4q9nkzUFy6QJAauTrRvBEHNp5eJ9SHpNN7w1Fvmem
VkY8QceVjPreNEzIhacUwlsZQigiL6pivhEI9UrqMKgsReU9r+oAZKt/qwJAU5Pg
D9K6EzHon3beOUaHQXAa70cyxphqptAiiKuw/1MumT8pKZ6bJM3H3nA6dGyK4hdG
SOmlDtIQ6yh7yhnAyQJAPKCoBoiR6f+feOwBrrh0KTbHucZygH4y9bPW+tz5nrwt
ChypQ8vu7qG7Arcw9jYJ2HJFXHeQ3Hx2FjhruYfwew==
-----END RSA PRIVATE KEY-----
二、如何使用Python进行RSA加密解密
1、模块安装文章来源:https://www.toymoban.com/news/detail-694261.html
pip install pycryptodome
2、实例文章来源地址https://www.toymoban.com/news/detail-694261.html
from Crypto import Random
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
import base64
# 伪随机数生成器
random_generator = Random.new().read
# rsa算法生成秘钥对实例对象:1024是秘钥的长度
rsa = RSA.generate(1024, random_generator)
#生成私钥并保存到pem文件,如果知道秘钥值key3也可以不保存,拼接上begin/end标识后加载直接用,如key3 = '-----BEGIN PUBLIC KEY-----\n' + key3 + '\n' + '-----END PUBLIC KEY-----'
private_pem = rsa.exportKey()
with open("private.pem", "wb") as f:#wb,以二进制格式写入文件
f.write(private_pem)
#生成公钥并保存到pem文件,当然也可以不保存直接用,即key1
public_pem = rsa.publickey().exportKey()
with open("public.pem", "wb") as f: #wb,以二进制格式写入文件
f.write(public_pem)
#plaintext:需要加密的明文文本,公钥加密,私钥解密
plaintext = '123456'
#加密过程
# 1:加载公钥
with open("public.pem") as f1:
key1 = f1.read() #从pem文件中读取公钥内容,包括begin头/end尾标识
public_key = RSA.import_key(key1) #加载公钥key1
#:2:加密
cipher_rsa = PKCS1_v1_5.new(public_key)
cipher_text = base64.b64encode(cipher_rsa.encrypt(plaintext.encode('utf-8'))) #cipher_text 即为加密后的密文
#打印加密结果
print(cipher_text.decode('utf-8'))#Python3的字符串的编码语言用的是unicode编码,由于Python的字符串类型是str,如果直接打印出来print(cipher_text),会是b'字符串',解决方案是后面添加decode('utf-8')
# 读取私钥
with open("private.pem") as f2:
key2 = f2.read()#从pem文件中读取私钥内容,包括begin头/end尾标识
private_key = RSA.import_key(key2)#加载私钥key2
# 解密
cipher_rsa = PKCS1_v1_5.new(private_key)
text = cipher_rsa.decrypt(base64.b64decode(cipher_text),random_generator)
#打印解密结果
print(text.decode('utf-8'))#Python3的字符串的编码语言用的是unicode编码,由于Python的字符串类型是str,如果直接打印出来print(text),会是b'字符串',解决方案是后面添加decode('utf-8')
到了这里,关于Python RSA加密解密的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!