MD5加密
import hashlib
def md5_encrypt(text):
md5 = hashlib.md5()
md5.update(text.encode('utf-8'))
encrypted_text = md5.hexdigest()
return encrypted_text
plaintext = input("请输入要加密的文本: ")
encrypted_text = md5_encrypt(plaintext)
print("加密后的结果为:", encrypted_text)
DES加密与解密
import base64
from Cryptodome.Cipher import DES
from Cryptodome.Util.Padding import pad, unpad
def des_encrypt(text, key):
cipher = DES.new(key.encode(), DES.MODE_ECB)
encrypt_text = cipher.encrypt(pad(text.encode(), DES.block_size, style='pkcs7'))
return base64.b64encode(encrypt_text).decode()
def des_decrypt(encrypt_text, key):
encrypt_text = base64.b64decode(encrypt_text)
cipher = DES.new(key.encode(), DES.MODE_ECB)
decrypt_text = cipher.decrypt(encrypt_text)
return unpad(decrypt_text, DES.block_size, style='pkcs7').decode()
AES加密与解密
from Cryptodome.Cipher import AES
from Cryptodome.Util.Padding import pad, unpad
import base64
def aes_encrypt(text, key, iv):
cipher = AES.new(key.encode(), AES.MODE_CBC, iv.encode())
encrypt_text = cipher.encrypt(pad(text.encode(), AES.block_size))
return base64.b64encode(encrypt_text).decode()
def aes_decrypt(encrypt_text, key, iv):
encrypt_text = base64.b64decode(encrypt_text)
cipher = AES.new(key.encode(), AES.MODE_CBC, iv.encode())
decrypt_text = cipher.decrypt(encrypt_text)
return unpad(decrypt_text, AES.block_size).decode()
RSA加密与解密(建议使用方法三)
公钥:
# public_key.pem
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdkCwaA32WNVTfePalphw0kAVg
QtQXv/SNhOMwKLgYdJuYzqPYO6SM6D30CmunEqmUGVEivaI352R9GJU0bvSEptkO
7uBUxqz9INy8gnlb7TILj6vPCG/cTekFRrFKLYapd8fhRMHIKQEGSjtYMetN0Tnl
EQg/VkxFAHj0dyR2cwIDAQAB
-----END PUBLIC KEY-----
私钥:
# private_key.pem
-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAN2QLBoDfZY1VN94
9qWmHDSQBWBC1Be/9I2E4zAouBh0m5jOo9g7pIzoPfQKa6cSqZQZUSK9ojfnZH0Y
lTRu9ISm2Q7u4FTGrP0g3LyCeVvtMguPq88Ib9xN6QVGsUothql3x+FEwcgpAQZK
O1gx603ROeURCD9WTEUAePR3JHZzAgMBAAECgYEA1CGESAfNhzJap3euVt0apiy6
KPbzECYmJBT2XBawZCHJE93Mu2wnWQ3Gu0/60ESq/fPbXfLITNQLv0X5HPGaw1qe
0k/Q6vZg1BUGt/gwR/AhYpPpzI23jOz0Sttnv5YFkEwj/wx53tgQXo+RaCs1WECN
m+Fk2nzvIsnzHR2mKykCQQD19BrKHt0eK2qpP5epQktttS8kc9FTSrxlV9sK/hpg
sN16JMNdtXiVbqba11xeePtES3T9rw1K2mtoP0W/qaYVAkEA5p0Fi7TWRwr21CZ8
Hg/eWb5U/HHH1Kpsf3fysfDBlEJGfo1J5/l8KFPH5o5DuBKVC3UDy6+9AZO45OHs
kIUUZwJBAMNUqjH7cQCU2pCvwHLcA6uNSXNZx9SudhDGRV/ljm1cCYkHyWCSzsFe
r4T9pI4i5urGQpN2gsf/z21rUql0wkECQGHgTOVBiBw43A+okc8UfxNpw5otFFn/
RvNGzluVSiOJiW/WuYJaEcbv4AzLXpR2Vom5TTYazUYpEzUZahTgVosCQHJ97VC3
tq3wUq3Q0Xqvk/4IvG2tPtP9PjQn9RMARLBulhGk4hmATWFE6uSbRf6UOi//EoP/
tTwxsb50TWlMiPY=
-----END PRIVATE KEY-----
方法一:
# 导入库可以输入Crypto或Cryptodome,哪个没有报错就用哪个,效果都一样
from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import PKCS1_OAEP
import base64
# 加密
def encrypt_data(msg, public_key_path):
# 读取公钥文件
with open(public_key_path, 'r') as public_key_file:
public_key = RSA.import_key(public_key_file.read())
cipher = PKCS1_OAEP.new(public_key)
encrypted_text = cipher.encrypt(msg.encode('utf-8'))
return base64.b64encode(encrypted_text).decode('utf-8')
# 解密
def decrypt_data(encrypted_text, private_key_path):
# 读取私钥文件
with open(private_key_path, 'r') as private_key_file:
private_key = RSA.import_key(private_key_file.read())
cipher = PKCS1_OAEP.new(private_key)
decoded_encrypted_text = base64.b64decode(encrypted_text.encode('utf-8'))
decrypted_message = cipher.decrypt(decoded_encrypted_text)
return decrypted_message.decode('utf-8')
# 加密后文本
print(encrypt_data("Hello, this is a secret message!", "./public_key.pem"))
# 解密后文本
print(decrypt_data(encrypt_data("Hello, this is a secret message!", "./public_key.pem"), "./private_key.pem"))
方法二:
import base64
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.asymmetric import padding
# 读取公钥文件
with open("public_key.pem", "rb") as public_key_file:
public_key = serialization.load_pem_public_key(
public_key_file.read()
)
# 读取私钥文件
with open("private_key.pem", "rb") as private_key_file:
private_key = serialization.load_pem_private_key(
private_key_file.read(),
password=None # 如果私钥有密码保护,需要提供密码
)
# 加密消息
message_text = "Hello, this is a secret message!"
message = message_text.encode('utf-8')
ciphertext = public_key.encrypt(
message,
padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(),
label=None
)
)
# 将加密后的字节串转换为 Base64 字符串
encrypted_str = base64.b64encode(ciphertext).decode('utf-8')
# 解密消息
decoded_ciphertext = base64.b64decode(encrypted_str.encode('utf-8'))
decrypted_message = private_key.decrypt(
decoded_ciphertext,
padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(), label=None
)
)
# 将解密后的字节串转换为文本
decrypted_text = decrypted_message.decode('utf-8')
# 打印加密前的文本、加密后的字符串和解密后的文本
print("Original Message:", message_text)
print("Encrypted Message String:", encrypted_str)
print("Decrypted Message Text:", decrypted_text)
方法三:
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
# 加密
def encrypt_data(msg, key_file):
public_key = get_key(key_file) # 读取公钥信息
cipher = PKCS1_v1_5.new(public_key) # 生成一个加密的类
encrypt_text = base64.b64encode(cipher.encrypt(msg.encode())) # 对数据进行加密
return encrypt_text.decode() # 对文本进行解码
# 解密
def decrypt_data(encrypted_text, key_file):
private_key = RSA.import_key(open(key_file).read()) # 读取私钥信息
encrypted_bytes = base64.b64decode(encrypted_text.encode()) # 将加密的文本从Base64格式解码为字节串
cipher = PKCS1_v1_5.new(private_key) # 创建一个PKCS1_v1_5解密器对象
decrypted_bytes = cipher.decrypt(encrypted_bytes, None) # 使用解密器对象解密消息
return decrypted_bytes.decode() # 返回解密后的消息,将其从字节串解码为文本
public_key = r"public_key.pem"
encrypt_text = encrypt_data("1234", public_key)
print("加密后文本:", encrypt_text)
private_key = r'private_key.pem'
decrypt_text = decrypt_data(encrypt_text, private_key)
print("解密后文本:", decrypt_text)