diff --git a/.gitignore b/.gitignore index 6bf4b5a..c28c2dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /__pycache__/ /i-try/ /venv/ - +test_rsa.py +.gitignore \ No newline at end of file diff --git a/client.py b/client.py index 7789cb8..952c331 100644 --- a/client.py +++ b/client.py @@ -1,6 +1,7 @@ -import requests import threading +import requests import time +import rsa import os class Client: @@ -10,16 +11,22 @@ class Client: self.port = 80 self.username = username - self.talk_url = f"http://{self.server}:{self.port}/talk" - self.info_url = f"http://{self.server}:{self.port}/update" + self.base_url = f"http://{self.server}:{self.port}" + + self.talk_url = f"{self.base_url}/talk" + self.info_url = f"{self.base_url}/update" + self.key_url = f"{self.base_url}/get_key" + + self.pubkey = None def send_info(self): while True: user_input = input("You're message: ") + message = f'{self.username}: {user_input}' requests.post(self.talk_url, data={ - "text": f'{self.username}: {user_input}' + "text": rsa.encrypt(message.encode('utf8'), self.pubkey) }) @@ -33,18 +40,45 @@ class Client: last_try = r.json() os.system("cls") for msg in last_try["status"]: - print(f"{msg}\n") + print(f"{rsa.decrypt(msg.encode(), self.seckey)}\n") + + + def _key_request(self) -> None: + + with requests.get(self.key_url) as r: + with open("public_rec.pem", 'wb') as f: + f.write(r.text.encode()) + + + def _remove_keys(self) -> None: + with open('public_rec.pem', 'wb') as f: + pass + + + def _validate_keys(self) -> None: + + self._key_request() + + with open('public_rec.pem', "rb") as f: + first_key = f.read() + self.pubkey = rsa.PublicKey.load_pkcs1(first_key) + self._remove_keys() + def __call__(self): + + self._validate_keys() + threads = [ threading.Thread(target=self.send_info), threading.Thread(target=self.update_info) ] + for th in threads: th.start() if __name__ == '__main__': - c = Client(input("Who are you? \t")) + c = Client("sneaky") # input("Who are you? \t") c() \ No newline at end of file diff --git a/readme.MD b/readme.MD index 622524f..7fe08e5 100644 --- a/readme.MD +++ b/readme.MD @@ -18,4 +18,14 @@ how to run clear client to do -* rsa \ No newline at end of file +* rsa + +potential crypting pipeline + +* Client making priv key +* Server making symmetric key +* Client sending public key to server +* Server crypting symmetric key and sending to client +* Client encrypting private key +* And than communicate with server via +* symmetric key \ No newline at end of file diff --git a/reqs.txt b/reqs.txt index f53e784..b70bae6 100644 --- a/reqs.txt +++ b/reqs.txt @@ -1,2 +1,3 @@ sanic -requests \ No newline at end of file +requests +rsa \ No newline at end of file diff --git a/server.py b/server.py index 7e975fd..d5f59c6 100644 --- a/server.py +++ b/server.py @@ -1,21 +1,35 @@ +from email.policy import HTTP from typing import Any, Coroutine from sanic import Sanic, Request, response from sanic.response import HTTPResponse from sanic.server.websockets.impl import WebsocketImplProtocol - +import rsa app = Sanic("app") app.config.OAS = False actual_messages = [] +(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()) + @app.route('/talk', methods=["GET", "POST"]) async def talking(request: Request) -> HTTPResponse: - actual_messages.append(request.form["text"]) + actual_messages.append(request.form["text"][0]) return response.json({"status": "ok"}) @app.route('/update', methods=["GET", "POST"]) async def talking(request: Request) -> HTTPResponse: - return response.json({"status": actual_messages}) \ No newline at end of file + return response.json({"status": [rsa.encrypt(k.encode('utf8'), pubkey) for k in actual_messages]}) + + +@app.route('/get_key', methods=['GET', 'POST']) +async def get_key(request: Request) -> HTTPResponse: + return await response.file("public.pem", status=200) \ No newline at end of file