This commit is contained in:
Maxime Vorwerk
2024-08-04 14:27:12 +02:00
parent 71574f1649
commit ead48393eb
7 changed files with 676 additions and 0 deletions

76
aes_abc/aes-abc-3.py Executable file
View File

@@ -0,0 +1,76 @@
#!python3
from Crypto.Cipher import AES
import os
import math
BLOCK_SIZE = 16
UMAX = int(math.pow(256, BLOCK_SIZE))
def remove_line(s):
# returns the header line, and the rest of the file
return s[:s.index(b'\n') + 1], s[s.index(b'\n')+1:]
def parse_header_ppm(f):
data = f.read()
header = b""
for i in range(3):
header_i, data = remove_line(data)
header += header_i
return header, data
def pad(pt):
padding = BLOCK_SIZE - len(pt) % BLOCK_SIZE
return pt + (bytes([padding]) * padding)
def aes_abc_encrypt(pt):
KEY = b"0123456789abcdef"
cipher = AES.new(KEY, AES.MODE_ECB)
ct = cipher.encrypt(pad(pt))
blocks = [ct[i * BLOCK_SIZE:(i+1) * BLOCK_SIZE] for i in range(len(ct) // BLOCK_SIZE)]
iv = b"0123456789abcdef"
blocks.insert(0, iv)
for i in range(len(blocks) - 1):
prev_blk = int(blocks[i].hex(), 16)
curr_blk = int(blocks[i+1].hex(), 16)
n_curr_blk = (prev_blk + curr_blk) % UMAX
blocks[i+1] = n_curr_blk.to_bytes(BLOCK_SIZE, 'big')
ct_abc = b"".join(blocks)
_blocks = [ct_abc[i * BLOCK_SIZE:(i+1) * BLOCK_SIZE] for i in range(len(ct_abc) // BLOCK_SIZE)]
assert len(blocks) == len(_blocks)
assert blocks == _blocks
for i in reversed(range(len(_blocks)-1)):
prev_blk = int.from_bytes(_blocks[i], 'big')
curr_blk = int.from_bytes(_blocks[i+1], 'big')
n_curr_blk = (curr_blk - prev_blk)%UMAX
_blocks[i+1] = n_curr_blk.to_bytes(BLOCK_SIZE, 'big')
_blocks = _blocks[1:]
assert len(blocks) == len(_blocks)+1
_ct = b"".join(_blocks)
assert len(_ct) == len(ct)
assert _ct == ct
return iv, ct_abc, ct
if __name__=="__main__":
with open('flag.ppm', 'rb') as f:
header, data = parse_header_ppm(f)
iv, c_img, ct = aes_abc_encrypt(data)
with open('test.enc.ppm', 'wb') as fw:
fw.write(header)
fw.write(c_img)

77
aes_abc/aes-abc.py Executable file
View File

@@ -0,0 +1,77 @@
#!python2.7
from Crypto.Cipher import AES
import os
import math
BLOCK_SIZE = 16
UMAX = int(math.pow(256, BLOCK_SIZE))
def to_bytes(n):
s = hex(n)
s_n = s[2:]
if 'L' in s_n:
s_n = s_n.replace('L', '')
if len(s_n) % 2 != 0:
s_n = '0' + s_n
decoded = s_n.decode('hex')
pad = (len(decoded) % BLOCK_SIZE)
if pad != 0:
decoded = "\0" * (BLOCK_SIZE - pad) + decoded
return decoded
def remove_line(s):
# returns the header line, and the rest of the file
return s[:s.index('\n') + 1], s[s.index('\n')+1:]
def parse_header_ppm(f):
data = f.read()
header = ""
for i in range(3):
header_i, data = remove_line(data)
header += header_i
return header, data
def pad(pt):
padding = BLOCK_SIZE - len(pt) % BLOCK_SIZE
return pt + (chr(padding) * padding)
def aes_abc_encrypt(pt):
KEY = "0123456789abcdef"
cipher = AES.new(KEY, AES.MODE_ECB)
ct = cipher.encrypt(pad(pt))
blocks = [ct[i * BLOCK_SIZE:(i+1) * BLOCK_SIZE] for i in range(len(ct) / BLOCK_SIZE)]
iv = "0123456789abcdef"
blocks.insert(0, iv)
for i in range(len(blocks) - 1):
prev_blk = int(blocks[i].encode('hex'), 16)
curr_blk = int(blocks[i+1].encode('hex'), 16)
n_curr_blk = (prev_blk + curr_blk) % UMAX
blocks[i+1] = to_bytes(n_curr_blk)
ct_abc = "".join(blocks)
return iv, ct_abc, ct
if __name__=="__main__":
with open('flag.ppm', 'rb') as f:
header, data = parse_header_ppm(f)
iv, c_img, ct = aes_abc_encrypt(data)
with open('test.enc.ppm', 'wb') as fw:
fw.write(header)
fw.write(c_img)

BIN
aes_abc/body.enc.ppm Executable file

Binary file not shown.

479
aes_abc/body.ppm Normal file

File diff suppressed because one or more lines are too long

4
aes_abc/flag.ppm Normal file

File diff suppressed because one or more lines are too long

40
aes_abc/sol.py Executable file
View File

@@ -0,0 +1,40 @@
#!python3
import math
BLOCK_SIZE = 16
UMAX = int(math.pow(256, BLOCK_SIZE))
def remove_line(s):
# returns the header line, and the rest of the file
return s[:s.index(b'\n') + 1], s[s.index(b'\n')+1:]
def parse_header_ppm(f):
data = f.read()
header = b""
for i in range(3):
header_i, data = remove_line(data)
header += header_i
return header, data
with open("body.enc.ppm", 'rb') as f:
header, data = parse_header_ppm(f)
blocks = [data[i * BLOCK_SIZE:(i+1) * BLOCK_SIZE] for i in range(len(data) // BLOCK_SIZE)]
for i in reversed(range(len(blocks) - 1)):
prev_blk = int.from_bytes(blocks[i], 'big')
curr_blk = int.from_bytes(blocks[i+1], 'big')
n_curr_blk = (curr_blk - prev_blk) % UMAX
blocks[i+1] = n_curr_blk.to_bytes(16, 'big')
blocks = blocks[1:]
with open("body.ppm", 'wb') as f:
f.write(header)
for block in blocks:
f.write(block)

BIN
aes_abc/test.enc.ppm Normal file

Binary file not shown.