-
-
Save artjomb/e65680ce7d1d8284670cbe56afec814b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from Crypto.Cipher import AES | |
from Crypto.Util import Counter | |
import random | |
import time | |
datafile = "t1.dat" | |
BLOCK_SIZE = 16 | |
def read_block(fname): | |
block_list = [] | |
with open(fname, 'rb') as blobfo: | |
atEOF = False | |
while not atEOF: | |
blobdata = blobfo.read(BLOCK_SIZE) | |
block_list.append(blobdata) | |
# print len(blobdata) | |
if len(blobdata) < BLOCK_SIZE: | |
atEOF = True | |
return block_list | |
print 'loading data' | |
block_list = read_block(datafile) | |
print 'loading finish' | |
print len(block_list), 'blocks' | |
print 'start encryption' | |
NUM_COUNTER_BITS = 128 | |
# Here I just use a random key | |
key = os.urandom(16) | |
t1 = time.time() | |
ct1 = [] | |
for block in block_list: | |
ctr = Counter.new(NUM_COUNTER_BITS) | |
cipher = AES.new(key, AES.MODE_CTR, counter=ctr) | |
ct1.append(cipher.encrypt(block)) | |
t2 = time.time() | |
print 'finish encryption' | |
print 'total time:', "%.20f" % (t2 - t1) | |
print 'time for each aes:', (t2 - t1) / len(block_list) | |
print 'num of aes per sec:', len(block_list) / (t2 - t1) | |
print 'now with method 1.5' | |
t1 = time.time() | |
ctr = Counter.new(NUM_COUNTER_BITS) | |
cipher = AES.new(key, AES.MODE_CTR, counter=ctr) | |
ct2 = [] | |
for block in block_list: | |
ct2.append(cipher.encrypt(block)) | |
t2 = time.time() | |
print 'finish encryption' | |
print 'total time:', "%.20f" % (t2 - t1) | |
print 'time for each aes:', (t2 - t1) / len(block_list) | |
print 'num of aes per sec:', len(block_list) / (t2 - t1) | |
#for b1, b2 in zip(ct1, ct2): | |
# if b1 != b2: | |
# print "Fail", b1, b2 | |
print 'now try to encrypt whole file' | |
with open(datafile, 'rb') as f: | |
block = f.read() | |
print type(block), len(block) | |
print 'start encryption' | |
NUM_COUNTER_BITS = 128 | |
key = os.urandom(16) | |
t1 = time.time() | |
ctr = Counter.new(NUM_COUNTER_BITS) | |
cipher = AES.new(key, AES.MODE_CTR, counter=ctr) | |
cipher.encrypt(block) | |
t2 = time.time() | |
print 'finish encryption' | |
print 'total time:', t2 - t1 | |
print 'time for each aes:', (t2 - t1) / len(block_list) | |
print 'num of aes per sec:', len(block_list) / (t2 - t1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment