我正在使用 python 来加密字符串列表。然而,它需要很长的时间(0.045 秒/列表)。我是否犯了任何导致代码运行缓慢的重大错误? 这是代码:
row=['string1', 'string2','string3','string4','string5','string6','string7','string8','string9']
from pyDes import *
import time
def encode(data,password):
k = des(password, CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
d = k.encrypt(data)
return d
start=time.time()
for idx, val in enumerate(row):
row[idx]=encode(str(val).encode(), 'password')
end=time.time()
print(end-start)
实际上,该列表要长得多(~33),并且是字符串和整数的组合。 有关如何加快该过程的任何提示。我还会考虑建议使用不同的加密。 谢谢!
最佳答案
密码的任何纯Python实现基本上都会很慢。 Python 的用途有很多,但快速位运算并不是其中之一。这就是为什么许多解释语言使用密码的 C/C++ 实现(甚至手动优化的汇编)。
尝试PyCrypto其中supports the AES-NI CPU instruction在最近的 Intel 和 AMD 处理器上。谁知道呢,您实际上可能会使用几分钟内无法被破解的密码实现(例如 key 大小为 56 位的单个 DES)。
关于Python:加快列表加密速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43733969/