以下是使用 Python 实现非对称加密的 A 端和 B 端软件的详细步骤及代码示例,并且会说明如何将其打包为可执行的 .exe
文件。这里我们使用 cryptography
库来实现 RSA 非对称加密算法。
1. 安装依赖库
首先,确保你已经安装了 cryptography
库。如果没有安装,可以使用以下命令进行安装:
pip install cryptography
2. 编写 A 端软件代码(a_end.py
)
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
import base64def encrypt_message(public_key_pem, plaintext):# 加载公钥public_key = serialization.load_pem_public_key(public_key_pem.encode(),backend=default_backend())# 加密明文ciphertext = public_key.encrypt(plaintext.encode(),padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),algorithm=hashes.SHA256(),label=None))# 将加密后的字节数据转换为 Base64 编码的字符串,方便复制encrypted_base64 = base64.b64encode(ciphertext).decode()return encrypted_base64if __name__ == "__main__":# 读取公钥文件try:with open('public_key.pem', 'r') as f:public_key_pem = f.read()except FileNotFoundError:print("未找到公钥文件 'public_key.pem',请确保文件存在。")exit(1)# 输入明文plaintext = input("请输入要加密的明文: ")# 加密明文encrypted_text = encrypt_message(public_key_pem, plaintext)# 输出可复制的密文print("加密后的密文(可复制):")print(encrypted_text)
3. 编写 B 端软件代码(b_end.py
)
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
import base64def generate_key_pair():# 生成私钥private_key = rsa.generate_private_key(public_exponent=65537,key_size=2048,backend=default_backend())# 生成公钥public_key = private_key.public_key()# 保存私钥到文件private_pem = private_key.private_bytes(encoding=serialization.Encoding.PEM,format=serialization.PrivateFormat.PKCS8,encryption_algorithm=serialization.NoEncryption())with open('private_key.pem', 'wb') as f:f.write(private_pem)# 保存公钥到文件public_pem = public_key.public_bytes(encoding=serialization.Encoding.PEM,format=serialization.PublicFormat.SubjectPublicKeyInfo)with open('public_key.pem', 'wb') as f:f.write(public_pem)print("密钥对生成成功,私钥保存为 'private_key.pem',公钥保存为 'public_key.pem'。")def decrypt_message(private_key_pem, encrypted_base64):# 加载私钥private_key = serialization.load_pem_private_key(private_key_pem.encode(),password=None,backend=default_backend())# 将 Base64 编码的密文转换为字节数据ciphertext = base64.b64decode(encrypted_base64)# 解密密文plaintext = private_key.decrypt(ciphertext,padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),algorithm=hashes.SHA256(),label=None))return plaintext.decode()if __name__ == "__main__":# 生成密钥对generate_key_pair()# 读取私钥文件try:with open('private_key.pem', 'r') as f:private_key_pem = f.read()except FileNotFoundError:print("未找到私钥文件 'private_key.pem',请确保文件存在。")exit(1)# 输入密文encrypted_text = input("请输入要解密的密文: ")# 解密密文decrypted_text = decrypt_message(private_key_pem, encrypted_text)# 输出解密后的明文print("解密后的明文:")print(decrypted_text)
4. 打包为 .exe
文件
我们可以使用 PyInstaller
库将 Python 脚本打包为可执行的 .exe
文件。首先,确保你已经安装了 PyInstaller
:
pip install pyinstaller
打包 A 端软件
在命令行中,进入包含 a_end.py
文件的目录,然后运行以下命令:
pyinstaller --onefile a_end.py
打包完成后,在 dist
目录下会生成 a_end.exe
文件。
打包 B 端软件
同样,在命令行中进入包含 b_end.py
文件的目录,运行以下命令:
pyinstaller --onefile b_end.py
打包完成后,在 dist
目录下会生成 b_end.exe
文件。
5. 使用说明
- B 端软件(
b_end.exe
):运行该软件会生成密钥对,将私钥保存为private_key.pem
,公钥保存为public_key.pem
。然后提示你输入要解密的密文,输入后会输出解密后的明文。 - A 端软件(
a_end.exe
):运行该软件前,需要确保public_key.pem
文件存在。运行后,输入要加密的明文,软件会输出加密后的密文,该密文可以复制并提供给 B 端进行解密。
通过以上步骤,你就可以实现一个简单的非对称加密的 A 端和 B 端软件,并将其打包为可执行的 .exe
文件。