-
Notifications
You must be signed in to change notification settings - Fork 0
/
rc4.py
153 lines (113 loc) · 4.56 KB
/
rc4.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
# -*- coding: utf-8 -*-
from Tkinter import *
import socket
from threading import Thread
from random import randint, choice
import random
import sys
class RC4:
S = []
p = q = None
key = "skioaujh"
def __init__(self):
self.key = self.convertKey()
def KSA(self): # Key Scheduling Algorithm
self.p = 0
self.q = 0
keylenght = len(self.key)
# Usado para inicializar a permutação no array S.
# primeiro preenchemos o array S com valores de 0 à 255
self.S = range(256)
# depois somamos o valor de j, o valor de S apontado por i e o
# valor de K (chave) apontado por i e armazenamos na variável j.
# trocamos os valores entre S[i] e S[j]
j = 0
for i in range(256):
j = (j + self.S[i] + self.key[i % keylenght]) % 256
self.S[i], self.S[j] = self.S[j], self.S[i]
def PRGA(self): # Pseudo-Random Generation Algorithm
# Para todas repetições necessárias, o PRGA modifica o estado e
# a saída do byte resultante. Em cada repetição:
# O PRGA incrementa em 1 a variável p.
self.p = (self.p + 1) % 256
## Adiciona o valor de S apontado por p com q e armazena o resultado em q.
self.q = (self.q + self.S[self.p]) % 256
## Troca os valores entre S[p] e S[q].
self.S[self.p], self.S[self.q] = self.S[self.q], self.S[self.p]
## A saída é então calculada entre o valor de S
## apontado por S[p] + S[q] que será Xorada com a mensagem original.
return self.S[(self.S[self.p] + self.S[self.q]) % 256]
def encrypt(self, plainText):
self.KSA()
return "".join("%02X" % (ord(c) ^ self.PRGA()) for c in plainText)
def decrypt(self, cipher):
self.KSA()
byteList = []
for i in range(0, len(cipher), 2):
byte = cipher[i:i+2]
byteList.append(int(byte, 16))
return "".join([chr(byte ^ self.PRGA()) for byte in byteList])
def convertKey(self):
return [ord(c) for c in self.key]
####################################################################################
############################# INTERFACE AND SOCKETS ################################
####################################################################################
class Receive():
def __init__(self, server, messagesLog):
self.server = server
self.messagesLog = messagesLog
while 1:
try:
text = self.server.recv(1024)
text = RC4().decrypt(text)
messagesLog.insert(INSERT, text + "\n")
except Exception as e:
print(e)
break
class App(Thread):
RECEIVE_UDP_ON_IP = "127.0.0.1"
RECEIVE_UDP_ON_PORT = 5005
server = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
server.bind((RECEIVE_UDP_ON_IP, RECEIVE_UDP_ON_PORT))
def __init__(self, master):
Thread.__init__(self)
# text to show messages
self.text = Text(master)
self.text.pack()
# inputs - IP AND PORT
ipLabelVar = StringVar()
ipLabel = Label(master, textvariable = ipLabelVar)
ipLabelVar.set("Destination IP Address:")
ipLabel.pack()
entryIP = Entry(master, width = 15)
entryIP.pack()
portLabelVar = StringVar()
portLabel = Label(master, textvariable = portLabelVar)
portLabelVar.set("Destination Port:")
portLabel.pack()
entryPort = Entry(master, width = 5)
entryPort.pack()
# input - MESSAGE CONTENT
entryMessage = Entry(master, width = 70)
entryMessage.pack()
entryIP.focus_set()
# button - SEND MESSAGE
def getEntryText():
# text.insert(INSERT, entryMessage.get())
message = "Message from " + self.RECEIVE_UDP_ON_IP + ": " + entryMessage.get()
self.text.insert(INSERT, "Message sent: " + entryMessage.get() + "\n")
UDP_IP = entryIP.get()
UDP_PORT = int(entryPort.get())
message = RC4().encrypt(message)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.sendto(message, (UDP_IP, UDP_PORT))
entryMessage.delete(0, 'end')
sendButton = Button(master, text="Send message", width=10, command=getEntryText)
sendButton.pack()
def run(self):
Receive(self.server, self.text)
master = Tk()
master.title("UDP RC4 Chat")
app = App(master).start()
mainloop()