Skip to content

Commit

Permalink
Ajout du chiffrement
Browse files Browse the repository at this point in the history
  • Loading branch information
sl4shme committed Oct 16, 2013
1 parent 7a5705d commit 6be4bcb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
1 change: 1 addition & 0 deletions bugReport
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#ajouter un option pour autoriser les connections uniquement depuis TOR
#ne pas afficher les chat commencant par / car ceci est souvent une commande rate
29 changes: 29 additions & 0 deletions crypt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/python
import hashlib
from Crypto import Random
from Crypto.Cipher import AES
import base64


class Crypt:
def __init__(self, password):
self.key = hashlib.sha256(password).digest()
BS = 16
self.pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
self.unpad = lambda s : s[0:-ord(s[-1])]

def encrypt(self, data):
data = self.pad(data)
iv = Random.new().read(AES.block_size)
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return base64.b64encode(iv + cipher.encrypt(data))

def decrypt(self, enc):
enc = base64.b64decode(enc)
iv = enc[:16]
cipher = AES.new(self.key, AES.MODE_CBC, iv)
dec = self.unpad(cipher.decrypt(enc[16:]))
if dec != '':
return dec
else:
return '<Encrypted>'
3 changes: 2 additions & 1 deletion help.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
"/bug <message> : Send a bug / suggestion report.",
"/list : List peoples in channel.",
"/afk : Toggle afk.",
"/notif <on|off> : Toggle visual flash notification on new message."]
"/notif <on|off> : Toggle visual flash notification on new message.",
"/encrypt <on|off> : Toggle encryption."]
16 changes: 13 additions & 3 deletions minion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import socket, os, re, hashlib, threading
import socket, os, re, hashlib, threading, crypt

class AfkManager(threading.Thread):
def __init__(self, minion):
Expand Down Expand Up @@ -46,6 +46,8 @@ def handlerGetComm(self, incMess):
self.handlerRemPeer(incMess[5:])
if incMess[1:4] == "msg":
self.handlerGetMess(incMess[5:])
if incMess[1:4] == "enc":
self.handlerGetEnc(incMess[5:])

def handlerAddPeer(self, mess):
pid, name = mess.split("|")
Expand All @@ -62,10 +64,17 @@ def handlerRemPeer(self, mess):
except:
pass

def handlerGetMess(self, message):
def handlerGetMess(self, mess):
if self.minion.screen.doNotif == True:
self.minion.screen.notif.flash = True
self.minion.screen.printMessage(str(message))
self.minion.screen.printMessage(str(mess))

def handlerGetEnc(self, mess):
if self.minion.encrypt == True:
mess = self.minion.crypto.decrypt(mess)
else :
mess = '<Encrypted>'
self.handlerGetMess(mess)

def handlerGetNick(self, pid):
mess="/msg "+self.minion.nickname+" "+self.minion.pid
Expand Down Expand Up @@ -93,6 +102,7 @@ def __init__(self, channel, scr, nickname):
self.channelHash=hashlib.sha256(self.channel).hexdigest()
self.nickname=nickname
self.afk=False
self.encrypt=False
self.mySocket=SocketManager(self)
self.mySocket.setDaemon(True)
self.mySocket.start()
Expand Down
21 changes: 17 additions & 4 deletions sschat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/python
import minion, screen, signal, help, re, time, socket, threading
import minion, screen, signal, help, re, time, socket, threading, crypt

class Sschat:
def __init__(self, channel="", nickname=""):
Expand All @@ -23,8 +23,13 @@ def main(self):
chatMessage= self.screen.getInput()
if chatMessage[0] != "/":
chatMessage = self.minion.nickname+" : "+chatMessage
self.minion.sendMessage("/msg "+chatMessage)
self.screen.printMessage(chatMessage)
if self.minion.encrypt == False:
self.minion.sendMessage("/msg "+chatMessage)
else :
chatMessage = "<e> "+chatMessage
encMessage = self.minion.crypto.encrypt(chatMessage)
self.minion.sendMessage("/enc "+encMessage)
self.screen.printMessage(chatMessage)
else:
newChannel = self.command(chatMessage[1:])
if newChannel:
Expand Down Expand Up @@ -90,6 +95,14 @@ def command(self, mess):
self.screen.clearConvers()
elif cmd == "help":
self.screen.scrollPrinter(help.help)
elif cmd == "encrypt" and len(args) == 1:
if args[0] == "on" and self.minion.encrypt == False:
self.screen.printMessage("Please enter the private key.")
self.minion.crypto=crypt.Crypt(self.screen.getInput())
self.minion.encrypt=True
if args[0] == "off" and self.minion.encrypt == True:
del self.minion.crypto
self.minion.encrypt=False
elif cmd == "list":
self.screen.printMessage("Peoples present in channel :")
self.minion.sendMessage("/get "+self.minion.pid)
Expand Down Expand Up @@ -157,5 +170,5 @@ def command(self, mess):
chat=Sschat()
while 1 :
newChannel, nickname = chat.main()
chat=None
del chat
chat=Sschat(newChannel, nickname)

0 comments on commit 6be4bcb

Please sign in to comment.