-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcreate_examples.py
executable file
·115 lines (86 loc) · 4.59 KB
/
create_examples.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
# create markdown page if examples for kids_crypto
# has the side effect of testing nearly everything, albeit not in a pythonish way
# intentionally call python not via function but shell to get the command lines examples right
import os
import argparse
# techniques used via --technique param of crypto_puzzles.py (also determines the order they appear in the resulting page)
techniques='exEUflWuSCc1A3MNQw'
# unused for various reasons: techniques='Lmnj'
# technique 's' doesn't work here, 'm' boring
max_grade=12
cp_path='./crypto_puzzles.py'
#plaintext = 'Top secret!'
plaintext = 'Top secret! The sweets are hidden under your chair!'
def escape_markdown(md):
md=md.replace('|', '\|')
return md
parser = argparse.ArgumentParser()
parser.add_argument("--create_function_list", "-c", action='store_true', help="Create list of all functions and distinct grades, which give different results")
parser.add_argument("--markdown", "-m", action='store_true', help="Create markdown instead of HTML (don't use, sucks for long text in table cells)")
parser.add_argument("--debug", "-d", action='store_true', help="Debug")
args = parser.parse_args()
create_function_list = args.create_function_list
debug = args.debug
markdown = args.markdown
if not create_function_list:
if markdown:
print('Python function | Minimum school grade | Encrypted output | Command line')
print('--- | --- | --- | ---')
else:
print('<html><head><title>Crypto Puzzles examples</title></head><body><font face="arial,helvetica,sans-serif"><h1>Crypto Puzzles examples</h1>All examples use the plaintext: "' + plaintext + '" <br>Created using https://github.com/2d4d/crypto_puzzles/')
print('<table border=1 style=\'font-family:"Arial"; font-size:70%\'><tr><th>Crypto function <th> Minimum school grade <th width=40%> Encrypted output <th> Command line')
last_res=""
for technique in techniques:
for grade in range(1,max_grade+1):
# stego_saurus can only hide 3 digits
if technique == 'u':
cmd_text = "698"
else:
cmd_text = plaintext
cmd = cp_path + ' -T ' + technique + ' --grade ' + str(grade) + ' "' + cmd_text + '"'
if technique == 'Q':
# picked grades with different results by hand:
if grade not in [1, 2, 4, 6, 10, 12]:
continue
else:
filename = "qr_in_qr_grade_" + str(grade) + ".png"
cmd += " --filename " + filename
elif 'filename' in locals():
del filename
cmd_real = cmd + ' --show_function_name --seed 1'
cmd = cmd.replace('./', '')
if debug:
print("cmd for markdown: " + cmd)
print("cmd as executed: " + cmd_real)
# execute crypto_puzzles.py in external process
res = os.popen(cmd_real).readlines()
if 'filename' in locals():
os.rename(filename, './examples/' + filename)
function_name = res.pop(0)
ciphertext = ''.join(res)
if 'figlet' in function_name or 'stego_saurus' in function_name or 'crackme' in function_name:
ciphertext = "<pre>\n" + ciphertext + "</pre>\n"
if 'stego_saurus' in function_name:
ciphertext += "The stegosaurus can only hide 3 characters/digits."
if 'qr_inside_qr' in function_name:
filename = ciphertext
ciphertext = "<img src='" + filename + "'><br>QR inside QR: Scan to see the different hints per grades."
if grade == 1:
ciphertext += " Grade 1 can be scanned directly because of the border around the inner QR."
if markdown:
ciphertext = escape_markdown(ciphertext.rstrip())
# put hard spaces in here because otherwise they're not properly visible in the rendered HTML
if 'emoji' in function_name:
ciphertext = ciphertext.replace(' ', ' ')
if res != last_res:
if not create_function_list:
if markdown:
print('|'.join((function_name.rstrip(), str(grade), ciphertext, cmd)) )
else:
print('<tr><td>' + function_name.rstrip() + '()<td style="text-align:center">' + str(grade) + '<td>' + ciphertext + '<td>' + cmd)
else:
print(function_name.rstrip(), str(grade))
last_res=res
# TODO: anderer plaintext
print('<tr><td>Euli: black_on_black<td style="text-align:center">1<td bgcolor=black>This very secure encryption is so far only available in .docx files generated by Euli because it depends on file formats. <td>Check out Euli')