TextBlob库本身并不提供对文本进行加密和解密的功能,它主要用于自然语言处理和文本分析。要对文本进行加密和解密,你可以使用其他加密算法和库,比如使用Python的Crypto库或者使用第三方加密算法。以下是一个使用Crypto库对文本进行加密和解密的示例:
from Crypto.Cipher import AESfrom Crypto.Random import get_random_bytesimport base64def 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 keykey = get_random_bytes(16)# Encrypt texttext = "Hello, world!"encrypted_text = encrypt_text(key, text)print("Encrypted text:", encrypted_text)# Decrypt textdecrypted_text = decrypt_text(key, encrypted_text)print("Decrypted text:", decrypted_text)请注意,以上示例中使用了AES对称加密算法对文本进行加密和解密。在实际应用中,你需要妥善保存密钥以确保安全性。


