-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenc.py
executable file
·81 lines (61 loc) · 1.88 KB
/
enc.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import dataclasses
import re
import secrets
import sys
from pyrage import passphrase # type: ignore
args = sys.argv[1:]
def make_password(num_words):
text = open("amontillado.txt").read()
words = list(set(re.sub("[^a-z]", " ", text.lower()).split()))
return "".join(secrets.choice(words) for _ in range(num_words))
@dataclasses.dataclass
class Node:
letter: str
id: int
@dataclasses.dataclass
class Edge:
a: Node
b: Node
@dataclasses.dataclass
class Graph:
nodes: list[Node]
edges: list[Edge]
class IdGen:
def __init__(self):
self.ids = set()
def generate_id(self):
while True:
new_id = secrets.randbelow(1024**3)
if new_id not in self.ids:
self.ids.add(new_id)
return new_id
def make_hint(password):
graph = Graph([], [])
idg = IdGen()
random = secrets.SystemRandom()
graph.nodes = [Node(letter, idg.generate_id()) for letter in password]
edgelst = list(zip(graph.nodes, graph.nodes[1:]))
for _ in range(len(password) * 3):
edgelst.append(tuple(random.sample(graph.nodes, 2))) # type: ignore
graph.edges = [
Edge(b, a) if random.random() % 2 else Edge(a, b) for a, b in edgelst
]
random.shuffle(graph.nodes)
random.shuffle(graph.edges)
return graph
def encrypt(num_words, secret):
password = make_password(num_words)
print(password + f" {len(password)}")
graph = make_hint(password)
with open("hint.txt", "w") as f:
f.write(
"\n".join([f"{n.id} [{n.letter}];" for n in graph.nodes])
+ "\n"
+ "\n".join([f"{e.a.id} -- {e.b.id};" for e in graph.edges])
)
with open("secret.txt", "wb") as f:
f.write(passphrase.encrypt(secret.encode(), password))
def main():
encrypt(int(args[0]), open("flag.txt").read())
if __name__ == "__main__":
main()