Python 常用加密算法实现 - AES、RSA、ECC、DES、3DES、Rabin、ElGamal、Diffie-Hellman
以下是使用 Python 实现常见加密算法的代码示例:
- AES 算法加密实现:
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
def encrypt_aes(key, plaintext):
cipher = AES.new(key, AES.MODE_EAX)
nonce = cipher.nonce
ciphertext, tag = cipher.encrypt_and_digest(plaintext.encode())
return nonce + ciphertext
def decrypt_aes(key, ciphertext):
nonce = ciphertext[:16]
ciphertext = ciphertext[16:]
cipher = AES.new(key, AES.MODE_EAX, nonce)
plaintext = cipher.decrypt(ciphertext)
return plaintext.decode()
- RSA 算法加密实现:
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
def generate_rsa_keypair():
key = RSA.generate(2048)
private_key = key.export_key()
public_key = key.publickey().export_key()
return private_key, public_key
def encrypt_rsa(public_key, plaintext):
key = RSA.import_key(public_key)
cipher = PKCS1_OAEP.new(key)
ciphertext = cipher.encrypt(plaintext.encode())
return ciphertext
def decrypt_rsa(private_key, ciphertext):
key = RSA.import_key(private_key)
cipher = PKCS1_OAEP.new(key)
plaintext = cipher.decrypt(ciphertext)
return plaintext.decode()
- ECC 算法加密实现:
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import serialization
def generate_ecc_keypair():
private_key = ec.generate_private_key(ec.SECP256R1())
public_key = private_key.public_key()
private_key_bytes = private_key.private_bytes(encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption())
public_key_bytes = public_key.public_bytes(encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo)
return private_key_bytes, public_key_bytes
def encrypt_ecc(public_key, plaintext):
public_key = serialization.load_pem_public_key(public_key)
ciphertext = public_key.encrypt(plaintext.encode(), ec.ECIES())
return ciphertext
def decrypt_ecc(private_key, ciphertext):
private_key = serialization.load_pem_private_key(private_key, password=None)
plaintext = private_key.decrypt(ciphertext, ec.ECIES())
return plaintext.decode()
- DES 算法加密实现:
from Crypto.Cipher import DES
def encrypt_des(key, plaintext):
cipher = DES.new(key, DES.MODE_ECB)
ciphertext = cipher.encrypt(plaintext.encode())
return ciphertext
def decrypt_des(key, ciphertext):
cipher = DES.new(key, DES.MODE_ECB)
plaintext = cipher.decrypt(ciphertext)
return plaintext.decode()
- 3DES 算法加密实现:
from Crypto.Cipher import DES3
def encrypt_3des(key, plaintext):
cipher = DES3.new(key, DES3.MODE_ECB)
ciphertext = cipher.encrypt(plaintext.encode())
return ciphertext
def decrypt_3des(key, ciphertext):
cipher = DES3.new(key, DES3.MODE_ECB)
plaintext = cipher.decrypt(ciphertext)
return plaintext.decode()
- Rabin 算法加密实现:
from Crypto.PublicKey import Rabin
from Crypto.Cipher import PKCS1_OAEP
def generate_rabin_keypair():
key = Rabin.generate(2048)
private_key = key.export_key()
public_key = key.publickey().export_key()
return private_key, public_key
def encrypt_rabin(public_key, plaintext):
key = Rabin.import_key(public_key)
cipher = PKCS1_OAEP.new(key)
ciphertext = cipher.encrypt(plaintext.encode())
return ciphertext
def decrypt_rabin(private_key, ciphertext):
key = Rabin.import_key(private_key)
cipher = PKCS1_OAEP.new(key)
plaintext = cipher.decrypt(ciphertext)
return plaintext.decode()
- ElGamal 算法加密实现:
from Crypto.PublicKey import ElGamal
from Crypto.Cipher import PKCS1_OAEP
def generate_elgamal_keypair():
key = ElGamal.generate(2048)
private_key = key.export_key()
public_key = key.publickey().export_key()
return private_key, public_key
def encrypt_elgamal(public_key, plaintext):
key = ElGamal.import_key(public_key)
cipher = PKCS1_OAEP.new(key)
ciphertext = cipher.encrypt(plaintext.encode())
return ciphertext
def decrypt_elgamal(private_key, ciphertext):
key = ElGamal.import_key(private_key)
cipher = PKCS1_OAEP.new(key)
plaintext = cipher.decrypt(ciphertext)
return plaintext.decode()
- Diffie-Hellman 算法密钥交换实现:
from cryptography.hazmat.primitives.asymmetric import dh
from cryptography.hazmat.primitives import serialization
def generate_dh_keypair():
parameters = dh.generate_parameters(generator=2, key_size=2048)
private_key = parameters.generate_private_key()
public_key = private_key.public_key()
private_key_bytes = private_key.private_bytes(encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption())
public_key_bytes = public_key.public_bytes(encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo)
return private_key_bytes, public_key_bytes
def derive_dh_shared_key(private_key, peer_public_key):
private_key = serialization.load_pem_private_key(private_key, password=None)
peer_public_key = serialization.load_pem_public_key(peer_public_key)
shared_key = private_key.exchange(peer_public_key)
return shared_key
以上代码示例中使用了一些第三方库,如 pycryptodome 和 cryptography,需要通过安装对应的库来使用。
原文地址: https://www.cveoy.top/t/topic/hh4 著作权归作者所有。请勿转载和采集!