在特定基础上计数的东西在Python中如何工作?

问题描述 投票:0回答:1

所以,我正在制作Python代码基本上是暴力加密(因为为什么不),但我的代码效率真的很低基本上我有一个脚本循环遍历其中包含字母/数字的列表,它只有一堆for 循环,每个循环递增一个,一旦该循环结束,它就会递增下一个循环,依此类推。它现在确实有效,但就像我说的,它的效率非常低。到目前为止我拥有的当前代码是这样的(这个是针对 SHA-512 的):

def bruteForceDecryptSHALength3(encrypted, characters):
    # For 1-3 letter words
    decrypt = ""
    for c1 in characters:
        decrypt = c1
        if hashlib.sha512(decrypt.encode("utf-8")).hexdigest() == encrypted:
            return decrypt
        for c2 in characters:
            decrypt = c1 + c2
            if hashlib.sha512(decrypt.encode("utf-8")).hexdigest() == encrypted:
                return decrypt
            for c3 in characters:
                decrypt = c1 + c2 + c3
                if hashlib.sha512(decrypt.encode("utf-8")).hexdigest() == encrypted:
                    return decrypt

我只是想知道一种使用 less for 循环的方法,它给出相同的结果,这样我就有了一种更有效的暴力破解方法(这仍然非常低效)

python decoding
1个回答
2
投票

只需将 1 添加到包含数字各个数字的列表即可。

def addOne(digits):
  index = 0
  carry = 1
  while carry == 1:
    index -= 1
    sum = digits[index] + carry
    digits[index] = sum & 0x1
    carry = sum >> 1

digits = [0] * 10
for _ in range(15):
  addOne(digits)
  print(''.join(map(str,digits)))

根据您对问题的编辑,您更清楚您想要什么。

您想要枚举 N 个字符集合的笛卡尔积。

import hashlib
from itertools import product

def bruteForceDecryptSHALengthN(encrypted, characters, N=3):
  for charCount in range(1, N+1):
    for decryptTpl in product(characters, repeat=charCount):
      decrypt = ''.join(decryptTpl)
      if hashlib.sha512(decrypt.encode("utf-8")).hexdigest() == encrypted:
          return decrypt
© www.soinside.com 2019 - 2024. All rights reserved.