|
2 | 2 |
|
3 | 3 | import os |
4 | 4 | import base64 |
| 5 | +import random |
5 | 6 | from collections import defaultdict |
6 | 7 | import pprint |
7 | 8 |
|
@@ -141,35 +142,62 @@ def vignere(ciphertext, key): |
141 | 142 | plaintext = bytearray([a^b for a,b in zip(ciphertext, longkey)]) |
142 | 143 | return str(plaintext) |
143 | 144 |
|
144 | | -def AES_ECB_encrypt(key, plaintext): |
| 145 | +def AES_ECB_encrypt(key, plaintext, iv=None): |
145 | 146 | backend = default_backend() |
146 | 147 | cipher = Cipher(algorithms.AES(bytes(key)), modes.ECB(), backend=backend) |
147 | 148 | encryptor = cipher.encryptor() |
148 | 149 | ct = encryptor.update(bytes(plaintext)) + encryptor.finalize() |
149 | 150 | return ct |
150 | 151 |
|
151 | | -def AES_ECB_decrypt(key, ciphertext): |
| 152 | +def AES_ECB_decrypt(key, ciphertext, iv=None): |
152 | 153 | backend = default_backend() |
153 | 154 | cipher = Cipher(algorithms.AES(bytes(key)), modes.ECB(), backend=backend) |
154 | 155 | decryptor = cipher.decryptor() |
155 | 156 | pt = decryptor.update(ciphertext) + decryptor.finalize() |
156 | 157 | return pt |
157 | 158 |
|
158 | | -def CBC_AES_decrypt(ct, key, iv): |
| 159 | +def AES_CBC_decrypt(key, ct, iv): |
159 | 160 | blocks = chunkify(ct, 16) |
160 | 161 | pass1 = [bytearray(AES_ECB_decrypt(key, b)) for b in blocks] |
161 | 162 | return "".join([str(XOR(a,bytearray(b))) for a,b in zip(pass1, [iv]+blocks)]) |
162 | 163 |
|
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?") |
164 | 167 | blocks = chunkify(pt, 16) |
165 | 168 | ct = [iv] |
166 | 169 | for i,b in list(enumerate(blocks)): |
167 | 170 | nb = XOR(bytearray(ct[i]),bytearray(b)) |
168 | 171 | ct.append(AES_ECB_encrypt(key, str(nb))) |
169 | 172 | return "".join([str(x) for x in ct[1:]]) |
170 | 173 |
|
171 | | -def pkcs7_pad(text, blocksize): |
| 174 | +def pkcs7_pad(text, blocksize=16): |
172 | 175 | if len(text)%blocksize == 0: |
173 | 176 | return text+bytearray([16]*16) |
174 | 177 | pad = blocksize - (len(text)%blocksize) |
175 | 178 | 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