Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
goelp14
GitHub Repository: goelp14/easyctf-iv-problems
Path: blob/master/special_endings/source/encode.py
650 views
1
import base64
2
3
flag = "011010010110110001101100010111110110110101101001011100110111001101011111011110010110111101110101"
4
5
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
6
7
outfile = open("encrypted_lines.txt", "w")
8
with open("quotes.txt") as f:
9
for line in f:
10
encoded = base64.standard_b64encode(bytes(line.rstrip()))
11
if (encoded.rstrip().endswith("==") and flag != ""):
12
shift = flag[:4]
13
flag = flag[4:]
14
charToChange = encoded[-3:-2]
15
offset = int(shift, 2)
16
newChar = (alphabet[alphabet.find(charToChange[:1]) + offset])
17
encoded = encoded[:-3] + newChar + "=="
18
elif (encoded.rstrip().endswith("=") and flag != ""):
19
shift = flag[:2]
20
flag = flag[2:]
21
charToChange = encoded[-2:-1]
22
offset = int(shift, 2)
23
newChar = (alphabet[alphabet.find(charToChange[:1]) + offset])
24
encoded = encoded[:-2] + newChar + "="
25
outfile.write(encoded + "\n")
26
outfile.close()
27
28