43 lines
1.5 KiB
Python
43 lines
1.5 KiB
Python
|
import asyncio
|
||
|
import websockets
|
||
|
import base64
|
||
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||
|
from cryptography.hazmat.backends import default_backend
|
||
|
import json
|
||
|
|
||
|
# Конфигурация шифрования
|
||
|
KEY = b'8888888888888888'
|
||
|
IV_LENGTH = 16
|
||
|
|
||
|
async def decrypt_message(encrypted_message):
|
||
|
# Декодируем base64
|
||
|
encrypted_data = base64.b64decode(encrypted_message)
|
||
|
|
||
|
# Извлекаем IV и зашифрованный текст
|
||
|
iv = encrypted_data[:IV_LENGTH]
|
||
|
cipher_text = encrypted_data[IV_LENGTH:]
|
||
|
|
||
|
# Создаем объект Cipher для расшифровки
|
||
|
cipher = Cipher(algorithms.AES(KEY), modes.CBC(iv), backend=default_backend())
|
||
|
decryptor = cipher.decryptor()
|
||
|
|
||
|
# Расшифровываем данные
|
||
|
decrypted_data = decryptor.update(cipher_text) + decryptor.finalize()
|
||
|
|
||
|
# Удаляем PKCS5Padding
|
||
|
padding_length = decrypted_data[-1]
|
||
|
decrypted_data = decrypted_data[:-padding_length]
|
||
|
|
||
|
return decrypted_data.decode('utf-8')
|
||
|
|
||
|
async def listen():
|
||
|
uri = "ws://45.67.56.214:8088/users"
|
||
|
async with websockets.connect(uri) as websocket:
|
||
|
while True:
|
||
|
message = await websocket.recv()
|
||
|
encrypted_messages = json.loads(message)
|
||
|
for encrypted_message in encrypted_messages:
|
||
|
decrypted_message = await decrypt_message(encrypted_message)
|
||
|
print(decrypted_message)
|
||
|
|
||
|
asyncio.get_event_loop().run_until_complete(listen())
|