-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
66 lines (46 loc) · 2.33 KB
/
main.py
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
from src.rsa import RSA
import hashlib
import base64
from pickle import dumps, loads
def main():
message = input("Insira uma mensagem para ser cifrada: ").encode()
rsa = RSA()
# 1.a) geração de chaves (p e q primos com no mínimo de 1024 bits)
print("\nGerando as chaves publica e privada...")
public_key, private_key = rsa.generate_keys()
#print("Public key: ", public_key)
#print("Private key: ", private_key)
# 1.b) cifração/decifração assimétrica RSA usando OAEP
encrypted_message = rsa.OAEPencrypt(message, public_key)
#print("Encrypted message: ", encrypted_message, "\n")
decrypted_message = rsa.OAEPdecrypt(encrypted_message, private_key)
#print("Decrypted message: ", decrypted_message)
# 2.a) calculo de hashes da mensagem em claro (função de hash SHA-3)
hash_sha3 = hashlib.sha3_256(message).digest()
print("Gerando o hash sha3 da menssagem...")
#print("Hash sha3: ", hash_sha3)
# 2.b) assinatura da mensagem (cifração do hash da mensagem)
signature_message = rsa.OAEPencrypt(hash_sha3, public_key)
print("Cifrando o hash com RSA-OAEP...")
#print("Signature message: ", signature_message)
# 2.c) formatação do resultado (caracteres especiais e informações para verificação em BASE64)
b64_message_encoded = base64.b64encode(dumps(signature_message))
print("Codificando em base64...")
#print("BASE64 encoded message: ", b64_message_encoded)
# 3.a) Parsing do documento assinado e decifração da mensagem (de acordo com a formatação usada, no caso BASE64)
b64_message_decoded = base64.b64decode(b64_message_encoded)
signature_message = loads(b64_message_decoded)
print("---------------\nDecodificando base64...")
# 3.b) Decifração da assinatura (decifração do hash)
decrypted_hash = rsa.OAEPdecrypt(signature_message, private_key)
print("Decodificando o hash da mensagem com RSA-OEAP...")
# 3.c) Verificação (cálculo e comparação do hash do arquivo)
# Mensagem modificada
#message = "Outra mensagem".encode()
hash_sha3_file = hashlib.sha3_256(message).digest()
if hash_sha3_file == decrypted_hash:
print("Verificação realizada com sucesso")
else:
print("Falha na verificação")
if __name__ == "__main__":
main()