Python之加密解密篇
看看资讯 / 100+人浏览
注意:免费节点订阅链接已更新至 2026-11-9,点击查看详情
- 「凯撒密码(Caesar Cipher)」:
凯撒密码是一种替换加密的技术,通过将字母表中的每个字母移动固定数目来进行加密。
def caesar_cipher(text, shift): result = "" for char in text: if char.isalpha(): shift = shift % 26 if char.islower(): result += chr((ord(char) - 97 + shift) % 26 + 97) else: result += chr((ord(char) - 65 + shift) % 26 + 65) else: result += char return result # 加密 encrypted = caesar_cipher("Hello, World!", 3) print("Encrypted:", encrypted) # 解密 decrypted = caesar_cipher(encrypted, -3) print("Decrypted:", decrypted)- 「Base64 编码」:
Base64 是一种用64个字符表示任意二进制数据的方法。
import base64 # 编码 encoded = base64.b64encode(b"Hello, World!") print("Encoded:", encoded) # 解码 decoded = base64.b64decode(encoded) print("Decoded:", decoded.decode('utf-8'))- 「MD5 哈希」:
MD5 是一种广泛使用的哈希函数,可以产生一个128位的哈希值。
import hashlib # 哈希 md5_hash = hashlib.md5() md5_hash.update(b"Hello, World!") hashed = md5_hash.hexdigest() print("MD5 Hash:", hashed)- 「SHA-256 哈希」:
SHA-256 生成一个固定长度的256位哈希值。
hashed = hashlib.sha256(b"Hello, World!").hexdigest() print("SHA-256 Hash:", hashed)- 「AES 加密」:
AES(高级加密标准)是一种广泛使用的对称加密算法。
from Crypto.Cipher import AES from Crypto.Random import get_random_bytes from Crypto.Util.Padding import pad, unpad key = get_random_bytes(16) # AES-128 data = pad(b"Hello, World!", AES.block_size) cipher = AES.new(key, AES.MODE_CBC) encrypted = cipher.encrypt(data) print("Encrypted:", encrypted) decrypted = cipher.decrypt(encrypted) print("Decrypted:", unpad(decrypted, AES.block_size).decode('utf-8'))- 「RSA 加密」:
RSA 是一种非对称加密算法,广泛用于数据加密和数字签名。
from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP key = RSA.generate(2048) pub_key = key.publickey() cipher_rsa = PKCS1_OAEP.new(pub_key) encrypted_rsa = cipher_rsa.encrypt(b"Hello, World!") print("Encrypted with RSA:", encrypted_rsa) decrypted_rsa = cipher_rsa.decrypt(encrypted_rsa) print("Decrypted with RSA:", decrypted_rsa.decode('utf-8'))- 「XOR 加密」:
XOR 操作是一种简单的加密技术,通过将每个字符与一个密钥进行异或操作来加密。
def xor_encrypt(text, key): return ''.join(chr(ord(c) ^ ord(k)) for c, k in zip(text, key * len(text))) def xor_decrypt(encrypted, key): return xor_encrypt(encrypted, key) key = "secret" encrypted = xor_encrypt("Hello, World!", key) print("Encrypted with XOR:", encrypted) decrypted = xor_decrypt(encrypted, key) print("Decrypted with XOR:", decrypted)- 「Vigenère 方阵」:
Vigenère 方阵是一种多表替换加密的形式,它使用一系列的凯撒密码。
def vigenere_encrypt(plaintext, key): result = "" for i, char in enumerate(plaintext): if char.isalpha(): key_index = i % len(key) key_char = key[key_index].upper() result += chr((ord(char.upper()) + ord(key_char) - 65) % 26 + 65) else: result += char return result def vigenere_decrypt(ciphertext, key): return vigenere_encrypt(ciphertext, key) encrypted = vigenere_encrypt("Hello, World!", "KEY") print("Encrypted with Vigenère:", encrypted) decrypted = vigenere_decrypt(encrypted, "KEY") print("Decrypted with Vigenère:", decrypted)- 「转置加密」:
转置加密是一种简单的加密方法,通过改变字符在字符串中的位置来进行加密。
def transpose_encrypt(plaintext, n): return ''.join(''.join(plaintext[i::n]) for i in range(n)) def transpose_decrypt(ciphertext, n): return transpose_encrypt(ciphertext, n) encrypted = transpose_encrypt("Hello, World!", 3) print("Encrypted with Transposition:", encrypted) decrypted = transpose_decrypt(encrypted, 3) print("Decrypted with Transposition:", decrypted)- 「希尔密码」:
希尔密码是一种使用线性代数的多表替换加密技术。
from sympy import Matrix def hill_encrypt(plaintext, key): key_matrix = Matrix(key) plaintext_matrix = Matrix(plaintext) encrypted_matrix = plaintext_matrix * key_matrix return [int(x) for x in encrypted_matrix.tolist()] def hill_decrypt(ciphertext, key): key_matrix = Matrix(key) ciphertext_matrix = Matrix(ciphertext) inverse_key_matrix = key_matrix.inv_mod(26) decrypted_matrix = ciphertext_matrix * inverse_key_matrix return ''.join(chr(int(x) + 65) for x in decrypted_matrix.tolist()) plaintext = "HELLO" key = [3, 3, 3] encrypted = hill_encrypt(plaintext, key) print("Encrypted with Hill Cipher:", ''.join(map(str, encrypted))) decrypted = hill_decrypt(encrypted, key) print("Decrypted with Hill Cipher:", decrypted)
版权声明:
作者: FreeClashNode
链接: https://www.freeclashnode.com/news/article-2983.htm
来源: www.freeclashnode.com
文章版权归作者所有,未经允许请勿转载。
免费节点实时更新
热门文章
- 11月2日免费节点分享|22.5M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 10月14日免费节点分享|22.5M/S,Clash节点/V2ray节点/Singbox节点/SSR节点|免费上网梯子每天更新
- 10月11日免费节点分享|18.6M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 10月25日免费节点分享|18.1M/S,SSR节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月13日免费节点分享|22.1M/S,V2ray节点/Clash节点/Singbox节点/Shadowrocket节点|免费上网梯子每天更新
- 10月12日免费节点分享|22.7M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月27日免费节点分享|20M/S,Shadowrocket节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 10月28日免费节点分享|22.8M/S,Singbox节点/SSR节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 10月21日免费节点分享|22M/S,Clash节点/Singbox节点/V2ray节点/SSR节点|免费上网梯子每天更新
- 10月17日免费节点分享|18M/S,Clash节点/SSR节点/V2ray节点/Singbox节点|免费上网梯子每天更新
最新文章
- 解锁全球视野:星愿浏览器科学上网终极指南
- 11月9日免费节点分享|22.3M/S,Shadowrocket节点/Clash节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 轻松翻越网络高墙:Shadowrocket一键导入配置全攻略
- 11月8日免费节点分享|21.3M/S,Clash节点/V2ray节点/Singbox节点/SSR节点|免费上网梯子每天更新
- 当Vmess突然失效:一场技术与封锁的无声博弈
- 11月7日免费节点分享|22.9M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 梅林固件科学上网完全指南:从入门到精通,零成本解锁网络自由
- 11月6日免费节点分享|20.9M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 幸运破解器科学上网全解析:解锁数字世界的终极指南
- 11月5日免费节点分享|21.9M/S,Shadowrocket节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 11月4日免费节点分享|18M/S,Clash节点/Singbox节点/SSR节点/V2ray节点|免费上网梯子每天更新
- 穿越数字迷雾:深度解析V2Ray中转迟滞之谜与极速优化指南
- 跨越数字边界:科学上网的终极指南与深度思考
- 从连接到掌控:深入理解 Quantumult 的工作流程与网络管理哲学
- 11月2日免费节点分享|22.5M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 深度解析《荒野冲突》:征服与策略的终极对决
- 11月1日免费节点分享|20.3M/S,V2ray节点/Shadowrocket节点/Clash节点/Singbox节点|免费上网梯子每天更新
- 斐讯路由器刷机配置v2ray全攻略:打造安全高效的翻墙网络环境
- 10月31日免费节点分享|18.7M/S,Shadowrocket节点/Clash节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 谷歌浏览器科学上网扩展全攻略:从入门到精通
- 10月30日免费节点分享|20.4M/S,V2ray节点/SSR节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 苹果手机V2Ray完全指南:从零开始打造安全上网环境
- 10月29日免费节点分享|21.6M/S,SSR节点/Clash节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 探索数字世界的门户:电脑科学上网贴吧完全指南
- 10月28日免费节点分享|22.8M/S,Singbox节点/SSR节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 穿越数字边界:科学上网的全面指南与深度思考
- 10月27日免费节点分享|20M/S,Shadowrocket节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 数字世界的破壁之旅:科学上网全指南与实用工具深度解析
- 10月26日免费节点分享|21.5M/S,V2ray节点/SSR节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 跨越知识边界:浙大学子的全球学术网络探索指南
- 10月25日免费节点分享|18.1M/S,SSR节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月24日免费节点分享|20.5M/S,V2ray节点/Clash节点/Singbox节点/Shadowrocket节点|免费上网梯子每天更新
- 刘大猫科学上网全攻略:解锁全球网络的终极指南
- 10月23日免费节点分享|22.2M/S,Shadowrocket节点/Clash节点/Singbox节点/V2ray节点|免费上网梯子每天更新
- 全面解析Quantumult安卓版:功能、安装与配置教程
- 双重防护,畅享无界:AdGuard联手V2RayNG,打造极致安全的上网体验
- 10月22日免费节点分享|18.8M/S,V2ray节点/SSR节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 掌握Clash白名单设置:精准控制代理流量的艺术
- 10月21日免费节点分享|22M/S,Clash节点/Singbox节点/V2ray节点/SSR节点|免费上网梯子每天更新
- 突破网络边界:技术科学上网的终极安全指南
- 10月20日免费节点分享|22.9M/S,SSR节点/V2ray节点/Clash节点/Singbox节点|免费上网梯子每天更新
- Shadowrocket 选购指南:从入门到精通,找到你的专属代理神器
- 10月19日免费节点分享|19.3M/S,Singbox节点/V2ray节点/Clash节点/Shadowrocket节点|免费上网梯子每天更新
- 深入解读:V2Ray为何比SSR更安全?一场网络隐私的全方位较量
- 10月18日免费节点分享|20.6M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 深度指南:Shadowrocket从入门到精通——iOS用户的网络自由密钥
- 10月17日免费节点分享|18M/S,Clash节点/SSR节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 深入解析科学上网套:全方位使用指南与常见问题解答
- 10月16日免费节点分享|22M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月15日免费节点分享|21.2M/S,Clash节点/SSR节点/V2ray节点/Singbox节点|免费上网梯子每天更新
归档
- 2026-11 17
- 2026-10 61
- 2026-09 58
- 2026-08 63
- 2026-07 61
- 2026-06 55
- 2026-05 56
- 2026-04 51
- 2026-03 60
- 2026-02 52
- 2026-01 56
- 2025-12 59
- 2025-11 55
- 2025-10 56
- 2025-09 55
- 2025-08 49
- 2025-07 31
- 2025-06 30
- 2025-05 31
- 2025-04 30
- 2025-03 388
- 2025-02 360
- 2025-01 403
- 2024-12 403
- 2024-11 390
- 2024-10 403
- 2024-09 388
- 2024-08 402
- 2024-07 427
- 2024-06 442
- 2024-05 181
- 2024-04 33
- 2024-03 31
- 2024-02 29
- 2024-01 51
- 2023-12 52
- 2023-11 32
- 2023-10 32
- 2023-09 3