Python RSA加解密

Python RSA加解密

RSA是一种非对称加密技术,在软件、网页中已得到广泛应用,特别对于爬虫来说,会经常遇到使用RSA加密的网站,掌握RSA加密和解密的流程,在遇到相关网站时,可以第一时间辨别出网站的加密逻辑,从而快速破解。

利用python实现RSA加密,需要安装模块
pip install pycryptodome

生成密钥对

一般在业务对接或爬取网站数据时,公钥和私钥肯定是已经定好的,但是这里我们为了后续可以进行加解密的模拟操作,先了解下如何自己创建秘钥对。

1
2
3
4
5
6
7
from Crypto.PublicKey import RSA

f = RSA.generate(2048)
private_key = f.exportKey("PEM") # 生成私钥
public_key = f.publickey().exportKey() # 生成公钥
print(private_key)
print(public_key)
1
2
b'-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA1SCt+JO0PA8aFGUx2yxlg/V5gy4Q2oWNszP5T/JxEONv5xgv\nUT6rV8ncGJRzHllnI5wJw1UR62uVQhoWi8HzYHVxCUKIH6jlnbY6ocRu89PDHUXU\nMseLwiatqj2TEhOEXeDhvcqcK4uVDftMupyCPDZtdHvAzTuicCKT8CBOMFF0VsME\nr9SwQYxVwEUIskis9x2Z4cT3TCox50zifx9pPmxneQ7OsKTHhjwqUF/3V12IB9hA\ndijzQjwe+Q0x24MTP5VBhuZD9j21KNjMPkdfcn+Ad0OYklMGh4urI3O7/vUh6/Tv\nzAN7N9mKfjP+BIwhtTdQZWw9gJG+bz24LEHNgQIDAQABAoIBABIyWZg1KDjOg0gF\ngdW4Oyyim07XcontyDTqNZmdVgj+mjHNb1DM/H0g14Fjv1ZyhKtWKcE7NK34gARm\nTNF+7EQqks2bGBs35O7rvaLNLjn0NR79NMpnHigj/ETiP/nVn8ISLsPxFhdjsgTB\nFYKDVV1Ful+tJz9+nXZchWwFnhdi3Q/cTCkv3r88La5rpfiFE+Ozf/GBthUuNTPq\ns9Xn5gvQXqyLWz32yUi2us8a6F/GS68jTcNciQ/33cjJG6ufUX+neW/h1DNXh1ET\nQIzSWGV0vk98FmKAcUDq8/SNVqt8C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oWNszP5T/JxEONv5xgvUT6rV8ncGJRzHllnI5wJw1UR62uVQhoWi8Hz\nYHVxCUKIH6jlnbY6ocRu89PDHUXUMseLwiatqj2TEhOEXeDhvcqcK4uVDftMupyC\nPDZtdHvAzTuicCKT8CBOMFF0VsMEr9SwQYxVwEUIskis9x2Z4cT3TCox50zifx9p\nPmxneQ7OsKTHhjwqUF/3V12IB9hAdijzQjwe+Q0x24MTP5VBhuZD9j21KNjMPkdf\ncn+Ad0OYklMGh4urI3O7/vUh6/TvzAN7N9mKfjP+BIwhtTdQZWw9gJG+bz24LEHN\ngQIDAQAB\n-----END PUBLIC KEY-----'

可以看出来RSA的公钥和私钥的特征比较明显,通过这个特征有时也可以帮我们快速判断加密方法。

加密

加密流程:

  1. 输入文本(str);
  2. 字符串编码(默认utf-8)(bytes);
  3. rsa加密(bytes);
  4. base64编码(bytes);
  5. 解码为字符串(str)。

下面的代码承接上面生成的秘钥和公钥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import base64
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA

def encryption(text: str, public_key: bytes):
# 字符串指定编码(转为bytes)
text = text.encode('utf-8')
# 构建公钥对象
cipher_public = PKCS1_v1_5.new(RSA.importKey(public_key))
# 加密(bytes)
text_encrypted = cipher_public.encrypt(text)
# base64编码,并转为字符串
text_encrypted_base64 = base64.b64encode(text_encrypted ).decode()
return text_encrypted_base64

text = '123456'
text_encrypted_base64 = encryption(text, public_key)
print('密文:', text_encrypted_base64)

1
密文: Ul6HCvcbWg2JBjjKlpr5LmGGjJnEZUv8dMpODF132VWeMcHJis1OqsW1rycQm18mTyA1AydRDp1vc2T8sg7qTZFPS1W2YtLmalpwnVKdcC7arGNURAzw/3NGLbRpMAsvqiMusI8nX9EaA6iZE4TLV61DHgUdQLoiosKiKlnx5l+POO2C9X47J+U1+PG/977a8zUAlmsZv85sQ1TtKXgqAGhpGkMnNYzS6YHOBCfJiXVYgOWO3BTYh35k8AHaL6FOxHrJePzmHiLl4vB9qhhMvT3xm3t32LGMUOI4vEWrB/jnF3dJgzfomYpTEbRWCnhJecjzVd1oZm0gV+bmLKrcyQ==

解密

解密流程:

  1. 输入文本(str);
  2. 字符串编码(默认utf-8)(bytes);
  3. base64解码(bytes);
  4. rsa解密(bytes);
  5. 解码为字符串(str)。

下面的代码承接上面加密部分的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from Crypto import Random

def decryption(text_encrypted_base64: str, private_key: bytes):
# 字符串指定编码(转为bytes)
text_encrypted_base64 = text_encrypted_base64.encode('utf-8')
# base64解码
text_encrypted = base64.b64decode(text_encrypted_base64)
# 构建私钥对象
cipher_private = PKCS1_v1_5.new(RSA.importKey(private_key))
# 解密(bytes)
text_decrypted = cipher_private.decrypt(text_encrypted, Random.new().read)
# 解码为字符串
text_decrypted = text_decrypted.decode()
return text_decrypted

text_decrypted = decryption(text_encrypted_base64, private_key)
print('明文:', text_decrypted)

明文: 123456