Python之加密解密篇

  1. 「凯撒密码(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)
  1. 「Base64 编码」:
    Base64 是一种用64个字符表示任意二进制数据的方法。
import base64  # 编码 encoded = base64.b64encode(b"Hello, World!") print("Encoded:", encoded)  # 解码 decoded = base64.b64decode(encoded) print("Decoded:", decoded.decode('utf-8'))
  1. 「MD5 哈希」:
    MD5 是一种广泛使用的哈希函数,可以产生一个128位的哈希值。
import hashlib  # 哈希 md5_hash = hashlib.md5() md5_hash.update(b"Hello, World!") hashed = md5_hash.hexdigest() print("MD5 Hash:", hashed)
  1. 「SHA-256 哈希」:
    SHA-256 生成一个固定长度的256位哈希值。
hashed = hashlib.sha256(b"Hello, World!").hexdigest() print("SHA-256 Hash:", hashed)
  1. 「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'))
  1. 「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'))
  1. 「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)
  1. 「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)
  1. 「转置加密」:
    转置加密是一种简单的加密方法,通过改变字符在字符串中的位置来进行加密。
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)
  1. 「希尔密码」:
    希尔密码是一种使用线性代数的多表替换加密技术。
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

文章版权归作者所有,未经允许请勿转载。

免费节点实时更新

热门文章

最新文章

归档