commit d85b31355c22108c59285856e103d9f9def67992
parent 931e3bc27e51dadfb856737a201ab74697fd7533
Author: mpizzzle <michael.770211@gmail.com>
Date: Tue, 31 Oct 2017 21:23:35 +0000
refactoring ctr encryption into method
Diffstat:
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/set3/aes_in_ctr_mode.py b/set3/aes_in_ctr_mode.py
@@ -1,18 +1,18 @@
from Crypto.Cipher import AES
ciphertext = "L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==".decode("base64")
-blocks = [ciphertext[i:i + AES.block_size] for i in range(0, len(ciphertext), AES.block_size)]
-
key = "YELLOW SUBMARINE"
-aes = AES.new(key, AES.MODE_ECB)
-
nonce = ''.join(['\x00' for i in range(AES.block_size / 2)])
-block_count = ['\x00' for i in range(AES.block_size / 2)]
-plaintext = ""
+def encrypt(ciphertext, key, nonce):
+ aes = AES.new(key, AES.MODE_ECB)
+ block_count = ['\x00' for i in range(AES.block_size / 2)]
+ plaintext = ""
+
+ for block in [ciphertext[i:i + AES.block_size] for i in range(0, len(ciphertext), AES.block_size)]:
+ plaintext += ''.join([chr(ord(a) ^ ord(b)) for a, b in zip(block, aes.encrypt(nonce + ''.join(block_count)))])
+ block_count[0] = chr((ord(block_count[0]) + 1) % 256) #you get the point
-for block in blocks:
- plaintext += ''.join([chr(ord(a) ^ ord(b)) for a, b in zip(block, aes.encrypt(nonce + ''.join(block_count)))])
- block_count[0] = chr((ord(block_count[0]) + 1) % 256) #you get the point
+ return plaintext
-print plaintext
+print encrypt(encrypt(encrypt(ciphertext, key, nonce), key, nonce), key, nonce)