init
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import base64
|
||||
import hashlib
|
||||
import random
|
||||
from typing import Tuple
|
||||
|
||||
class StringEncryptor:
|
||||
def __init__(self, primary_key: bytes, secondary_key: bytes, salt: bytes):
|
||||
self.primary_key = primary_key
|
||||
self.secondary_key = secondary_key
|
||||
self.salt = salt
|
||||
|
||||
def hide_byte(self, b: int) -> str:
|
||||
"""
|
||||
Obfuscates a single byte as a random arithmetic or bitwise expression.
|
||||
Used in the string encryption code to hide key bytes.
|
||||
"""
|
||||
pattern = random.randint(1, 5)
|
||||
if pattern == 1:
|
||||
x1, x2, x3 = [random.randint(65, 90) for _ in range(3)]
|
||||
result = x1 ^ x2 ^ x3 ^ b
|
||||
return f"({x1}^{x2}^{x3}^{result})"
|
||||
elif pattern == 2:
|
||||
base = random.randint(65, 90)
|
||||
multiplier = random.randint(2, 4)
|
||||
adjusted = (base * multiplier - b)
|
||||
return f"(({base}*{multiplier}-{adjusted}))"
|
||||
elif pattern == 3:
|
||||
shift = random.randint(1, 3)
|
||||
modified = (b << shift) >> shift
|
||||
diff = b - modified
|
||||
return f"((({b << shift})>>{shift})+{diff})"
|
||||
elif pattern == 4:
|
||||
char1, char2 = random.sample(range(65, 91), 2)
|
||||
result = char1 + char2 - b
|
||||
return f"({char1}+{char2}-{result})"
|
||||
else:
|
||||
mod_base = random.randint(91, 100)
|
||||
result = b % mod_base
|
||||
factor = b // mod_base
|
||||
return f"({factor}*{mod_base}+{result})"
|
||||
|
||||
def encrypt_string(self, s: str) -> Tuple[str, str, str]:
|
||||
"""
|
||||
Encrypts a string with a multi-layer XOR scheme
|
||||
and base85-encodes the result. Returns:
|
||||
- encoded string,
|
||||
- the code snippet that re-creates the keys in Python,
|
||||
- the hex digest for the 'modifier'.
|
||||
"""
|
||||
data = s.encode('utf-8')
|
||||
modifier = hashlib.sha256(self.salt + data).digest()[:8]
|
||||
|
||||
layer1 = bytes(
|
||||
d ^ k ^ m
|
||||
for d, k, m in zip(
|
||||
data,
|
||||
self.primary_key * ((len(data) // len(self.primary_key)) + 1),
|
||||
modifier * ((len(data) // len(modifier)) + 1)
|
||||
)
|
||||
)
|
||||
|
||||
final_data = bytes(
|
||||
d ^ k
|
||||
for d, k in zip(
|
||||
layer1,
|
||||
self.secondary_key * ((len(layer1) // len(self.secondary_key)) + 1)
|
||||
)
|
||||
)
|
||||
|
||||
encoded = base64.b85encode(final_data).decode()
|
||||
|
||||
primary_key_code = f"bytes([{','.join(self.hide_byte(b) for b in self.primary_key)}])"
|
||||
secondary_key_code = f"bytes([{','.join(self.hide_byte(b) for b in self.secondary_key)}])"
|
||||
salt_code = f"bytes([{','.join(self.hide_byte(b) for b in self.salt)}])"
|
||||
|
||||
key_setup = (
|
||||
f"_pk = {primary_key_code}\n"
|
||||
f"_sk = {secondary_key_code}\n"
|
||||
f"_st = {salt_code}"
|
||||
)
|
||||
|
||||
return encoded, key_setup, modifier.hex()
|
||||
Reference in New Issue
Block a user