-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3_bean_counter.py
47 lines (39 loc) · 990 Bytes
/
3_bean_counter.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
import requests
import json
import binascii
from PIL import Image
url = 'http://aes.cryptohack.org/bean_counter/encrypt/'
def xor_hex(a, b):
temp = int(a, 16) ^ int(b, 16)
res = hex(temp)[2:].zfill(2)
return res
def encrypted():
r = requests.get(url)
enc = r.json()['encrypted']
return enc
def decrypted(encm, key):
dec = ''
t = 0
for i in range(0, len(encm), 2):
temp =xor_hex(encm[i:i+2], key[t:t+2])
t += 2
if (t >= len(key)):
t = 0
dec += temp
d = bytes.fromhex(dec)
with open('Tugas6/beancounter.png', 'wb') as f:
f.write(d)
enc = encrypted()
encpic = binascii.unhexlify(enc)
pngheader = '89504e470d0a1a0a0000000d49484452'
# first 11 bytes of key
print(len(enc), len(enc) % 32)
key1 = enc[:32]
key = ''
for i in range(0, len(key1), 2):
temp = xor_hex(key1[i:i+2], pngheader[i:i+2])
key += temp
# print(key)
decrypted(enc, key)
im = Image.open(r"Tugas6/beancounter.png")
im.show()