Skip to content

Instantly share code, notes, and snippets.

@Frontear
Last active October 26, 2021 17:42
Show Gist options
  • Save Frontear/45d2fa3da769841ae92d3fc2a0c29e3b to your computer and use it in GitHub Desktop.
Save Frontear/45d2fa3da769841ae92d3fc2a0c29e3b to your computer and use it in GitHub Desktop.
A simple string obfuscation/deobfuscation python program. This was used as a non-secure, educational use only "encryption" for a school assignment.
import zlib
import base64
import hashlib
SEPARATOR = "\0"
ENCODING = "UTF-8"
HASHING = hashlib.blake2b
def encrypt(data: str, key: str) -> bytes:
arr = []
data = list(data)
key_hash = HASHING(key.encode(ENCODING)).hexdigest()
arr.append(key_hash)
for i in range(len(data)):
char, shift = ord(data[i]), ord(key[i % len(key)])
enc = hex(char + shift)
arr.append(enc)
return base64.standard_b64encode(zlib.compress(SEPARATOR.join(arr).encode(ENCODING)))
def decrypt(data: bytes, key: str) -> str:
arr = []
data = zlib.decompress(base64.standard_b64decode(data)).decode(ENCODING).split(SEPARATOR)
key_hash = HASHING(key.encode(ENCODING)).hexdigest()
if key_hash == data[0]:
for i in range(len(data := data[1:])):
char, shift = int(data[i], 16), ord(key[i % len(key)])
dec = chr(char - shift)
arr.append(dec)
else:
raise InterruptedError("Given password was incorrect. Cannot decrypt")
return "".join(arr)
if __name__ == "__main__":
import string
data = string.printable
password = "ali4588"
with open("test.enc", "wb+") as f:
f.write(a := encrypt(data, password))
print(a)
f.seek(0)
print(d := decrypt(f.read(), password)); assert d == data
# len(b) > len(c)
print(a := encrypt(b := "a ", c := "a"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := "a ", c := "1"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := "1 ", c := "a"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := "1 ", c := "1"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := "a1", c := "a"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := "a1", c := "1"), d := decrypt(a, c)); assert b == d
#len(b) == len(c)
print(a := encrypt(b := "a ", c := "a1"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := "a ", c := "1a"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := "1 ", c := "a1"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := "1 ", c := "1a"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := "a1", c := "a1"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := "a1", c := "1a"), d := decrypt(a, c)); assert b == d
#len(b) < len(c)
print(a := encrypt(b := " ", c := "a1"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := " ", c := "1a"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := "a", c := "a1"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := "a", c := "1a"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := "1", c := "a1"), d := decrypt(a, c)); assert b == d
print(a := encrypt(b := "1", c := "1a"), d := decrypt(a, c)); assert b == d
@Frontear
Copy link
Author

Decryption no longer silently fails, instead Errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment