插件窝 干货文章 Python 实现文本加密和解密工具的方法大全

Python 实现文本加密和解密工具的方法大全

text 加密 Cipher 哈希 424    来源:    2025-03-14

在Python中,实现文本加密和解密的方法有很多种,常见的包括对称加密、非对称加密、哈希算法等。下面我将介绍几种常见的加密和解密方法,并提供相应的Python代码示例。

1. 对称加密(AES)

对称加密使用相同的密钥进行加密和解密。AES(Advanced Encryption Standard)是一种常见的对称加密算法。

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes

# 加密函数
def encrypt_aes(plain_text, key):
    cipher = AES.new(key, AES.MODE_CBC)
    ct_bytes = cipher.encrypt(pad(plain_text.encode(), AES.block_size))
    return cipher.iv + ct_bytes

# 解密函数
def decrypt_aes(cipher_text, key):
    iv = cipher_text[:AES.block_size]
    ct = cipher_text[AES.block_size:]
    cipher = AES.new(key, AES.MODE_CBC, iv)
    pt = unpad(cipher.decrypt(ct), AES.block_size)
    return pt.decode()

# 示例
key = get_random_bytes(16)  # 生成16字节的随机密钥
plain_text = "Hello, World!"
cipher_text = encrypt_aes(plain_text, key)
print("加密后的文本:", cipher_text)

decrypted_text = decrypt_aes(cipher_text, key)
print("解密后的文本:", decrypted_text)

2. 非对称加密(RSA)

非对称加密使用一对密钥:公钥和私钥。公钥用于加密,私钥用于解密。

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

# 生成RSA密钥对
key = RSA.generate(2048)
private_key = key.export_key()
public_key = key.publickey().export_key()

# 加密函数
def encrypt_rsa(plain_text, public_key):
    rsa_key = RSA.import_key(public_key)
    cipher = PKCS1_OAEP.new(rsa_key)
    cipher_text = cipher.encrypt(plain_text.encode())
    return cipher_text

# 解密函数
def decrypt_rsa(cipher_text, private_key):
    rsa_key = RSA.import_key(private_key)
    cipher = PKCS1_OAEP.new(rsa_key)
    plain_text = cipher.decrypt(cipher_text)
    return plain_text.decode()

# 示例
plain_text = "Hello, World!"
cipher_text = encrypt_rsa(plain_text, public_key)
print("加密后的文本:", cipher_text)

decrypted_text = decrypt_rsa(cipher_text, private_key)
print("解密后的文本:", decrypted_text)

3. 哈希算法(SHA-256)

哈希算法是一种单向加密方法,通常用于验证数据的完整性,而不是用于加密和解密。

import hashlib

# 哈希函数
def hash_sha256(text):
    return hashlib.sha256(text.encode()).hexdigest()

# 示例
text = "Hello, World!"
hashed_text = hash_sha256(text)
print("哈希后的文本:", hashed_text)

4. Base64 编码

Base64 是一种常见的编码方法,虽然不是加密算法,但可以用于简单的文本混淆。

import base64

# 编码函数
def encode_base64(text):
    return base64.b64encode(text.encode()).decode()

# 解码函数
def decode_base64(encoded_text):
    return base64.b64decode(encoded_text.encode()).decode()

# 示例
text = "Hello, World!"
encoded_text = encode_base64(text)
print("Base64 编码后的文本:", encoded_text)

decoded_text = decode_base64(encoded_text)
print("Base64 解码后的文本:", decoded_text)

5. 简单替换密码

这是一种非常简单的加密方法,通过替换字母表中的字符来实现加密。

# 简单替换密码
def simple_substitution_cipher(text, shift):
    result = ""
    for char in text:
        if char.isalpha():
            shift_amount = shift % 26
            if char.islower():
                result += chr((ord(char) - ord('a') + shift_amount) % 26 + ord('a'))
            else:
                result += chr((ord(char) - ord('A') + shift_amount) % 26 + ord('A'))
        else:
            result += char
    return result

# 示例
text = "Hello, World!"
shift = 3
encrypted_text = simple_substitution_cipher(text, shift)
print("加密后的文本:", encrypted_text)

decrypted_text = simple_substitution_cipher(encrypted_text, -shift)
print("解密后的文本:", decrypted_text)

总结

以上是几种常见的文本加密和解密方法。根据不同的需求,可以选择合适的加密方式。对称加密(如AES)适合需要高效加密的场景,非对称加密(如RSA)适合需要安全密钥交换的场景,而哈希算法(如SHA-256)则适合用于数据完整性验证。Base64 编码和简单替换密码则适合用于简单的文本混淆。

希望这些方法对你有所帮助!