Untitled
unknown
plain_text
7 months ago
1.3 kB
5
Indexable
from Crypto.Cipher import Salsa20
from Crypto.Hash import Poly1305
from Crypto.Random import get_random_bytes
# 🔹 Claves
key = get_random_bytes(32)
nonce = get_random_bytes(8)
# 🔹 Cifrado con Salsa20
plaintext = b"Mensaje seguro con autenticación"
cipher = Salsa20.new(key=key, nonce=nonce)
ciphertext = cipher.encrypt(plaintext)
# 🔹 Crear MAC con Poly1305
mac_key = get_random_bytes(32) # Clave para Poly1305
poly = Poly1305.new(key=mac_key)
poly.update(nonce + ciphertext)
auth_tag = poly.digest()
# 🔹 Transmitimos (nonce, ciphertext, auth_tag)
transmitted_data = nonce + ciphertext + auth_tag
# 🔹 Verificación en el receptor
received_nonce = transmitted_data[:8]
received_ciphertext = transmitted_data[8:-16]
received_auth_tag = transmitted_data[-16:]
# Verificar autenticidad con Poly1305
poly = Poly1305.new(key=mac_key)
poly.update(received_nonce + received_ciphertext)
if poly.digest() == received_auth_tag:
print("✅ Mensaje auténtico")
cipher = Salsa20.new(key=key, nonce=received_nonce)
decrypted_text = cipher.decrypt(received_ciphertext)
print("🔓 Texto descifrado:", decrypted_text.decode())
else:
print("❌ Mensaje alterado o autenticación fallida")
Editor is loading...
Leave a Comment