目录
一、非对称加密和RSA
1.非对称秘钥
https://blog.csdn.net/gao131360144/article/details/79966094
HTTPS:SSH、数字指纹、数字签名SHA、RSA非对称加密、AES对称加密
2.RSA加密的核心
https://blog.csdn.net/qq_45616828/article/details/127702311
3.Python中实现RSA加密解密
import rsa
from binascii import b2a_hex, a2b_hex
# 第一步:生成RSA公钥和私钥
# pub, priv = rsa.newkeys(256)
pub, priv = rsa.newkeys(2048)
print(pub, priv)
# 第二步:公钥加密
encrypt = rsa.encrypt('Hello-蜗牛'.encode(), pub)
print(encrypt)
encstr = b2a_hex(encrypt).decode()
print(encstr)
# 第三步:私钥解密
# decrypt = rsa.decrypt(encrypt, priv)
decrypt = rsa.decrypt(a2b_hex(encstr), priv)
print(decrypt.decode())
二、HTTPS通信过程
三、对称加密和AES
1.AES简介
2.Python中实现AES加解密
加密过程
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
# 加密过程
source = 'Hello-蜗牛难道不是吗也许'
# 如果source分组后剩余长度不足16位的倍数就用空格补足为16位
if len(source.encode('utf-8')) % 16:
add = 16 - (len(source.encode('utf-8')) % 16)
else:
add = 0
source = source + ('\0' * add)
print(source)
# 定义密钥和偏移量,必须是16个字节、24字节或32字节
# key = 'todayiswonderful-1234567'.encode()
key = 'todayiswonderful-FEDCBA987654321'.encode()
mode = AES.MODE_CBC
iv = b'1234567890ABCDEF'
cryptos = AES.new(key, mode, iv)
cipher = cryptos.encrypt(source.encode())
print(cipher)
print(b2a_hex(cipher).decode())
解密过程
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
# 解密
source = 'f0c0ba97df33f91fdbfacd99c38838f347831e7e3a1f893f6752cdd080fc1c83cb300c0b7e117d587d7e5a69dbb70aee'
key = 'todayiswonderful-FEDCBA987654321'.encode()
mode = AES.MODE_CBC
iv = b'1234567890ABCDEF'
cryptos = AES.new(key, mode, iv)
dest = cryptos.decrypt(a2b_hex(source))
print(dest.decode().rstrip('\0'))