/
bluetooth_app.py
172 lines (138 loc) · 4.15 KB
/
bluetooth_app.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import sys
import threading
from bluetooth import *
import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
import ast
from Crypto.Cipher import AES
#server functions
def sendmesgs():
while True:
mesg = raw_input('<You>')
if(mesg=="close"):
client_sock.close()
server_sock.close()
mesg_len = len(mesg)
qt=mesg_len%16
if(qt==0):
ciphertext=enc_obj.encrypt(mesg)
client_sock.send(ciphertext)
else:
lenreq=16-qt
st='~'
st=st*lenreq
mesg=mesg+st
ciphertext=enc_obj.encrypt(mesg)
client_sock.send(ciphertext)
def receivemesgs():
while True:
mesg=client_sock.recv(1024)
print('mes_length',len(mesg))
plaintext=dec_obj.decrypt(mesg)
index=plaintext.find('~')
plfinal=plaintext[:index]
print('\n' + '<client>' + plfinal+'\n<You>')
#print('<You>',end='')
#client functions
def sendmesgc():
while True:
mesg = raw_input('<You>')
if(mesg=="close"):
sock.close()
mesg_len=len(mesg)
qt=mesg_len%16
if(qt==0):
ciphertext=enc_obj.encrypt(mesg)
sock.send(ciphertext)
else:
lenreq=16-qt
st='~'
st=st*lenreq
mesg=mesg+st
#print("mesg_len=",len(mesg))
ciphertext=enc_obj.encrypt(mesg)
sock.send(ciphertext)
def receivemesgc():
while True:
mesg=sock.recv(1024)
plaintext=dec_obj.decrypt(mesg)
index=plaintext.find('~')
plfinal=plaintext[:index]
print('\n' + '<server>' + plfinal +'\n<You>')
print("choose option to behave like\n1.As Server\n2.As Client")
option = input()
if(option==1):
#server code
server_sock=BluetoothSocket( RFCOMM )
print(server_sock)
PORT_ANY = 5
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
#UUID = "1e0ca4ea-299d-4335-93eb-27fcfe7fa848"
#advertise_service( server_sock,"Bluetooth_service",service_classes = [SERIAL_PORT_CLASS],profiles = [SERIAL_PORT_PROFILE])
print("waiting..")
client_sock, client_info = server_sock.accept()
print("Accepted connection from ",client_info)
#rsa generation
random_generator = Random.new().read
key = RSA.generate(1024, random_generator) #generate pub and priv key
publickey = key.publickey() # pub key export for exchange
public_key_string = publickey.exportKey()
print("sending rsa publickey")
client_sock.send(public_key_string)
print("waiting for aes key..")
aes_key_client=client_sock.recv(1024)
#print("aeskey received = "+aes_key_client)
decrypted = key.decrypt(ast.literal_eval(aes_key_client))
print 'decrypted', decrypted
dec_obj= AES.new(decrypted,AES.MODE_CBC,'This is an IV456')
enc_obj= AES.new(decrypted,AES.MODE_CBC,'This is an IV456')
#chat start
tsend = threading.Thread(target=sendmesgs,args=())
treceive = threading.Thread(target=receivemesgs,args=())
tsend.start()
treceive.start()
tsend.join()
treceive.join()
print('connection closed')
elif(option==2):
#client code
print "performing inquiry..."
nearby_devices = discover_devices(lookup_names = True)
print "found %d devices" % len(nearby_devices)
i=1;
for name, addr in nearby_devices:
print "%s. %s - %s" % (i,addr, name)
i=i+1
print "choose which server to connect"
chosen_option = input();
server_address = nearby_devices[chosen_option-1][0]
print("chosen option = "+server_address)
PORT_ANY = 5
sock=BluetoothSocket( RFCOMM )
print("printing sock=")
print(sock)
sock.connect((server_address, PORT_ANY))
print("connection successfull")
tsend1 = threading.Thread(target=sendmesgc,args=())
trecieve1 = threading.Thread(target=receivemesgc,args=())
#rsa key receiving
print("waiting for rsa key...")
public_key_string = sock.recv(1024)
public_key = RSA.importKey(public_key_string)
print("public key received")
#aes generation
aes_key = 'This is a key123'
encrypted = public_key.encrypt(aes_key, 16)
print("sending encrypted aes key...")
sock.send(str(encrypted))
dec_obj= AES.new(aes_key,AES.MODE_CBC,'This is an IV456')
enc_obj= AES.new(aes_key,AES.MODE_CBC,'This is an IV456')
#chat start
tsend1.start()
trecieve1.start()
tsend1.join()
trecieve1.join()
else:
print("wrong option opted..")