File size: 1,316 Bytes
19ecc0d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import base64
import json
def encrypt_payload(data_dict, key):
"""Encrypts the dictionary payload using a custom XOR cipher with shifting."""
plain_text = json.dumps(data_dict)
key_bytes = key.encode('utf-8')
plain_bytes = plain_text.encode('utf-8')
encrypted = bytearray()
for i, b in enumerate(plain_bytes):
# Custom cipher: XOR with key and apply dynamic shifting
k_byte = key_bytes[i % len(key_bytes)]
shift = (i * 3) % 256
val = b ^ k_byte ^ shift
encrypted.append(val)
return base64.b64encode(encrypted).decode('utf-8')
def decrypt_payload(ciphertext, key):
"""Decrypts the base64-encoded ciphertext using the key."""
encrypted_bytes = base64.b64decode(ciphertext)
key_bytes = key.encode('utf-8')
decrypted = bytearray()
for i, b in enumerate(encrypted_bytes):
k_byte = key_bytes[i % len(key_bytes)]
shift = (i * 3) % 256
val = b ^ k_byte ^ shift
decrypted.append(val)
return json.loads(decrypted.decode('utf-8'))
def obfuscate_key(key):
"""Obfuscates the cipher key for safe transmission in HTML templates."""
reversed_key = key[::-1]
return base64.b64encode(reversed_key.encode('utf-8')).decode('utf-8')
|