Crypting: Add rsa crypting. Add symmetric keys. Mvp is ready
This commit is contained in:
parent
de24ac3b11
commit
c379b82318
52
client.py
52
client.py
|
|
@ -1,11 +1,24 @@
|
||||||
|
from cryptography.fernet import Fernet
|
||||||
import threading
|
import threading
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
import rsa
|
import rsa
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
class Client:
|
class Client:
|
||||||
|
|
||||||
|
def _key_gen(self) -> None:
|
||||||
|
|
||||||
|
(pubkey, privkey) = rsa.newkeys(512)
|
||||||
|
|
||||||
|
with open("private.pem", "wb") as f:
|
||||||
|
f.write(privkey.save_pkcs1())
|
||||||
|
|
||||||
|
with open("public.pem", "wb") as f:
|
||||||
|
f.write(pubkey.save_pkcs1())
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, username: str):
|
def __init__(self, username: str):
|
||||||
self.server = "95.165.158.131"
|
self.server = "95.165.158.131"
|
||||||
self.port = 80
|
self.port = 80
|
||||||
|
|
@ -18,6 +31,9 @@ class Client:
|
||||||
self.key_url = f"{self.base_url}/get_key"
|
self.key_url = f"{self.base_url}/get_key"
|
||||||
|
|
||||||
self.pubkey = None
|
self.pubkey = None
|
||||||
|
self.privkey = None
|
||||||
|
self.symetric_key = None
|
||||||
|
self.fernet = None
|
||||||
|
|
||||||
|
|
||||||
def send_info(self):
|
def send_info(self):
|
||||||
|
|
@ -26,7 +42,8 @@ class Client:
|
||||||
user_input = input("You're message: ")
|
user_input = input("You're message: ")
|
||||||
message = f'{self.username}: {user_input}'
|
message = f'{self.username}: {user_input}'
|
||||||
requests.post(self.talk_url, data={
|
requests.post(self.talk_url, data={
|
||||||
"text": rsa.encrypt(message.encode('utf8'), self.pubkey)
|
"text": self.fernet.encrypt(message.encode()),
|
||||||
|
"username": self.username
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -39,27 +56,42 @@ class Client:
|
||||||
continue
|
continue
|
||||||
last_try = r.json()
|
last_try = r.json()
|
||||||
os.system("cls")
|
os.system("cls")
|
||||||
for msg in last_try["status"]:
|
if len(last_try['status']) > 0:
|
||||||
print(f"{rsa.decrypt(msg.encode(), self.seckey)}\n")
|
i = 0
|
||||||
|
for msg in last_try["status"]:
|
||||||
|
actual_message = self.fernet.decrypt(msg.encode()).decode("utf-8")
|
||||||
|
if i == 0:
|
||||||
|
users = last_try["users_in_chat"]
|
||||||
|
print(f"Users in chat: {users}\n\n")
|
||||||
|
print(f"{actual_message}\n")
|
||||||
|
else:
|
||||||
|
print(f"{actual_message}\n")
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
|
||||||
def _key_request(self) -> None:
|
def _key_request(self) -> None:
|
||||||
|
with open('private.pem', 'rb') as f:
|
||||||
|
self.privkey = rsa.PrivateKey.load_pkcs1(f.read())
|
||||||
|
|
||||||
with requests.get(self.key_url) as r:
|
with open("public.pem", 'rb') as f:
|
||||||
with open("public_rec.pem", 'wb') as f:
|
with requests.get(self.key_url, data={"pubkey": f.read(), "username": self.username}, stream=True) as r:
|
||||||
f.write(r.text.encode())
|
message = r.raw.read(999)
|
||||||
|
self.symetric_key = rsa.decrypt(message, self.privkey)
|
||||||
|
self.fernet = Fernet(self.symetric_key)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _remove_keys(self) -> None:
|
def _remove_keys(self) -> None:
|
||||||
with open('public_rec.pem', 'wb') as f:
|
os.remove("private.pem")
|
||||||
pass
|
os.remove("public.pem")
|
||||||
|
|
||||||
|
|
||||||
def _validate_keys(self) -> None:
|
def _validate_keys(self) -> None:
|
||||||
|
|
||||||
|
self._key_gen()
|
||||||
self._key_request()
|
self._key_request()
|
||||||
|
|
||||||
with open('public_rec.pem', "rb") as f:
|
with open('public.pem', "rb") as f:
|
||||||
first_key = f.read()
|
first_key = f.read()
|
||||||
|
|
||||||
self.pubkey = rsa.PublicKey.load_pkcs1(first_key)
|
self.pubkey = rsa.PublicKey.load_pkcs1(first_key)
|
||||||
|
|
@ -80,5 +112,5 @@ class Client:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
c = Client("sneaky") # input("Who are you? \t")
|
c = Client(input("Who are you? \t"))
|
||||||
c()
|
c()
|
||||||
BIN
preview.png
BIN
preview.png
Binary file not shown.
|
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 23 KiB |
13
readme.MD
13
readme.MD
|
|
@ -1,4 +1,4 @@
|
||||||
here is simple online chat what can be runned in cmd
|
here is secured online chat what can be runned in cmd
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
@ -16,11 +16,8 @@ how to run clear client
|
||||||
* pip install -r reqs.txt
|
* pip install -r reqs.txt
|
||||||
* python client.py
|
* python client.py
|
||||||
|
|
||||||
to do
|
|
||||||
|
|
||||||
* rsa
|
crypting pipeline
|
||||||
|
|
||||||
potential crypting pipeline
|
|
||||||
|
|
||||||
* Client making priv key
|
* Client making priv key
|
||||||
* Server making symmetric key
|
* Server making symmetric key
|
||||||
|
|
@ -28,4 +25,8 @@ potential crypting pipeline
|
||||||
* Server crypting symmetric key and sending to client
|
* Server crypting symmetric key and sending to client
|
||||||
* Client encrypting private key
|
* Client encrypting private key
|
||||||
* And than communicate with server via
|
* And than communicate with server via
|
||||||
* symmetric key
|
* symmetric key
|
||||||
|
|
||||||
|
to do
|
||||||
|
|
||||||
|
* interface for choosing server in client
|
||||||
23
server.py
23
server.py
|
|
@ -2,21 +2,17 @@ from email.policy import HTTP
|
||||||
from typing import Any, Coroutine
|
from typing import Any, Coroutine
|
||||||
from sanic import Sanic, Request, response
|
from sanic import Sanic, Request, response
|
||||||
from sanic.response import HTTPResponse
|
from sanic.response import HTTPResponse
|
||||||
|
from cryptography.fernet import Fernet
|
||||||
from sanic.server.websockets.impl import WebsocketImplProtocol
|
from sanic.server.websockets.impl import WebsocketImplProtocol
|
||||||
|
|
||||||
import rsa
|
import rsa
|
||||||
|
|
||||||
app = Sanic("app")
|
app = Sanic("app")
|
||||||
app.config.OAS = False
|
app.config.OAS = False
|
||||||
|
|
||||||
actual_messages = []
|
actual_messages = []
|
||||||
|
users = {}
|
||||||
(pubkey, privkey) = rsa.newkeys(512)
|
key = Fernet.generate_key()
|
||||||
|
|
||||||
with open("private.pem", "wb") as f:
|
|
||||||
f.write(privkey.save_pkcs1())
|
|
||||||
|
|
||||||
with open("public.pem", "wb") as f:
|
|
||||||
f.write(pubkey.save_pkcs1())
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/talk', methods=["GET", "POST"])
|
@app.route('/talk', methods=["GET", "POST"])
|
||||||
|
|
@ -27,9 +23,16 @@ async def talking(request: Request) -> HTTPResponse:
|
||||||
|
|
||||||
@app.route('/update', methods=["GET", "POST"])
|
@app.route('/update', methods=["GET", "POST"])
|
||||||
async def talking(request: Request) -> HTTPResponse:
|
async def talking(request: Request) -> HTTPResponse:
|
||||||
return response.json({"status": [rsa.encrypt(k.encode('utf8'), pubkey) for k in actual_messages]})
|
return response.json({"status": actual_messages, "users_in_chat": list(users.keys())})
|
||||||
|
|
||||||
|
|
||||||
@app.route('/get_key', methods=['GET', 'POST'])
|
@app.route('/get_key', methods=['GET', 'POST'])
|
||||||
async def get_key(request: Request) -> HTTPResponse:
|
async def get_key(request: Request) -> HTTPResponse:
|
||||||
return await response.file("public.pem", status=200)
|
|
||||||
|
pubkey = rsa.PublicKey.load_pkcs1(request.form['pubkey'][0])
|
||||||
|
data = rsa.encrypt(key, pubkey)
|
||||||
|
|
||||||
|
if request.ip not in users:
|
||||||
|
users[request.form['username'][0]] = key
|
||||||
|
|
||||||
|
return response.raw(data)
|
||||||
Loading…
Reference in New Issue
Block a user