Skip to content

Commit 443b2a9

Browse files
committed
Cleanup and bring common code to utils
1 parent 839f98b commit 443b2a9

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

Cryptopal-Challenges/brice/python/Challenge10.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
with open("../data/10.txt") as fp:
88
CIPHERTEXT = base64.b64decode(fp.read())
99

10-
PLAINTEXT = utils.CBC_AES_decrypt(CIPHERTEXT, KEY, IV)
11-
ENCRYPTED = utils.CBC_AES_encrypt(PLAINTEXT, KEY, IV)
10+
PLAINTEXT = utils.AES_CBC_decrypt(KEY, CIPHERTEXT, IV)
11+
ENCRYPTED = utils.AES_CBC_encrypt(KEY, PLAINTEXT, IV)
1212

1313
assert CIPHERTEXT == ENCRYPTED
1414

Cryptopal-Challenges/brice/python/Challenge8.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@
33

44
from utils import *
55

6-
def ECB_score(text):
7-
return vignereScore(bytearray(text), 16)
8-
9-
def repeated_blocks(text, blocksize):
10-
count = defaultdict(int)
11-
for block in chunkify(text, blocksize):
12-
count[block] += 1
13-
return max(count.items(), key=lambda kv: kv[1])[1]
14-
15-
16-
176
if __name__ == "__main__":
187
with open("../data/8.txt") as fp:
198
lines = fp.readlines()

Cryptopal-Challenges/brice/python/utils.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import base64
5+
import random
56
from collections import defaultdict
67
import pprint
78

@@ -141,35 +142,62 @@ def vignere(ciphertext, key):
141142
plaintext = bytearray([a^b for a,b in zip(ciphertext, longkey)])
142143
return str(plaintext)
143144

144-
def AES_ECB_encrypt(key, plaintext):
145+
def AES_ECB_encrypt(key, plaintext, iv=None):
145146
backend = default_backend()
146147
cipher = Cipher(algorithms.AES(bytes(key)), modes.ECB(), backend=backend)
147148
encryptor = cipher.encryptor()
148149
ct = encryptor.update(bytes(plaintext)) + encryptor.finalize()
149150
return ct
150151

151-
def AES_ECB_decrypt(key, ciphertext):
152+
def AES_ECB_decrypt(key, ciphertext, iv=None):
152153
backend = default_backend()
153154
cipher = Cipher(algorithms.AES(bytes(key)), modes.ECB(), backend=backend)
154155
decryptor = cipher.decryptor()
155156
pt = decryptor.update(ciphertext) + decryptor.finalize()
156157
return pt
157158

158-
def CBC_AES_decrypt(ct, key, iv):
159+
def AES_CBC_decrypt(key, ct, iv):
159160
blocks = chunkify(ct, 16)
160161
pass1 = [bytearray(AES_ECB_decrypt(key, b)) for b in blocks]
161162
return "".join([str(XOR(a,bytearray(b))) for a,b in zip(pass1, [iv]+blocks)])
162163

163-
def CBC_AES_encrypt(pt, key, iv):
164+
def AES_CBC_encrypt(key, pt, iv):
165+
if len(pt)%16 != 0:
166+
raise RuntimeError("Wrong size for plaintext, not multiple of blocksize. Did you forget to pad?")
164167
blocks = chunkify(pt, 16)
165168
ct = [iv]
166169
for i,b in list(enumerate(blocks)):
167170
nb = XOR(bytearray(ct[i]),bytearray(b))
168171
ct.append(AES_ECB_encrypt(key, str(nb)))
169172
return "".join([str(x) for x in ct[1:]])
170173

171-
def pkcs7_pad(text, blocksize):
174+
def pkcs7_pad(text, blocksize=16):
172175
if len(text)%blocksize == 0:
173176
return text+bytearray([16]*16)
174177
pad = blocksize - (len(text)%blocksize)
175178
return text+bytearray([pad]*pad)
179+
180+
def pkcs7_depad(text, blocksize):
181+
return text[:-int(text[-1])]
182+
183+
def randstr(size=16):
184+
return str(bytearray([random.randint(0,255) for i in range(size)]))
185+
186+
def ECB_score(text):
187+
return vignereScore(bytearray(text), 16)
188+
189+
def repeated_block_counts(text, blocksize):
190+
count = defaultdict(int)
191+
for block in chunkify(text, blocksize):
192+
count[block] += 1
193+
return sorted(count.values())
194+
195+
def repeated_blocks(text, blocksize):
196+
return max(repeated_block_counts(text, blocksize))
197+
198+
def accuracy(predictions, actual):
199+
correct = 0
200+
for p, actual in zip(predictions, actual):
201+
if p is actual:
202+
correct +=1
203+
return float(correct)/len(predictions)

0 commit comments

Comments
 (0)