not working trab 4

This commit is contained in:
José Henrique 2023-10-23 17:26:05 -03:00
parent 83f806b815
commit 7b1b7860b6
8 changed files with 66 additions and 44 deletions

View File

@ -20,5 +20,4 @@ class AESCipher(object):
cipher = AES.new(self.key, AES.MODE_EAX, nonce=self.nonce)
plaintext = cipher.decrypt(enc)
print('plaintext', plaintext.decode())
return plaintext.decode()

View File

@ -3,6 +3,7 @@ from hashlib import sha256
import socket
from AES import AESCipher
import os
import base64
HOST = "127.0.0.1"
PORT = AS_PORT
@ -16,7 +17,7 @@ def process_message(message):
username = body[0]
password = body[1]
password = sha256(password.encode()).hexdigest()
key = message[3]
key = os.urandom(32)
return register_user(username, password, key)
elif message[0] == "login":
body = message[1].split("<>")
@ -34,16 +35,17 @@ def process_message(message):
body = message[1].split("<>")
ID_C = body[0]
key = get_key(ID_C)
key = get_ktgs()
AES = AESCipher(key)
K_tgs = C_K_tgs
M2_AES = AESCipher(K_tgs)
inner_message = AES.decrypt(body[1])
inner_message = inner_message.split("<>")
T_R = inner_message[1]
N1 = inner_message[2]
# generate m2
K_tgs = get_ktgs()
M2_AES = AESCipher(K_tgs)
K_c_tgs = os.urandom(16)
T_c_tgs = f"{ID_C}<>{T_R}<>{K_c_tgs}"
T_c_tgs = M2_AES.encrypt(T_c_tgs)
@ -51,14 +53,13 @@ def process_message(message):
M2 = f"{K_c_tgs}<>{N1}"
M2 = AES.encrypt(M2)
M2 = f"{M2}<>{T_c_tgs}"
print('sending m2', M2)
return M2
else:
print("Invalid message")
def register_user(username, password, key):
with open("users.data", "a") as f:
f.write(f"{username}<>{password}<>{key.encode()}\n")
f.write(f"{username}<>{password}<>{base64.b64encode(key)}\n")
return f"success<>{key}"
def login(username, password):
@ -69,21 +70,23 @@ def login(username, password):
user, pw, key = line.split("<>")
if user == username and pw == password:
key = eval(key)
print(f"Login succeeded!")
key = base64.b64decode(key)
print(f"Login succeeded [{username}]")
return f"success<>{key}"
print("Login failed!")
print(f"Login failed! [{username}]")
return "failure"
def get_key(username):
return C_User_Key
def get_ktgs():
message = f"getkey"
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, TGS_PORT))
s.sendall(message.encode())
with open("users.data", "r", newline='\n') as f:
for line in f:
line = line.strip()
line = line.replace("\n", "")
user, _, key = line.split("<>")
if user == username:
return eval(key)
data = s.recv(4096)
data = data.decode()
key = eval(data)
key = base64.b64decode(key)
return key
def main():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:

View File

