我试图弄清楚为什么下面的任务,反复RC4加密字符串,使用Pool.map()时速度较慢。您可以看到我将长时间运行的attack_byte
函数映射到范围(0,16),这比迭代调用attack_byte
16次要慢。我的代码中是否有一些无法并行化的操作?
import binascii
import base64
import itertools as it
import operator as op
import os
import multiprocessing as mp
from Crypto.Cipher import ARC4
secret_cookie = base64.b64decode("QkUgU1VSRSBUTyBEUklOSyBZT1VSIE9WQUxUSU5F").decode("utf-8")
def rc4_oracle(request):
rand_key = os.urandom(128)
cipher = ARC4.new(rand_key)
ptext = request + secret_cookie
return cipher.encrypt(ptext)
def attack_byte(idx):
chars = {char: 0 for char in range(256)}
padding = 'A'*(15 - idx)
for i in range(2**24):
if i % (10**6) == 0:
print("Iteration: ", i)
ctext = rc4_oracle(padding)
guess = ctext[15] ^ 0xF0
chars[guess]+= 1
max_char = max(chars.items(), key = op.itemgetter(1))
return chr(max_char[0])
pool = mp.Pool(processes = 16)
found_cookie = pool.map(attack_byte, range(0, 16))
#for i in range(16):
#found_cookie += attack_byte(i)
注意:这是来自cryptopals加密挑战。
我正在使用具有24核和64GB RAM的VSphere VM。