使用Python内置函数加密和解密文本
Python是一种高级编程语言,具有广泛的应用领域,包括数据分析、计算机视觉、自然语言处理等等。在这些领域中,加密和解密数据是非常常见的需求。Python内置了一些加密和解密函数,可以方便地实现这些需求。
在Python中,最常用的加密算法是AES对称加密算法和RSA非对称加密算法。对称加密算法使用相同的密钥进行加密和解密,而非对称加密算法使用公钥和私钥对数据进行加密和解密。
下面将分别介绍使用Python内置函数进行AES对称加密和解密以及RSA非对称加密和解密的方法。
1. 使用Python内置函数进行AES对称加密和解密
AES是一种高级加密标准,目前被广泛应用于数据加密和解密。在Python中,使用cryptodome库可以方便地实现AES加密和解密。
示例代码:
from Crypto.Cipher import AES
import base64
# 加密函数
def aes_encrypt(text, key):
# 将密钥和明文转化为bytes类型
key = bytes(key, 'utf-8')
text = bytes(text, 'utf-8')
# 使用PKCS7Padding方式进行补位,使得加密数据满足AES算法长度要求
cipher = AES.new(key, AES.MODE_ECB)
length = 16
count = len(text)
add = length - (count % length)
text = text + (b'\0' * add)
# 加密
ciphertext = cipher.encrypt(text)
# 使用Base64编码输出加密结果
return base64.b64encode(ciphertext).decode()
# 解密函数
def aes_decrypt(text, key):
# 将密钥和密文转化为bytes类型
key = bytes(key, 'utf-8')
text = base64.b64decode(text)
# 解密
cipher = AES.new(key, AES.MODE_ECB)
ciphertext = cipher.decrypt(text)
# 去除Padding
return ciphertext.rstrip(b'\0').decode()
# 测试
text = 'Hello, world'
key = '1234567890123456'
ciphertext = aes_encrypt(text, key)
print('加密结果:', ciphertext)
plaintext = aes_decrypt(ciphertext, key)
print('解密结果:', plaintext)
在上述代码中,aes_encrypt函数用于加密明文,而aes_decrypt函数用于解密密文。其中,使用PKCS7Padding方式进行补位以满足AES算法长度要求,并使用Base64编码输出加密结果。在测试中,我们将“Hello, world”进行加密和解密。
2. 使用Python内置函数进行RSA非对称加密和解密
RSA是一种非对称加密算法,主要用于数据加密和数字签名。在Python中,使用cryptodome库可以方便地实现RSA加密和解密。
示例代码:
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
import base64
# 生成密钥对,公钥和私钥分别保存在public.pem和private.pem文件中
def generate_key_pair():
key = RSA.generate(2048)
with open('private.pem', 'wb') as f:
f.write(key.export_key())
with open('public.pem', 'wb') as f:
f.write(key.publickey().export_key())
# 加密函数
def rsa_encrypt(text, public_key_path):
with open(public_key_path, 'rb') as f:
key = f.read()
rsakey = RSA.import_key(key)
cipher = PKCS1_v1_5.new(rsakey)
# 分块加密
length = 245
count = len(text)
pos = 0
ciphertext = []
while pos < count:
if pos + length < count:
block = text[pos : pos+length]
else:
block = text[pos : count]
ciphertext.append(cipher.encrypt(block.encode()))
pos += length
# 输出加密结果
return base64.b64encode(b''.join(ciphertext)).decode()
# 解密函数
def rsa_decrypt(text, private_key_path):
with open(private_key_path, 'rb') as f:
key = f.read()
rsakey = RSA.import_key(key)
cipher = PKCS1_v1_5.new(rsakey)
# 分块解密
length = 256
ciphertext = base64.b64decode(text)
count = len(ciphertext)
pos = 0
plaintext = []
while pos < count:
if pos + length < count:
block = ciphertext[pos : pos+length]
else:
block = ciphertext[pos : count]
plaintext.append(cipher.decrypt(block, None).decode())
pos += length
# 输出解密结果
return ''.join(plaintext)
# 测试
text = 'Hello, world'
generate_key_pair()
public_key_path = 'public.pem'
private_key_path = 'private.pem'
ciphertext = rsa_encrypt(text, public_key_path)
print('加密结果:', ciphertext)
plaintext = rsa_decrypt(ciphertext, private_key_path)
print('解密结果:', plaintext)
在上述代码中,generate_key_pair函数用于生成公钥和私钥,而rsa_encrypt函数和rsa_decrypt函数分别用于加密和解密明文。在加密和解密过程中,使用PKCS1_v1_5填充方式进行加密,并分块加密和分块解密以满足RSA算法长度的要求。
总结:
本文介绍了使用Python内置函数进行AES加密和解密以及RSA加密和解密的方法。这些函数不仅方便易用,而且功能强大,可以满足大部分加密和解密需求。对于更高级的加密和解密算法,可以使用其他Python库,比如pycryptodome、cryptography等。
