最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
TextBlob如何实现文本加密与解密
时间:2026-05-31 10:00:01 编辑:袖梨 来源:一聚教程网
虽然TextBlob库专注于文本分析处理,但实现加密功能需借助专业加密工具。本文将详细介绍如何通过Python的Crypto库实现文本的AES加密解密操作。

from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
import base64
def encrypt_text(key, text):
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(text.encode())
return base64.b64encode(cipher.nonce + tag + ciphertext)
def decrypt_text(key, encrypted_text):
encrypted_text = base64.b64decode(encrypted_text)
nonce = encrypted_text[:AES.block_size]
tag = encrypted_text[AES.block_size:AES.block_size+16]
ciphertext = encrypted_text[AES.block_size+16:]
cipher = AES.new(key, AES.MODE_EAX, nonce)
decrypted_text = cipher.decrypt_and_verify(ciphertext, tag)
return decrypted_text.decode()
# Generate a random key
key = get_random_bytes(16)
# Encrypt text
text = "Hello, world!"
encrypted_text = encrypt_text(key, text)
print("Encrypted text:", encrypted_text)
# Decrypt text
decrypted_text = decrypt_text(key, encrypted_text)
print("Decrypted text:", decrypted_text)通过上述AES加密方案,我们可以有效保护文本数据安全,但切记密钥管理是保障加密效果的关键环节。
相关文章
- Tplink企业版路由器WiFi名称的默认设置介绍(Tplink企业版路由器WiFi名称的默认设置是什么) 09-06
- Tplink路由器灯常亮无法上网的原因分析(如何解决Tplink路由器灯常亮无法上网的问题) 09-06
- Tplink千兆企业级路由器自动重启的作用和优势介绍(如何设置Tplink千兆企业级路由器自动重启功能) 09-06
- 一根天线的tplink路由器有哪些(一根天线的Tplink路由器的特点和优势介绍) 09-06
- tplink路由器外网访问不了nas(Tplink路由器外网访问NAS的原因分析) 09-06
- Tplink无法搜到路由器的原因分析(如何解决Tplink无法搜到路由器的问题) 09-06