from SyloraQ.module import runwithin,decode_base64,reverse_string,shiftinwin,replace,SQNode,boa,shiftinwin,Jbtc,Jctb,encode_base64,decode_base64,reverse_string
import os
import hashlib

class inviShade: # Obfuscated to enhance security and mitigate the risk of algorithm reverse engineering or data compromise.
    def encode(text):return runwithin(decode_base64(reverse_string(shiftinwin(-8381,SQNode("inviSenc")))),"DEcode.ec()",text)
    def decode(text):return runwithin(decode_base64(reverse_string(shiftinwin(-8381,SQNode("inviSenc")))),"DEcode.dc()",text)

def hex2byte(hex_str: str) -> bytes:return bytes.fromhex(hex_str)
def gf_mul(a, b):
    p = 0
    for _ in range(8):
        if b & 1:p ^= a
        hi_bit_set = a & 0x80
        a <<= 1
        a &= 0xFF
        if hi_bit_set:a ^= 0x1B
        b >>= 1
    return p
def gf_inv(a):
    if a == 0:
        return 0
    lm, hm = 1, 0
    low, high = a, 0x11B
    while low > 1:
        ratio = high // low
        nm = hm ^ gf_mul(lm, ratio)
        new = high ^ gf_mul(low, ratio)
        hm, lm = lm, nm
        high, low = low, new
    return lm & 0xFF
def rotate_left(byte, count):return ((byte << count) | (byte >> (8 - count))) & 0xFF
def affine_transform(byte):
    b = byte
    res = b ^ rotate_left(b, 1) ^ rotate_left(b, 2) ^ rotate_left(b, 3) ^ rotate_left(b, 4) ^ 0x63
    return res & 0xFF
def compute_inv_sbox(sbox):
    inv_sbox = [0]*256
    for i, val in enumerate(sbox):inv_sbox[val] = i
    return inv_sbox
SBOX = [0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,0x04, 0xc7, 0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xdf,0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16,];INV_SBOX = compute_inv_sbox(SBOX);RCON = [0x00,0x01, 0x02, 0x04, 0x08,0x10, 0x20, 0x40, 0x80,0x1B, 0x36,]
def xor_bytes(a, b):return bytes(x ^ y for x, y in zip(a, b))
def pad_pkcs7(data):
    pad_len = 16 - (len(data) % 16)
    return data + bytes([pad_len]*pad_len)
def unpad_pkcs7(data):
    pad_len = data[-1]
    if pad_len < 1 or pad_len > 16:raise ValueError("Invalid PKCS7 padding")
    if data[-pad_len:] != bytes([pad_len]*pad_len):raise ValueError("Invalid PKCS7 padding")
    return data[:-pad_len]
