funcionando trabalho 4

This commit is contained in:
José Henrique 2023-10-22 17:15:12 -03:00
parent c8799ed37a
commit 83f806b815
6 changed files with 45 additions and 43 deletions

View File

@ -1,4 +1,5 @@
from Crypto.Cipher import AES
import base64
class AESCipher(object):
def __init__(self, key):
@ -8,14 +9,16 @@ class AESCipher(object):
def encrypt(self, raw):
cipher = AES.new(self.key, AES.MODE_EAX, nonce=self.nonce)
ciphertext, _ = cipher.encrypt_and_digest(raw.encode())
return ciphertext
return base64.b64encode(ciphertext)
def decrypt(self, enc):
if type(enc) == str:
enc = eval(enc)
enc = base64.b64decode(enc)
cipher = AES.new(self.key, AES.MODE_EAX, nonce=self.nonce)
plaintext = cipher.decrypt(enc)
print('plaintext', type(plaintext), plaintext)
print('plaintext', plaintext.decode())
return plaintext.decode()

View File

@ -12,14 +12,14 @@ def process_message(message):
message = message.split("||")
if message[0] == "register":
body = message[1].split(",")
body = message[1].split("<>")
username = body[0]
password = body[1]
password = sha256(password.encode()).hexdigest()
key = message[3]
return register_user(username, password, key)
elif message[0] == "login":
body = message[1].split(",")
body = message[1].split("<>")
username = body[0]
password = body[1]
password = sha256(password.encode()).hexdigest()
@ -31,7 +31,7 @@ def process_message(message):
T_c_tgs = {ID_C + T_R + K_c_tgs}K_tgs
M2 = [{K_c_tgs + N_1}Kc + T_c_tgs]
"""
body = message[1].split(",")
body = message[1].split("<>")
ID_C = body[0]
key = get_key(ID_C)
@ -40,17 +40,17 @@ def process_message(message):
M2_AES = AESCipher(K_tgs)
inner_message = AES.decrypt(body[1])
inner_message = inner_message.split(",")
inner_message = inner_message.split("<>")
T_R = inner_message[1]
N1 = inner_message[2]
K_c_tgs = os.urandom(16)
T_c_tgs = f"{ID_C},{T_R},{K_c_tgs}"
T_c_tgs = f"{ID_C}<>{T_R}<>{K_c_tgs}"
T_c_tgs = M2_AES.encrypt(T_c_tgs)
M2 = f"{K_c_tgs},{N1}"
M2 = f"{K_c_tgs}<>{N1}"
M2 = AES.encrypt(M2)
M2 = f"{M2},{T_c_tgs}"
M2 = f"{M2}<>{T_c_tgs}"
print('sending m2', M2)
return M2
else:
@ -58,19 +58,19 @@ def process_message(message):
def register_user(username, password, key):
with open("users.data", "a") as f:
f.write(f"{username},{password},{key.encode()}\n")
return f"success,{key}"
f.write(f"{username}<>{password}<>{key.encode()}\n")
return f"success<>{key}"
def login(username, password):
with open("users.data", "r", newline='\n') as f:
for line in f:
line = line.strip()
line = line.replace("\n", "")
user, pw, key = line.split(",")
user, pw, key = line.split("<>")
if user == username and pw == password:
key = eval(key)
print(f"Login succeeded!")
return f"success,{key}"
return f"success<>{key}"
print("Login failed!")
return "failure"
@ -81,7 +81,7 @@ def get_key(username):
for line in f:
line = line.strip()
line = line.replace("\n", "")
user, _, key = line.split(",")
user, _, key = line.split("<>")
if user == username:
return eval(key)

View File

@ -13,7 +13,7 @@ def register_user(username, password):
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}<>{key}"
s.sendall(message.encode())
data = s.recv(4096)
@ -27,12 +27,12 @@ def login(username, password):
global CLIENT_ID, KEY
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, AS_PORT))
message = f"login||{username},{password}"
message = f"login||{username}<>{password}"
s.sendall(message.encode())
data = s.recv(4096)
data = data.decode()
data = data.split(",")
data = data.split("<>")
if data[0] == "success":
print(f"Login succeeded!")
CLIENT_ID = username
@ -57,9 +57,9 @@ def print_something():
M4 = None
M6 = None
M1_inner = f"{ID_S},{T_R},{N1}"
M1_inner = f"{ID_S}<>{T_R}<>{N1}"
M1_inner = AES.encrypt(M1_inner)
M1 = f"{ID_C},{M1_inner}"
M1 = f"{ID_C}<>{M1_inner}"
message = f"request||{M1}"
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, AS_PORT))
@ -75,9 +75,9 @@ def print_something():
M2 = [{K_c_tgs + N_1}Kc + T_c_tgs]
M3 = [{ID_C + ID_S + T_R + N2}K_c_tgs + T_c_tgs]
'''
M2 = M2.split(",")
M2 = M2.split("<>")
M2_inner = AES.decrypt(M2[0])
M2_inner = M2_inner.split(",")
M2_inner = M2_inner.split("<>")
K_c_tgs = eval(M2_inner[0])
N1 = M2_inner[1]
@ -86,9 +86,9 @@ def print_something():
M2_AES = AESCipher(K_c_tgs)
N2 = randint(0, 1000000)
M3_inner = f"{ID_C},{ID_S},{T_R},{N2}"
M3_inner = f"{ID_C}<>{ID_S}<>{T_R}<>{N2}"
M3_inner = M2_AES.encrypt(M3_inner)
M3 = f"{M3_inner},{T_c_tgs}"
M3 = f"{M3_inner}<>{T_c_tgs}"
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, TGS_PORT))
message = f"request||{M3}"
@ -105,10 +105,10 @@ def print_something():
T_c_s = {ID_C + T_A + K_c_s}K_s
M5 = [{ID_C + T_A + S_R + N3}K_c_s + T_c_s]
'''
M4 = M4.split(",")
M4 = M4.split("<>")
M4_AES = AESCipher(K_c_tgs)
M4_inner = M4_AES.decrypt(M4[0])
M4_inner = M4_inner.split(",")
M4_inner = M4_inner.split("<>")
print('M4_inner', M4_inner)
K_c_s = eval(M4_inner[0])
@ -120,9 +120,9 @@ def print_something():
M5_AES = AESCipher(K_c_s)
N3 = randint(0, 1000000)
M5_inner = f"{ID_C},{T_A},{ID_S},{N3}"
M5_inner = f"{ID_C}<>{T_A}<>{ID_S}<>{N3}"
M5_inner = M5_AES.encrypt(M5_inner)
M5 = f"{M5_inner},{T_c_s}"
M5 = f"{M5_inner}<>{T_c_s}"
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, SERVICE_PORT))
@ -138,10 +138,10 @@ def print_something():
'''
M6 = [{Resposta, N3}K_c_s]
'''
M6 = M6.split(",")
M6 = M6.split("<>")
M6_AES = AESCipher(K_c_s)
M6_inner = M6_AES.decrypt(M6[0])
M6_inner = M6_inner.split(",")
M6_inner = M6_inner.split("<>")
resposta = M6_inner[0]
N3 = M6_inner[1]

View File

@ -19,7 +19,7 @@ def process_message(message):
M5 = [{ID_C + T_A + S_R + N3}K_c_s + T_c_s]
M6 = [{Resposta, N3}K_c_s]
"""
body = message[1].split(",")
body = message[1].split("<>")
K_s = C_K_s
T_c_s_AES = AESCipher(K_s)
@ -27,20 +27,19 @@ def process_message(message):
T_c_s = body[1]
T_c_s = T_c_s_AES.decrypt(T_c_s)
T_c_s = T_c_s.split(",")
T_c_s = T_c_s.split("<>")
ID_C = T_c_s[0]
T_A = T_c_s[1]
K_c_s = T_c_s[2]
K_c_s = eval(T_c_s[2])
M5_inner_AES = AESCipher(K_c_s)
M5_inner = M5_inner_AES.decrypt(M5_inner)
M5_inner = M5_inner.split(",")
M5_inner = M5_inner.split("<>")
ID_S = M5_inner[2]
N3 = M5_inner[3]
# generate M6
M6_inner = f"{randint(0, 100)},{N3}"
M6_inner = f"{randint(0, 100)}<>{N3}"
M6_inner_AES = AESCipher(K_c_s)
M6_inner = M6_inner_AES.encrypt(M6_inner)

