1、公钥私钥产生的原理是什么?
2、如何用公钥私钥加密解密?
3、base64.encodebytes,,enconding=等的意思是?
https://www.zhuxianfei.com/python/32925.html
https://blog.csdn.net/weixin_32043233/article/details/113538616?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&utm_relevant_index=2
本文用rsa模块进行非对称加密
1、安装、导入rsa
#Python中要进行非对称加密,使用rsa模块可以大大简化加密处理过程
import rsa
#base64是什么?
import base64
2、生成公私钥匙对
# 1、接收者(A)生成512位公私钥对
# a. lemon_pub为PublicKey对象, lemon_priv为PrivateKey对象
# b. 512为秘钥的位数, 可以自定义指定, 例如: 128、256、512、1024、2048等
lemon_pub, lemon_priv= rsa.newkeys(512)
print(lemon_priv)
print(lemon_pub)
#此时,两者都是以十进制表示的。但是Priv含有4个数字,pu是两个数字.65537是什么数字?
#e=65537=(2^16+1) NIST SP800-78 Rev 1 (2007) 曾强调“不允许使用比65537更低的公钥指数e”
PrivateKey(7489282605249783285730956122510063520860890042624614549376519057228116020422755784679296016743690303830792279732057512075844603166401921052085606923217321, 65537, 3635450928800698165447898242602082042039572988174876217393460194509941785791466481782114286243854214918186979909772753593283946860151163753031950917093029, 4472819001923124896944326212116600243613928361840938014123975571393307409229526199, 1674398763292166603797515831348167909476630097502058361135770151698232479)
PublicKey(7489282605249783285730956122510063520860890042624614549376519057228116020422755784679296016743690303830792279732057512075844603166401921052085606923217321, 65537)
##此时的状态:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a2EQPjVf-1644985003563)(attachment:image.png)]
3、加密解密
# 发送者(可优)使用接收者(柠檬小姐姐)的公钥去加密消息
# rsa只能处理字节类型, 故字符串类型需要转化为字节类型
# aa="Lemon little girl, I love you very much!" 类型是str .encode('utf-8')后的类型是bytes
love_talk = "Lemon little girl, I love you very much!".encode("utf-8") #类型转换为bytes
cryto_info = rsa.encrypt(love_talk, lemon_pub) # 使用接收者(柠檬小姐姐)的公钥加密
print('明文=',love_talk)
print('暗文=',cryto_info)
明文= b'Lemon little girl, I love you very much!'
暗文= b'\x11`\xba~0@`\x86VR\x83\xb6\x07\x04\xb7\xe8`\xe0\xf03\xa2\x0b_H\xe4\x1c|.\xe2:\xea\x8b\x04(\x94\xbe\x8c\xa4i`*P<\x8a\x11\xfcz\xa4\xd3\xf2\xda\x12O-\xfa\xcc@@\xc0\xe3\xb64\x02\x1d'
先加密,用对方的公钥,方法是rsa.encrypt(aa, pubkey)。此时:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v9S3oNXM-1644985003565)(attachment:image.png)]
# 3. 接收者(柠檬小姐姐)使用自己的私钥去解密消息
talk_real = rsa.decrypt(cryto_info, lemon_priv)
talk_real = talk_real.decode("utf-8")
print(talk_real)
Lemon little girl, I love you very much!
再解密,接收者用自己的私钥来解密。
方法是rsa.decypt(aa,privkey),最后别忘记把bytes转为str,用decode(‘utf-8’)。此时结果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tZWlr2fW-1644985003567)(attachment:image.png)]
4、其他场景加密解密
1) 本地化保存、导入密钥
2) 自定义函数加密解密
3) 签名与验证
1) 本地化保存、导入密钥
保存方法:密钥.save_pksc1().decode()
导入方法:=rsa.PublicKey.lod_pkcs1(f.read().encode())
# 生成密钥
pubkey, privkey = rsa.newkeys(512)
# 保存密钥
print("==============保存密钥===============")
with open('public.pem' ,'w+') as f:
f.write(pubkey.save_pkcs1().decode())
with open('private.pem' ,'w+') as f:
f.write(privkey.save_pkcs1().decode())
#导入密钥
with open('public.pem' ,'r') as f:
pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
with open('private.pem' ,'r') as f:
privkey = rsa.PrivateKey.load_pkcs1(f.read().encode())
==============保存密钥===============
2) 自定义函数加密解密
"""
加密 RSA
"""
def rsa_encrypt(message):
crypto_email_text = rsa.encrypt(message.encode(), pubkey)
return crypto_email_text
text = rsa_encrypt("first test rsa")
print(text)
"""
解密
"""
def rsa_decrypt(message):
message_str = rsa.decrypt(message,privkey).decode()
return message_str
newmessage=rsa_encrypt("haha,one two three four smile99999!")
message = rsa_decrypt(newmessage)
print("\n",message)
b"\x1bx\xc4\xf4\x92\xa6\xf8\xf1\xff\x82P\xceT\xe1\x00\xd3\xb2\xda\x84\x9f\xcd\x14\x89p' \xca\x10\xbb\xc5\x08!k\x85\xeb\x9a :\xc5q\x06\xd2\x04\r\xab\x13\xf1\x9a\xb3q\x7f\x9d\xb9\xb4\xb0j\xe9\n\xec.\xe7\x96\xd4\x11"
haha,one two three four smile99999!
3) 签名、验证
(把一段消息,对方用私钥签名后,发来,用verify的公钥验证一下,看是不是匹配公钥的私钥)
签名方法:rsa.sign(message.encode(), privkey, 'SHA-1')
验证方法:rsa.verify(message.encode(), crypto_email_text, pubkey)
aaa='This is'
bbb=b'What?'
ccc=aaa.encode('utf-8')
ddd='现在几点了?'.encode('utf-8')
print(aaa,'\n',bbb,'\n',ccc,'\n',ddd)
This is
b'What?'
b'This is'
b'\xe7\x8e\xb0\xe5\x9c\xa8\xe5\x87\xa0\xe7\x82\xb9\xe4\xba\x86\xef\xbc\x9f'
"""
签名
"""
message = '这是重要指令:...'
crypto_email_text = rsa.sign(message.encode(), privkey, 'SHA-1')
print(crypto_email_text)
crypto_email_text=crypto_email_text
print(crypto_email_text)
"""
验证
"""
# 收到指令明文、密文,然后用公钥验证,进行身份确认
if rsa.verify(message.encode(), crypto_email_text, pubkey):
print ('OK')
else:
print ('NO')
# 但是如果不是的话,不会打印NO,而是报错
b"\x02ay\xeb7\xbc\x86R\x87sL\xb7'ImSx\xb5\xdd\xcc\xec\x85\xeb\x91\xc4\xd6\xef\xa2'D\xfe(\xc3\xf2\x8e\xe0\x07\xda\xe0Z\xfa\xe1\xef\xfd#\x1b\x85\x9d/\xd3\xcf\t\x15(\xfdM9V\xac\xe1kfu."
b"\x02ay\xeb7\xbc\x86R\x87sL\xb7'ImSx\xb5\xdd\xcc\xec\x85\xeb\x91\xc4\xd6\xef\xa2'D\xfe(\xc3\xf2\x8e\xe0\x07\xda\xe0Z\xfa\xe1\xef\xfd#\x1b\x85\x9d/\xd3\xcf\t\x15(\xfdM9V\xac\xe1kfu."
OK
5、加密过程的封装
# 导入base64模块来进行base64编码
import base64
import rsa
class HandleSign:
# 定义<a href="http://www.zhuxianfei.com/server/" target="_blank" class="infotextkey">服务器</a>公钥, 往往可以存放在公钥文件中
server_pub = """
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQENQujkLfZfc5Tu9Z1LprzedE
O3F7gs+7bzrgPsMl29LemonPYvIG8C604CprLittlenJpnhWu2lGirlWZyLq6sBr
tuPorOc42+gInFfyhJAwdZB6Sqlove7bW+jNe5youDtU7very6Gx+muchGo8Dg+S
kKlZFc8Br7SHtbL2tQIDAQAB
-----END PUBLIC KEY-----
"""
@classmethod
def to_encrypt(cls, msg, pub_key=None):
"""
非对称加密
:param msg: 待加密字符串或者字节
:param pub_key: 公钥
:return: base64密文字符串
"""
if isinstance(msg, str): # 如果msg为字符串, 则转化为字节类型
msg = msg.encode('utf-8')
elif isinstance(msg, bytes): # 如果msg为字节类型, 则无需处理
pass
else: # 否则抛出异常
raise TypeError('msg必须为字符串或者字节类型!')
if not pub_key: # 如果pub_key为空, 则使用全局公钥
pub_key = cls.server_pub.encode("utf-8")
elif isinstance(pub_key, str): # 如果pub_key为字符串, 则转化为字节类型
pub_key = pub_key.encode('utf-8')
elif isinstance(pub_key, bytes): # 如果msg为字节类型, 则无需处理
pass
else: # 否则抛出异常
raise TypeError('pub_key必须为None、字符串或者字节类型!')
public_key_obj = rsa.PublicKey.load_pkcs1_openssl_pem(pub_key) # 创建 PublicKey 对象
#2.创建 PublicKey 对象
#public_key_obj = rsa.PublicKey.load_pkcs1(pub_key)
cryto_msg = rsa.encrypt(msg, public_key_obj) # 生成加密文本
cipher_base64 = base64.b64encode(cryto_msg) # 将加密文本转化为 base64 编码
return cipher_base64.decode() # 将字节类型的 base64 编码转化为字符串类型
if __name__ == '__main__':
# 待加密字符串或者字节
love_talk = "Lemon little girl, I love you very much!"
#1.用自己生成的publickye测试下
#lemon_pub,lemon_priv=rsa.newkeys(512)
#lemon_pub2=lemon_pub.save_pkcs1()
# 调用to_encrypt类方法来进行加密
cryto_info = HandleSign.to_encrypt(love_talk)
print(cryto_info)
CHAlkWMMLnw+UkOZFwuRP8Q+TtQ50y9H0+RHHE6macU4Gc8XmzXpPrr7fQyRxRv2N9zFqTudSnR07TT3qxKPTCrZlQxFMAu02CCEX5RJ3zztPSYFE6gQ/Qi1jo8XHqBjRZ9a8FtO71XLn/gkbQ0vqCzYQw0on/vE9/lLNo7S7rM=