You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
932 B
26 lines
932 B
from Crypto.PublicKey import DSA
|
|
from Crypto.Signature import DSS
|
|
from Crypto.Hash import SHA256
|
|
import json
|
|
|
|
#abrimos un fichero JSON con el texto y la firma
|
|
#viene codificamos en dos caracteres hexadecimales cada byte
|
|
f = open("mensajefirmado.txt", "r")
|
|
mensajeFirmado =f.read()
|
|
print (mensajeFirmado)
|
|
mensajeRecibido = json.loads(mensajeFirmado)
|
|
|
|
#creamos un verificador con la firma leida en el fichero JSON
|
|
#usamos la clave pública del remitente
|
|
f = open("public_key_firma.pem", "r")
|
|
hash_obj = SHA256.new(bytes.fromhex(mensajeRecibido["mensaje"]))
|
|
pub_key = DSA.import_key(f.read())
|
|
verificador = DSS.new(pub_key, 'fips-186-3')
|
|
|
|
# Verificar la autenticidad del mensaje recibido
|
|
# en realidad lo hacemos del hash de todo el mensaje
|
|
try:
|
|
verificador.verify(hash_obj, bytes.fromhex(mensajeRecibido["firma"]))
|
|
print ("El mensaje es AUTENTICO")
|
|
except ValueError:
|
|
print ("Este mensaje no ha sido firmado de forma válida") |