Python之加密解密篇

程序员咋不秃头2024-05-03 13:08:22  123

「凯撒密码(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.md5md5_hash.update(b"Hello, World!")hashed = md5_hash.hexdigestprint("MD5 Hash:", hashed)

「SHA-256 哈希」:

SHA-256 生成一个固定长度的256位哈希值。

hashed = hashlib.sha256(b"Hello, World!").hexdigestprint("SHA-256 Hash:", hashed)

「AES 加密」:

AES(高级加密标准)是一种广泛使用的对称加密算法。

from Crypto.Cipher import AESfrom Crypto.Random import get_random_bytesfrom Crypto.Util.Padding import pad, unpadkey = get_random_bytes(16) # AES-128data = 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 RSAfrom Crypto.Cipher import PKCS1_OAEPkey = RSA.generate(2048)pub_key = key.publickeycipher_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 resultdef 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 Matrixdef 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)

转载此文是出于传递更多信息目的。若来源标注错误或侵犯了您的合法权益,请与本站联系,我们将及时更正、删除、谢谢。
https://www.414w.com/read/414881.html
0
最新回复(0)