-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathejercicio_10.py
More file actions
73 lines (54 loc) · 2.7 KB
/
ejercicio_10.py
File metadata and controls
73 lines (54 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import gnupg
gpg = gnupg.GPG()
# Imoprtamos las claves. Las abrimos en modo binario
with open("./assets/Pedro-publ.txt", 'rb') as f:
pedro_public_key = gpg.import_keys(f.read())
with open("./assets/RRHH-publ.txt", 'rb') as f:
rrhh_public_key = gpg.import_keys(f.read())
with open("./assets/RRHH-priv.txt", 'rb') as f:
rrhh_private_key = gpg.import_keys(f.read())
# Tarea 1: Verificar la firma del mensaje de Pedro usando su clave pública
## Leemos el mensaje firmado
with open("./assets/MensajeRespoDeRaulARRHH.sig", 'rb') as f:
signed_message = f.read()
## Verificamos la firma
verified = gpg.verify(signed_message)
print(f"Firma valida: {verified.valid}")
print(f"Firmado por: {verified.username}")
print(f"Fingerprint: {verified.fingerprint}")
print(f"Fecha de firma: {verified.sig_timestamp}\n")
for k in gpg.list_keys():
print(k['uids'], k['fingerprint'])
# Tarea 2: Firmar el mensaje de respuesta usando la clave privada de RRHH
response_message = "Viendo su perfil en el mercado, hemos decidido ascenderle y mejorarle un 25% su salario.\nSaludos."
## Buscamos el fingerprint de la clave privada de RRHH
private_keys = gpg.list_keys(secret=True)
rrhh_private_key_fingerprint = None
for key in private_keys:
if "RRHH" in key['uids'][0]:
rrhh_private_key_fingerprint = key['fingerprint']
break
## Si no se encuentra, usamos la primera clave privada disponible
if not rrhh_private_key_fingerprint and private_keys:
rrhh_fingerprint = private_keys[0]['fingerprint']
## Firmamos el mensaje de respuesta
signed_response = gpg.sign(response_message, keyid=rrhh_private_key_fingerprint, passphrase="123456", detach=False)
with open("./results/MensajeFirmadoDeRRHH.sig", 'w') as f:
f.write(str(signed_response))
# Tarea 3: Cifrar el mensaje confidencial usando las claves públicas de Pedro y RRHH
confidential_message = "Estamos todos de acuerdo, el ascenso será el mes que viene, agosto, si no hay sorpresas."
## Obtenemos los fingerprints de las claves públicas
pedro_public_key_fingerprint = None
rrhh_public_key_fingerprint = None
public_keys = gpg.list_keys()
for key in public_keys:
uids = str(key.get('uids', []))
if "Pedro" in uids and not pedro_public_key_fingerprint:
pedro_public_key_fingerprint = key['fingerprint']
if "RRHH" in uids and not rrhh_public_key_fingerprint:
rrhh_public_key_fingerprint = key['fingerprint']
## Coframos el mensaje confidencial para Pedro y RRHH
recipients = [pedro_public_key_fingerprint, rrhh_public_key_fingerprint]
message_encrypted = gpg.encrypt(confidential_message, recipients, passphrase="123456", always_trust=True)
with open("./results/MensajeConfidencialParaPedroYRRHH.gpg", 'w') as f:
f.write(str(message_encrypted))