def sub_word(word):return bytes(SBOX[b] for b in word)
def rot_word(word):return word[1:] + word[:1]
def key_expansion(key):
    Nk = 4
    Nr = 10
    Nb = 4
    w = [key[i*4:(i+1)*4] for i in range(Nk)]
    for i in range(Nk, Nb*(Nr+1)):
        temp = w[i-1]
        if i % Nk == 0:temp = xor_bytes(sub_word(rot_word(temp)), bytes([RCON[i//Nk], 0,0,0]))
        w.append(xor_bytes(w[i-Nk], temp))
    return w
def add_round_key(state, round_key):return bytes(s ^ k for s,k in zip(state, round_key))
def sub_bytes(state):return bytes(SBOX[b] for b in state)
def inv_sub_bytes(state):return bytes(INV_SBOX[b] for b in state)
def shift_rows(state):
    new_state = bytearray(16)
    for r in range(4):
        for c in range(4):new_state[4*c + r] = state[4*((c + r) %4) + r]
    return bytes(new_state)
def inv_shift_rows(state):
    new_state = bytearray(16)
    for r in range(4):
        for c in range(4):new_state[4*c + r] = state[4*((c - r) %4) + r]
    return bytes(new_state)
def xtime(a):return ((a <<1) ^ 0x1B) & 0xFF if (a & 0x80) else (a << 1)
def mix_single_column(a):
    t = a[0] ^ a[1] ^ a[2] ^ a[3]
    u = a[0]
    a0 = a[0] ^ t ^ xtime(a[0] ^ a[1])
    a1 = a[1] ^ t ^ xtime(a[1] ^ a[2])
    a2 = a[2] ^ t ^ xtime(a[2] ^ a[3])
    a3 = a[3] ^ t ^ xtime(a[3] ^ u)
    return bytes([a0 & 0xFF, a1 & 0xFF, a2 & 0xFF, a3 & 0xFF])
def mix_columns(state):
    new_state = bytearray(16)
    for c in range(4):
        col = state[c*4:c*4+4]
        mixed = mix_single_column(col)
        new_state[c*4:c*4+4] = mixed
    return bytes(new_state)
def inv_mix_columns(state):
    new_state = bytearray(16)
    for c in range(4):
        a = list(state[c*4:c*4+4])
        u = gf_mul(a[0], 0x0e) ^ gf_mul(a[1], 0x0b) ^ gf_mul(a[2], 0x0d) ^ gf_mul(a[3], 0x09)
        v = gf_mul(a[0], 0x09) ^ gf_mul(a[1], 0x0e) ^ gf_mul(a[2], 0x0b) ^ gf_mul(a[3], 0x0d)
        w = gf_mul(a[0], 0x0d) ^ gf_mul(a[1], 0x09) ^ gf_mul(a[2], 0x0e) ^ gf_mul(a[3], 0x0b)
        x = gf_mul(a[0], 0x0b) ^ gf_mul(a[1], 0x0d) ^ gf_mul(a[2], 0x09) ^ gf_mul(a[3], 0x0e)
        new_state[c*4:c*4+4] = bytes([u,v,w,x])
    return bytes(new_state)
def encrypt_block(block, round_keys):
    state = block
    state = add_round_key(state, b''.join(round_keys[0:4]))
    for rnd in range(1, 10):
        state = sub_bytes(state)
        state = shift_rows(state)
        state = mix_columns(state)
        state = add_round_key(state, b''.join(round_keys[rnd*4:(rnd+1)*4]))
    state = sub_bytes(state)
    state = shift_rows(state)
    state = add_round_key(state, b''.join(round_keys[40:44]))
    return state
def decrypt_block(block, round_keys):
    state = block
    state = add_round_key(state, b''.join(round_keys[40:44]))
    for rnd in range(9, 0, -1):
        state = inv_shift_rows(state)
        state = inv_sub_bytes(state)
        state = add_round_key(state, b''.join(round_keys[rnd*4:(rnd+1)*4]))
        state = inv_mix_columns(state)
    state = inv_shift_rows(state)
    state = inv_sub_bytes(state)
    state = add_round_key(state, b''.join(round_keys[0:4]))
    return state
def aes_cbc_encrypt(plaintext, key, iv):
    if len(key) != 16:raise ValueError("Key must be 16 bytes")
    if len(iv) != 16:raise ValueError("IV must be 16 bytes")
    round_keys = key_expansion(key)
    plaintext = pad_pkcs7(plaintext)
    blocks = [plaintext[i:i+16] for i in range(0, len(plaintext), 16)]
    ciphertext = b""
    prev = iv
    for block in blocks:
        block = xor_bytes(block, prev)
        encrypted = encrypt_block(block, round_keys)
        ciphertext += encrypted
        prev = encrypted
    return ciphertext
def aes_cbc_decrypt(ciphertext, key, iv):
    if len(key) != 16:raise ValueError("Key must be 16 bytes")
    if len(iv) != 16:raise ValueError("IV must be 16 bytes")
    if len(ciphertext) % 16 != 0:raise ValueError("Ciphertext length must be multiple of 16")
    round_keys = key_expansion(key)
    blocks = [ciphertext[i:i+16] for i in range(0, len(ciphertext), 16)]
    plaintext = b""
    prev = iv
    for block in blocks:
        decrypted = decrypt_block(block, round_keys)
        plaintext_block = xor_bytes(decrypted, prev)
        plaintext += plaintext_block
        prev = block
    return unpad_pkcs7(plaintext)
class ZypherTrail:
    @staticmethod
    def encode(string, max_row=5):
        min_row=0
        height = max_row - min_row + 1
        width = len(string)
        grid = [[' ' for _ in range(width)] for _ in range(height)]
        row = min_row
        direction = 1
        for col, char in enumerate(string):
            grid[row][col] = char
            row += direction
            if row > max_row:
                row = max_row - 1
                direction = -1
            elif row < min_row:
                row = min_row + 1
                direction = 1
        return '\n'.join(''.join(row) for row in grid)
    @staticmethod
    def decode(encoded_str):
        grid = encoded_str.split('\n')
        height = len(grid)
        width = len(grid[0]) if height > 0 else 0
        row = 0
        direction = 1
        result = []
        for col in range(width):
            char = grid[row][col]
            result.append(char)
            row += direction
            if row >= height:
                row = height - 2
                direction = -1
            elif row < 0:
                row = 1
                direction = 1
        return ''.join(result)
class FinalLockerCipher:
    def __init__(self, key): 
        if isinstance(key, str):
            key = key.encode()
        self.key = key
    def _permute(self, byte, i):
        return (byte ^ self.key[i % len(self.key)] ^ (i * 149) % 256) % 256
    def encrypt(self, data: bytes) -> bytes:
        return bytes([self._permute(b, i) for i, b in enumerate(data)])
    def decrypt(self, data: bytes) -> bytes:
        return self.encrypt(data)

class Q1:
    @staticmethod
    def encrypt(string: str, key: bytes = b"0123456789abcdef"):
        iv = os.urandom(16)
        plaintext = string.encode()
        ct_bytes  = aes_cbc_encrypt(plaintext, key, iv)
        rotated_iv     = shiftinwin(len(str(key)), iv)
        rotated_iv_hex = rotated_iv.hex()
        payload = f"{ct_bytes.hex()}:::{rotated_iv_hex}"
        return ZypherTrail.encode(payload)

    @staticmethod
    def decrypt(encrypted: str, key: bytes = b"0123456789abcdef"):
        recovered = ZypherTrail.decode(encrypted)
        cipher_hex, iv_hex = recovered.split(":::", 1)
        ct_bytes = bytes.fromhex(cipher_hex)
        rotated_iv = bytes.fromhex(iv_hex)
        iv = shiftinwin(-len(str(key)), rotated_iv)
        return aes_cbc_decrypt(ct_bytes, key, iv)

class Q2:
    @staticmethod
    def encrypt(string:str,key:int=8381):return reverse_string(shiftinwin(key,encode_base64(ZypherTrail.encode(Jctb(string)))))
    @staticmethod
    def decyrpt(string:str,key):return Jbtc(ZypherTrail.decode(decode_base64(shiftinwin(-key,reverse_string(string)))))
class Q3:
    @staticmethod
    def encode(string:str,key:str):
        Flc = FinalLockerCipher(key)
        return Flc.encrypt(string.encode()).hex()
    @staticmethod
    def decode(string: str, key: str):
        Flc = FinalLockerCipher(key)
        return Flc.decrypt(hex2byte(string)).decode()
def derive_key_bytes(password: str, salt: bytes = b'some_salt_here', iterations: int = 100_000, key_len: int = 16) -> bytes:return hashlib.pbkdf2_hmac('sha256', password.encode(), salt, iterations, dklen=key_len)

class Quasar:
    def encrypt(plaintext, password_str="securekey", key_int=8381):salt = b'ujHUhuUHhhuH';key_bytes = derive_key_bytes(password_str, salt);layer3 = Q3.encode((plaintext), password_str);layer2 = Q2.encrypt(layer3, key_int);layer1 = Q1.encrypt(layer2, key_bytes);return layer1
    def decrypt(encrypted, password_str="securekey", key_int=8381):salt = b'ujHUhuUHhhuH';key_bytes = derive_key_bytes(password_str, salt);layer1 = Q1.decrypt(encrypted, key_bytes);layer2 = Q2.decyrpt(layer1, key_int);layer3 = Q3.decode(layer2, password_str);return layer3
