what the hell
This commit is contained in:
1 parent
187dfdda9f
commit
1804b94cf2
3 files changed
+132
-17
No files matched your search
@@ -0,0 +1,74 @@
|
||||
# this one tries to restore the og thing from the key UHuhUHuhuhuhUHuhUHuhh.
|
||||
|
||||
import operator as op
|
||||
from typing import Literal
|
||||
from main import parse_bytecrypt
|
||||
import ast
|
||||
from collections.abc import Iterator
|
||||
|
||||
with open("./real/real.txt", "rb") as f:
|
||||
LENBIG = len(f.read())
|
||||
|
||||
|
||||
def restore_text1(target: bytes, cuh=Iterator[Iterator[tuple[int, int]]]):
|
||||
textin: list[int | None] = []
|
||||
for key in cuh: # stfu # type: ignore
|
||||
if len(key) != len(target):
|
||||
# raise "actually does happen error"
|
||||
raise ValueError(
|
||||
f"""Key Error: your target or your key is stupid
|
||||
{len(key)=}, {len(target)=}"""
|
||||
)
|
||||
|
||||
assert len(key[0]) == 2 # "input error"
|
||||
|
||||
for target_index, (in_index, dif) in enumerate(key):
|
||||
# print(target_index, in_index, dif)
|
||||
if len(textin) <= in_index:
|
||||
textin.extend([None] * (in_index + 1 - len(textin)))
|
||||
assert len(textin) > in_index
|
||||
textinindex = (target[target_index] - dif) % 256
|
||||
if in_index >= LENBIG:
|
||||
# raise neverhappenserror no actually its user input problem
|
||||
raise IndexError(f"{in_index} is big, big as {LENBIG} or more")
|
||||
if textin[in_index] is not None:
|
||||
assert op.eq(textin[in_index], textinindex)
|
||||
else:
|
||||
textin[in_index] = textinindex
|
||||
return textin
|
||||
|
||||
|
||||
def getkey(filename, mode: Literal["r", "rb"]):
|
||||
match mode:
|
||||
case "r":
|
||||
with open(filename, mode, encoding="utf-8") as f:
|
||||
return ast.literal_eval(f.read())
|
||||
case "rb":
|
||||
with open(filename, mode) as f:
|
||||
return list(parse_bytecrypt(f.read()))
|
||||
|
||||
# key1 = getkey("./real/keyreal not valid.txt", "r")
|
||||
# key2 = getkey("./real/keyREAL2 invalid.txt", "r")
|
||||
# key3 = getkey("./real/keyreal3 invalid.txt", "r")
|
||||
# key4 = getkey("./real/RealKeyBig4 invalid.bin", "rb")
|
||||
|
||||
|
||||
with open("./real/not_real.txt", "rb") as textin:
|
||||
|
||||
key5 = getkey("./real/keyreal5.txt", "r")
|
||||
key6 = getkey("./real/realkey6.txt", "r")
|
||||
key7 = getkey("./real/realkey7.bin", "rb")
|
||||
print(list(map(len, [key5, key6, key7]))) # [141, 141, 141]
|
||||
# print(key5)
|
||||
content = textin.read()
|
||||
print(len(content)) # 141 now not 145 anymore
|
||||
print(
|
||||
bytes(
|
||||
map(
|
||||
lambda a: a or ord("Q"),
|
||||
restore_text1(content, [key5, key6, key7]), # type: ignore
|
||||
)
|
||||
)
|
||||
) # i love when Q is NEVER used
|
||||
|
||||
# output: redacted LAMO you cant get this from two outdated files
|
||||
@@ -4,6 +4,8 @@ Proof-of-concept random key-based text transformer.
|
||||
|
||||
## Usage
|
||||
|
||||
you can use -o \<FILE> to write to file.
|
||||
|
||||
### Encrypt
|
||||
|
||||
```sh
|
||||
@@ -57,4 +59,4 @@ python main.py decrypt filein.txt keyfile.bin --bytes=4 > out.txt
|
||||
|
||||
---
|
||||
|
||||
Version 1.1
|
||||
Version 1.2
|
||||
@@ -46,30 +46,41 @@ def index_list(range_: range, length: int, seed=None) -> Iterator[int]:
|
||||
Generate a sequence of random indices from the given range, of specified length.
|
||||
If a seed is provided, the random sequence is reproducible.
|
||||
Yields:
|
||||
random_index: int: Randomly chosen index from the range.
|
||||
random_index (int): Randomly chosen index from the range.
|
||||
"""
|
||||
if seed is not None:
|
||||
random.seed(seed)
|
||||
return (random.choice(range_) for _ in repeat(None, length))
|
||||
|
||||
|
||||
def funky_function(text_in: str, text_out: str, seed=None) -> Iterator[tuple[int, int]]:
|
||||
def funky_function(
|
||||
text_in: bytes, text_out: bytes, seed=None
|
||||
) -> Iterator[tuple[int, int]]:
|
||||
"""
|
||||
For each character in text_out, randomly select a character from text_in (using index_list),
|
||||
and yield a tuple (index_in, offset) where offset is the difference in ordinal values.
|
||||
This forms the 'key' for transforming text_in to text_out.
|
||||
Args:
|
||||
text_in (bytes):
|
||||
the text file, in bytes
|
||||
text_out (bytes):
|
||||
uhhhh
|
||||
seed:
|
||||
whatever can be put in random.seed()
|
||||
Yields:
|
||||
(index_in, offset): Tuple[int, int]
|
||||
(index_in, offset) (int, int):
|
||||
idk how to explain this one
|
||||
|
||||
"""
|
||||
range_text_in = range(len(text_in))
|
||||
len_text_out = len(text_out)
|
||||
for id_out, id_in in enumerate(index_list(range_text_in, len_text_out, seed)):
|
||||
val_in = text_in[id_in]
|
||||
val_out = text_out[id_out]
|
||||
yield id_in, (ord(val_out) - ord(val_in)) % 256
|
||||
yield id_in, (val_out - val_in) % 256
|
||||
|
||||
|
||||
def rebuild(text_in, this: Iterator[tuple[int, int]]):
|
||||
def rebuild(text_in: bytes, this: Iterator[tuple[int, int]]) -> Iterator[int]:
|
||||
"""
|
||||
Rebuilds the output text by applying the offset to the character at index_in in text_in
|
||||
for each (index_in, offset) in the key.
|
||||
@@ -78,10 +89,13 @@ def rebuild(text_in, this: Iterator[tuple[int, int]]):
|
||||
"""
|
||||
for ind, offset in this:
|
||||
val_in = text_in[ind]
|
||||
yield chr((ord(val_in) + offset) % 256)
|
||||
yield (val_in + offset) % 256
|
||||
|
||||
|
||||
def keycrypt(genkey: Iterator[tuple[int, int]], usize_length=8) -> bytearray:
|
||||
"""
|
||||
This thing transform the output from the `funky_function` to a byte array.
|
||||
"""
|
||||
i = bytearray()
|
||||
for index, dif in genkey:
|
||||
i.extend(
|
||||
@@ -91,6 +105,10 @@ def keycrypt(genkey: Iterator[tuple[int, int]], usize_length=8) -> bytearray:
|
||||
|
||||
|
||||
def parse_bytecrypt(data: bytes, usize_length=8) -> Iterator[tuple[int, int]]:
|
||||
"""
|
||||
this transform the `keycrypt`ed output back to the `genkey`.
|
||||
should work probably i think.
|
||||
"""
|
||||
for i in range(0, len(data), usize_length + 1):
|
||||
index = int.from_bytes(data[i : i + usize_length])
|
||||
offset = data[i + usize_length]
|
||||
@@ -100,9 +118,12 @@ def parse_bytecrypt(data: bytes, usize_length=8) -> Iterator[tuple[int, int]]:
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
# description="asscryption: Random key-based text transformer (PoC). See top of file for usage.",
|
||||
usage=__doc__,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter, # what is ts
|
||||
description=__doc__,
|
||||
# usage=__doc__,
|
||||
epilog="by lda (and copilot)",
|
||||
)
|
||||
|
||||
subparsers = parser.add_subparsers(dest="command", required=True)
|
||||
|
||||
enc = subparsers.add_parser("encrypt")
|
||||
@@ -115,7 +136,7 @@ def main():
|
||||
nargs="?",
|
||||
const=8,
|
||||
type=int,
|
||||
help="Output key as bytes to stdout (optionally specify index size in bytes, default 8)",
|
||||
help="Output key as bytes to stdout / file (optionally specify index size in bytes, default 8)",
|
||||
)
|
||||
|
||||
dec = subparsers.add_parser("decrypt")
|
||||
@@ -130,21 +151,33 @@ def main():
|
||||
help="Read key as bytes (optionally specify index size in bytes, default 8)",
|
||||
)
|
||||
|
||||
dec.add_argument("-o", "--output", help="Output file (default: stdout)")
|
||||
enc.add_argument("-o", "--output", help="Output file (default: stdout)")
|
||||
# parser.add_argument(
|
||||
# "-o", "--output",
|
||||
# help="Output file (default: stdout)"
|
||||
# )
|
||||
args = parser.parse_args()
|
||||
|
||||
match args.command:
|
||||
case "encrypt":
|
||||
with open(args.filein, "r", encoding="utf-8") as fin:
|
||||
with open(args.filein, "rb") as fin:
|
||||
text_in = fin.read()
|
||||
with open(args.target, "r", encoding="utf-8") as fout:
|
||||
with open(args.target, "rb") as fout:
|
||||
text_out = fout.read()
|
||||
key = funky_function(text_in, text_out, args.seed)
|
||||
if args.bytes:
|
||||
sys.stdout.buffer.write(keycrypt(key))
|
||||
|
||||
# Handle output
|
||||
output_data = (
|
||||
keycrypt(key, args.bytes) if args.bytes else str(list(key)).encode()
|
||||
)
|
||||
if args.output:
|
||||
with open(args.output, "wb") as outf:
|
||||
outf.write(output_data)
|
||||
else:
|
||||
print(list(key))
|
||||
sys.stdout.buffer.write(output_data)
|
||||
case "decrypt":
|
||||
with open(args.filein, "r", encoding="utf-8") as fin:
|
||||
with open(args.filein, "rb") as fin:
|
||||
text_in = fin.read()
|
||||
if args.bytes:
|
||||
with open(args.keyfile, "rb") as kf:
|
||||
@@ -152,8 +185,14 @@ def main():
|
||||
else:
|
||||
with open(args.keyfile, "r", encoding="utf-8") as kf:
|
||||
key = ast.literal_eval(kf.read())
|
||||
result = "".join(rebuild(text_in, iter(key)))
|
||||
print(result)
|
||||
result = bytes(rebuild(text_in, iter(key)))
|
||||
|
||||
# Handle output
|
||||
if args.output:
|
||||
with open(args.output, "wb") as outf:
|
||||
outf.write(result)
|
||||
else:
|
||||
sys.stdout.buffer.write(result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user