-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCP_client_AES.py
55 lines (45 loc) · 1.38 KB
/
TCP_client_AES.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
import socket
import time
import sys
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Cipher import AES
from idna import check_hyphen_ok
#TAMANHO MAXIMO QUE PODE SER ENCRIPTADO POR VEZ = 86 BYTES
def TCP_client_AES(HEADER_SIZE):
s = socket.socket()
host = "123.123.123.123"
port = 55443
f = open('arquivo.txt','rb')
contador_pacotes = 0
t0 = time.time()
s.connect((host, port))
key = s.recv(16)
cipher = AES.new(key, AES.MODE_EAX)
l = f.read()
ciphertext, tag = cipher.encrypt_and_digest(l)
size_pacote = 1000-HEADER_SIZE
#1000 - 16 do nonce e 16 da tag
s.send(cipher.nonce+tag+ciphertext[:968-HEADER_SIZE])
contador_pacotes+=1
i = 968-HEADER_SIZE
print("STARTED")
while (i < len(ciphertext)):
if contador_pacotes%10000==0:
print(f"pacote: {contador_pacotes}")
if i+size_pacote < len(ciphertext):
chunk = ciphertext[i:i+size_pacote]
else:
chunk = ciphertext[i:]
i += size_pacote
s.send(chunk)
contador_pacotes+=1
time.sleep(sys.float_info.min)
t1 = time.time()
print(f"{t1-t0} seg, {contador_pacotes} pacotes")
f.close()
s.shutdown(socket.SHUT_WR)
s.close()
return t1-t0, contador_pacotes
if __name__ == '__main__':
TCP_client_AES(66)