mirror of
https://github.com/SantaSpeen/winConnect.git
synced 2026-02-16 10:30:44 +00:00
[+] WinConnectCryptoBase (and WinConnectCryptoNone)
[+] WinConnectCryptoSimple
This commit is contained in:
14
winConnect/crypto/crypto_class_base.py
Normal file
14
winConnect/crypto/crypto_class_base.py
Normal file
@@ -0,0 +1,14 @@
|
||||
class WinConnectCryptoBase:
|
||||
|
||||
def encrypt(self, data: bytes) -> bytes: ...
|
||||
def decrypt(self, data: bytes) -> bytes: ...
|
||||
|
||||
def test(self, test_bytes: bytes = b"test_string") -> bool:
|
||||
encrypted = self.encrypt(test_bytes)
|
||||
decrypted = self.decrypt(encrypted)
|
||||
if decrypted != test_bytes:
|
||||
return False
|
||||
return True
|
||||
|
||||
def load(self) -> None: ...
|
||||
def unload(self) -> None: ...
|
||||
42
winConnect/crypto/crypto_classes.py
Normal file
42
winConnect/crypto/crypto_classes.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import random
|
||||
|
||||
from .crypto_class_base import WinConnectCryptoBase
|
||||
from winConnect.exceptions import WinConnectCryptoSimpleBadHeaderException
|
||||
|
||||
|
||||
class WinConnectCryptoNone(WinConnectCryptoBase):
|
||||
def __init__(self): ...
|
||||
def encrypt(self, data: bytes) -> bytes:
|
||||
return data
|
||||
def decrypt(self, data: bytes) -> bytes:
|
||||
return data
|
||||
|
||||
class WinConnectCryptoSimple(WinConnectCryptoBase):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def encrypt(self, data: bytes) -> bytes:
|
||||
shift_key = random.randint(100, 749)
|
||||
key = random.randint(5, 250)
|
||||
encrypted_text = bytearray()
|
||||
header = f"wccs{shift_key}{key+shift_key}:"
|
||||
for char in header:
|
||||
encrypted_text.append(ord(char))
|
||||
for char in data:
|
||||
encrypted_text.append(char ^ key)
|
||||
return bytes(encrypted_text)
|
||||
|
||||
def decrypt(self, data: bytes) -> bytes:
|
||||
try:
|
||||
header, content = data.split(b":", 1)
|
||||
if header[:4] != b"wccs":
|
||||
raise WinConnectCryptoSimpleBadHeaderException("Bad header in message.")
|
||||
except ValueError:
|
||||
raise WinConnectCryptoSimpleBadHeaderException("No header in message.")
|
||||
shift_key = int(header[4:7])
|
||||
key = int(header[7:]) - shift_key
|
||||
decrypted_text = bytearray()
|
||||
for char in content:
|
||||
decrypted_text.append(char ^ key)
|
||||
return bytes(decrypted_text)
|
||||
|
||||
Reference in New Issue
Block a user