View File

@ -19,42 +19,42 @@ def process_message(message):
M4 = [{K_c_s + T_A + N2}K_c_tgs + T_c_s]
T_c_s = {ID_C + T_A + K_c_s}K_s
"""
body = message[1].split(",")
body = message[1].split("<>")
K_tgs = C_K_tgs
M3 = body[0]
M3 = M3.split(",")
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)
T_c_tgs = T_c_tgs.split(",")
T_c_tgs = T_c_tgs.split("<>")
ID_C = T_c_tgs[0]
T_R = int(T_c_tgs[1])
K_c_tgs = eval(T_c_tgs[2])
M3_inner_AES = AESCipher(K_c_tgs)
M3_inner = M3_inner_AES.decrypt(M3_inner)
M3_inner = M3_inner.split(",")
M3_inner = M3_inner.split("<>")
ID_S = M3_inner[1]
N2 = M3_inner[3]
# generate M4
K_c_s = os.urandom(16)
T_A = 600 if T_R > 600 else T_R
T_c_s = f"{ID_C},{T_A},{K_c_s}"
T_c_s = f"{ID_C}<>{T_A}<>{K_c_s}"
K_s = C_K_s
T_c_s_AES = AESCipher(K_s)
T_c_s = T_c_s_AES.encrypt(T_c_s)
M4_inner = f"{K_c_s},{T_A},{N2}"
M4_inner = f"{K_c_s}<>{T_A}<>{N2}"
M4_inner_AES = AESCipher(K_c_tgs)
M4_inner = M4_inner_AES.encrypt(M4_inner)
M4 = f"{M4_inner},{T_c_s}"
M4 = f"{M4_inner}<>{T_c_s}"
return M4
def main():

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"b'\\x9cs\\xb9\\x9f*\\xd4\\xa8@!.`\\xb1\\xa5y\\x84\\xfcv\\xdd\\x15X\\x0f`\\xfd\\xb1I\\xbf\\xb4\\xc9\\xaa\\xa2{\\x7f'"