本文目录导读:

我来介绍几种简单的Python字符串加密方法:
凯撒密码(Caesar Cipher) - 最简单的替换加密
def caesar_encrypt(text, shift=3):
"""凯撒加密:将每个字母向后移动shift位"""
result = ""
for char in text:
if char.isalpha():
# 判断大小写
ascii_offset = 65 if char.isupper() else 97
# 加密:移动位置并取模26
encrypted_char = chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset)
result += encrypted_char
else:
result += char
return result
def caesar_decrypt(text, shift=3):
"""凯撒解密:将每个字母向前移动shift位"""
return caesar_encrypt(text, -shift)
# 测试
original = "Hello World!"
encrypted = caesar_encrypt(original)
decrypted = caesar_decrypt(encrypted)
print(f"原文: {original}")
print(f"加密: {encrypted}")
print(f"解密: {decrypted}")
使用base64编码
import base64
def base64_encrypt(text):
"""将字符串编码为base64"""
# 将字符串转为字节,再进行base64编码
text_bytes = text.encode('utf-8')
encrypted = base64.b64encode(text_bytes)
return encrypted.decode('utf-8')
def base64_decrypt(encrypted_text):
"""解码base64字符串"""
encrypted_bytes = encrypted_text.encode('utf-8')
decrypted = base64.b64decode(encrypted_bytes)
return decrypted.decode('utf-8')
# 测试
original = "Hello World!"
encrypted = base64_encrypt(original)
decrypted = base64_decrypt(encrypted)
print(f"原文: {original}")
print(f"加密: {encrypted}")
print(f"解密: {decrypted}")
使用XOR加密
def xor_encrypt(text, key="my_secret_key"):
"""使用XOR加密,原理:a XOR b XOR b = a"""
encrypted = ""
key_length = len(key)
for i, char in enumerate(text):
# 将字符和密钥字符进行XOR运算
encrypted_char = chr(ord(char) ^ ord(key[i % key_length]))
encrypted += encrypted_char
return encrypted
def xor_decrypt(encrypted_text, key="my_secret_key"):
"""XOR解密(和加密是同一操作)"""
return xor_encrypt(encrypted_text, key)
# 测试
original = "Hello World!"
encrypted = xor_encrypt(original)
decrypted = xor_decrypt(encrypted)
print(f"原文: {original}")
print(f"加密: {encrypted}") # 可能显示乱码
print(f"解密: {decrypted}")
使用自制的替换密码
import random
import string
def create_cipher():
"""创建随机替换表"""
chars = string.printable[:-5] # 排除最后的空白字符
shuffled = list(chars)
random.shuffle(shuffled)
encryption_table = str.maketrans(chars, ''.join(shuffled))
decryption_table = str.maketrans(''.join(shuffled), chars)
return encryption_table, decryption_table
def custom_encrypt(text, encrypt_table):
"""使用替换表加密"""
return text.translate(encrypt_table)
def custom_decrypt(text, decrypt_table):
"""使用替换表解密"""
return text.translate(decrypt_table)
# 测试
encrypt_table, decrypt_table = create_cipher()
original = "Hello World!"
encrypted = custom_encrypt(original, encrypt_table)
decrypted = custom_decrypt(encrypted, decrypt_table)
print(f"原文: {original}")
print(f"加密: {encrypted}")
print(f"解密: {decrypted}")
使用建议
- 学习用途:凯撒密码和替换密码很适合理解加密原理
- 简单场景:base64适合需要简单编码/解码的场景
- 中小项目:XOR加密可以配合自定义密钥使用
- 安全要求:以上方法不安全,生产环境应使用
hashlib、cryptography等标准库
提示:这些方法仅供学习和简单使用,真正的数据加密建议使用专业的加密库如cryptography或hashlib。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播