Crypting: Done crypting pipeline and add some crypting code

This commit is contained in:
mirai 2022-07-02 08:30:37 +03:00
parent 68831c016f
commit de24ac3b11
5 changed files with 72 additions and 12 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
/__pycache__/ /__pycache__/
/i-try/ /i-try/
/venv/ /venv/
test_rsa.py
.gitignore

View File

@ -1,6 +1,7 @@
import requests
import threading import threading
import requests
import time import time
import rsa
import os import os
class Client: class Client:
@ -10,16 +11,22 @@ class Client:
self.port = 80 self.port = 80
self.username = username self.username = username
self.talk_url = f"http://{self.server}:{self.port}/talk" self.base_url = f"http://{self.server}:{self.port}"
self.info_url = f"http://{self.server}:{self.port}/update"
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): def send_info(self):
while True: while True:
user_input = input("You're message: ") user_input = input("You're message: ")
message = f'{self.username}: {user_input}'
requests.post(self.talk_url, data={ 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() last_try = r.json()
os.system("cls") os.system("cls")
for msg in last_try["status"]: 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): def __call__(self):
self._validate_keys()
threads = [ threads = [
threading.Thread(target=self.send_info), threading.Thread(target=self.send_info),
threading.Thread(target=self.update_info) threading.Thread(target=self.update_info)
] ]
for th in threads: for th in threads:
th.start() th.start()
if __name__ == '__main__': if __name__ == '__main__':
c = Client(input("Who are you? \t")) c = Client("sneaky") # input("Who are you? \t")
c() c()

View File

@ -18,4 +18,14 @@ how to run clear client
to do to do
* rsa * 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

View File

@ -1,2 +1,3 @@
sanic sanic
requests requests
rsa

View File

@ -1,21 +1,35 @@
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 sanic.server.websockets.impl import WebsocketImplProtocol from sanic.server.websockets.impl import WebsocketImplProtocol
import rsa
app = Sanic("app") app = Sanic("app")
app.config.OAS = False app.config.OAS = False
actual_messages = [] 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"]) @app.route('/talk', methods=["GET", "POST"])
async def talking(request: Request) -> HTTPResponse: async def talking(request: Request) -> HTTPResponse:
actual_messages.append(request.form["text"]) actual_messages.append(request.form["text"][0])
return response.json({"status": "ok"}) return response.json({"status": "ok"})
@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": actual_messages}) 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)