Python之加密解密篇
- 「凯撒密码(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
来源: FreeClashNode
文章版权归作者所有,未经允许请勿转载。
免费节点实时更新
热门文章
- 12月18日|18.1M/S,SSR/Clash/V2ray/Shadowrocket免费节点订阅链接每天更新
- 1月10日|22M/S,Shadowrocket/Clash/SSR/V2ray免费节点订阅链接每天更新
- 1月2日|22.9M/S,SSR/Shadowrocket/Clash/V2ray免费节点订阅链接每天更新
- 12月25日|18.4M/S,Shadowrocket/SSR/Clash/V2ray免费节点订阅链接每天更新
- 12月19日|22M/S,V2ray/Shadowrocket/SSR/Clash免费节点订阅链接每天更新
- 1月1日|19.9M/S,SSR/Shadowrocket/V2ray/Clash免费节点订阅链接每天更新
- 12月20日|19.6M/S,V2ray/Clash/Shadowrocket/SSR免费节点订阅链接每天更新
- 12月31日|18.9M/S,Shadowrocket/SSR/Clash/V2ray免费节点订阅链接每天更新
- 12月28日|18.5M/S,V2ray/SSR/Shadowrocket/Clash免费节点订阅链接每天更新
- 1月9日|21.6M/S,Clash/SSR/Shadowrocket/V2ray免费节点订阅链接每天更新
最新文章
- 1月16日|22.6M/S,SSR/V2ray/Clash(小猫咪)免费节点订阅链接每天更新
- 1月15日|22.7M/S,V2ray/Clash(小猫咪)/SSR免费节点订阅链接每天更新
- 1月14日|21.2M/S,SSR/Shadowrocket/V2ray/Clash免费节点订阅链接每天更新
- 1月13日|18M/S,Shadowrocket/SSR/V2ray/Clash免费节点订阅链接每天更新
- 1月12日|19.6M/S,SSR/Shadowrocket/Clash/V2ray免费节点订阅链接每天更新
- 1月11日|18.5M/S,SSR/V2ray/Clash/Shadowrocket免费节点订阅链接每天更新
- 1月10日|22M/S,Shadowrocket/Clash/SSR/V2ray免费节点订阅链接每天更新
- 1月9日|21.6M/S,Clash/SSR/Shadowrocket/V2ray免费节点订阅链接每天更新
- 1月8日|18.7M/S,V2ray/SSR/Shadowrocket/Clash免费节点订阅链接每天更新
- 1月7日|22.3M/S,Shadowrocket/Clash/V2ray/SSR免费节点订阅链接每天更新