Ich entwarf einen Puzzle-Algorithmus, um den Wert jedes verschlüsselten Blocks zu nehmen, der auf den nächsten zu verschlüsselnden Block zeigt. Ich muss AES-CTR-128 dafür mit einem bestimmten Grund verwenden.Warum ist mein Python AES-CTR-128 Mikro-Benchmark mit blockweise Verschlüsselung langsam?
Ich führe einen Dummy-Test, um zu sehen, wie schnell oder langsam es sein könnte.
Hier ist was ich getan habe. Ich habe sowohl Pycrypto als auch Kryptographie getestet.
Ich erstelle zuerst eine 16MB-Datei mit zufälligen Bytes.
Ich habe versucht, auf zwei Arten:
Methode 1. Laden Sie die Datei in eine Sperrliste mit 128-Bit-Blockgröße.
Methode 2. Laden Sie einfach die Datei in eine Zeichenfolge.
Jetzt habe ich die Gesamtzeit getestet, um jeden 128-Bit-Block zu verschlüsseln. Und ich habe die Gesamtzeit getestet, um die gesamte Datei zu verschlüsseln.
Hier ist das Ergebnis:
PyCrypto:
durch ein 128-Bit-Block eines zu verschlüsseln: 61.824 aes-CTR-128 pro
secdie gesamte zu verschlüsseln Datei: 8.843.713 AES-CTR-128 pro Sekunde
Kryptografie
128-Bit-Block einer nach dem anderen zu verschlüsseln: 384.959 aes-CTR-128 pro sec
die gesamte Datei zu verschlüsseln: 113.417.922 aes-CTR-128 pro sec
Ich frage mich, warum Methode 1 und 2 mir das Ergebnis mit so viel Unterschied gab? Nehmen diese beiden Methoden die gleiche Geschwindigkeit an?
Hier ist mein Testcode:
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
backend = default_backend()
from Crypto.Cipher import AES
from Crypto.Util import Counter
import random
import time
BLOCK_SIZE = 16
def read_block(fname):
block_list = []
blobfo = open(fname)
atEOF = False
while not atEOF:
blobdata = blobfo.read(BLOCK_SIZE)
block_list.append(blobdata)
if len(blobdata) < BLOCK_SIZE:
# we should stop after this...
atEOF = True
return block_list
print 'loading data'
block_list = read_block('mediumdata')
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()
for block in block_list:
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)
print 'now try to encrypt whole file'
block = open('mediumdata').read()
print type(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)
print 'now try cryptography'
print 'start encryption'
t1 = time.time()
num = random.randint(1, 65530)
nonce = "".join(chr((num >> (i * 8)) & 0xFF) for i in range(16))
backend = default_backend()
cipher = Cipher(algorithms.AES(key), modes.CTR(nonce), backend=backend)
encryptor = cipher.encryptor()
for block in block_list:
ciphertext = encryptor.update(block)
encryptor.finalize()
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)
print 'try a whole file'
block = open('mediumdata').read()
print 'start encryption'
t1 = time.time()
num = random.randint(1, 65530)
nonce = "".join(chr((num >> (i * 8)) & 0xFF) for i in range(16))
backend = default_backend()
cipher = Cipher(algorithms.AES(key), modes.CTR(nonce), backend=backend)
encryptor = cipher.encryptor()
ciphertext = encryptor.update(block)# + encryptor.finalize()
encryptor.finalize()
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)
Habe ich hier etwas vermissen?
Gibt es eine Möglichkeit, dass ich Methode 1 schneller machen kann?
Verschlüsselungen sind, neigen dazu, langsam zu sein, ich glaube nicht, dass Sie gleiche Geschwindigkeit – YOU
bekommen @YOU Ist Methode 1 sollte die gleiche Geschwindigkeit wie Methode 2 bekommen? – Luke
@YOU Die Verschlüsselungsgeschwindigkeit hängt vom Gerät ab, sie muss nicht langsam sein. Ein iPhone6S arbeitet mit mehr als 400 MB/s. – zaph