@ -12,16 +12,16 @@ def register_user(username, password):
global CLIENT_ID, KEY
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, AS_PORT))
key = os.urandom(32)
message = f"register||{username}<>{password}<>{key}"
message = f"register||{username}<>{password}"
s.sendall(message.encode())
data = s.recv(4096)
data = data.decode()
if data == "success":
data = data.split("<>")
if data[0] == "success":
print("Login succeeded!")
CLIENT_ID = username
KEY = key
KEY = eval(data[1])
def login(username, password):
global CLIENT_ID, KEY
@ -36,7 +36,7 @@ def login(username, password):
if data[0] == "success":
print(f"Login succeeded!")
CLIENT_ID = username
KEY = data[1]
KEY = eval(data[1])
def print_something():
global CLIENT_ID, KEY
@ -45,7 +45,7 @@ def print_something():
print("You must register or login first!")
return
AES = AESCipher(C_User_Key)
AES = AESCipher(KEY)
ID_C = CLIENT_ID
ID_S = "PRINTER"
@ -63,12 +63,12 @@ def print_something():
message = f"request||{M1}"
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, AS_PORT))
print('sending m1', message)
print('Sending M1', message)
s.sendall(message.encode())
data = s.recv(4096)
data = data.decode()
print('received m2', data)
print('Received M2', data)
M2 = data
'''
@ -92,12 +92,12 @@ def print_something():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, TGS_PORT))
message = f"request||{M3}"
print('sending m3', message)
print('Sending M3', message)
s.sendall(message.encode())
data = s.recv(4096)
data = data.decode()
print('received m4', data)
print('Received M4', data)
M4 = data
'''
@ -109,7 +109,6 @@ def print_something():
M4_AES = AESCipher(K_c_tgs)
M4_inner = M4_AES.decrypt(M4[0])
M4_inner = M4_inner.split("<>")
print('M4_inner', M4_inner)
K_c_s = eval(M4_inner[0])
T_A = M4_inner[1]
@ -127,12 +126,12 @@ def print_something():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, SERVICE_PORT))
message = f"request||{M5}"
print('sending m5', message)
print('Sending M5', message)
s.sendall(message.encode())
data = s.recv(4096)
data = data.decode()
print('received m6', data)
print('Received M6', data)
M6 = data
'''

View File

@ -1,7 +1,3 @@
AS_PORT = 65431
TGS_PORT = 65433
SERVICE_PORT = 65434
C_User_Key = b'\xb0O\x12M\xfe\x99\xc0\xff\xa9B\x12e\xdf\x89\xec\x06\x8fhXy\x95\xc8\xdf\x80\x03F\xd72\x11\xb6\xdb\x06'
C_K_tgs = b'\xb0O\x12M\xfe\x99\xc0\xff\xa9B\x12e\xdf\x89\xec\x06\x8fhXy\x95\xc8\xdf\x80\x03F\xd72\x11\xb6\xdb\x06'
C_K_s = b'\xb0O\x12M\xfe\x99\xc0\xff\xa9B\x12e\xdf\x89\xec\x06\x8fhXy\x95\xc8\xdf\x80\x03F\xd72\x11\xb6\xdb\x06'
AS_PORT = 65000
TGS_PORT = 65001
SERVICE_PORT = 65002

View File

@ -4,14 +4,18 @@ import socket
from AES import AESCipher
import os
from random import randint
import base64
HOST = "127.0.0.1"
PORT = SERVICE_PORT
SERVICE_KEY = os.urandom(32)
def process_message(message):
print(message)
message = message.split("||")
if message[0] == "getkey":
return f"{base64.b64encode(SERVICE_KEY)}"
if message[0] == "request":
"""
T_c_s = {ID_C + T_A + K_c_s}K_s
@ -20,7 +24,7 @@ def process_message(message):
M6 = [{Resposta, N3}K_c_s]
"""
body = message[1].split("<>")
K_s = C_K_s
K_s = SERVICE_KEY
T_c_s_AES = AESCipher(K_s)
M5_inner = body[0]

View File

@ -3,14 +3,19 @@ from common import *
import socket
from AES import AESCipher
import os
import base64
HOST = "127.0.0.1"
PORT = TGS_PORT
TGS_KEY = os.urandom(32)
def process_message(message):
print(message)
message = message.split("||")
if message[0] == "getkey":
return f"{base64.b64encode(TGS_KEY)}"
if message[0] == "request":
"""
T_c_tgs = {ID_C + T_R + K_c_tgs}K_tgs
@ -20,13 +25,12 @@ def process_message(message):
T_c_s = {ID_C + T_A + K_c_s}K_s
"""
body = message[1].split("<>")
K_tgs = C_K_tgs
K_tgs = TGS_KEY
M3 = body[0]
M3 = M3.split("<>")
M3_inner = M3[0]
T_c_tgs = body[1]
print('T_c_tgs', type(T_c_tgs), T_c_tgs)
M3_AES = AESCipher(K_tgs)
T_c_tgs = M3_AES.decrypt(T_c_tgs)
@ -46,7 +50,7 @@ def process_message(message):
T_A = 600 if T_R > 600 else T_R
T_c_s = f"{ID_C}<>{T_A}<>{K_c_s}"
K_s = C_K_s
K_s = get_ks()
T_c_s_AES = AESCipher(K_s)
T_c_s = T_c_s_AES.encrypt(T_c_s)
@ -57,6 +61,18 @@ def process_message(message):
M4 = f"{M4_inner}<>{T_c_s}"
return M4
def get_ks():
message = f"getkey"
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, SERVICE_PORT))
s.sendall(message.encode())
data = s.recv(4096)
data = data.decode()
key = eval(data)
key = base64.b64decode(key)
return key
def main():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
@ -74,4 +90,8 @@ def main():
conn.sendall(response.encode())
if __name__ == "__main__":
if not os.path.exists("tgs_db.data"):
with open("tgs_db.data", "w") as f:
pass
main()

1
trabalho4/tgs_db.data Normal file
View File

@ -0,0 +1 @@
fake<>PRINTER<>b'+@,\xb7!\xd4\x9b,\x0e\xa3<\xe4]\x95\xda`_\xa6\x19$C|\x17\x92k\r-\xf0\xbe^7\xf6'

View File

@ -1 +1 @@
fake<>a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3<>b"b'\\x9cs\\xb9\\x9f*\\xd4\\xa8@!.`\\xb1\\xa5y\\x84\\xfcv\\xdd\\x15X\\x0f`\\xfd\\xb1I\\xbf\\xb4\\xc9\\xaa\\xa2{\\x7f'"
fake<>a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3<>b'zl+psilWmCU547E7BGr4b8Edq7fX8XVkY7J8MfpVMS0='