3
預期結果:遞歸遍歷每個字符組合
該方案需要一個散列密碼作爲輸入;這被傳遞給解密函數。 該函數遍歷每個混合大小寫的字母組合,對每個這樣的字符串進行散列。 如果找到與輸入相匹配的內容,則會打印出未散列的密碼;否則,它退出。
實際結果:
在每個混合大小寫字母僅適用於當前迭代的最後一封信功能迭代。問題的
說明:
我想實現一個簡單的蠻力在Python DES-加密的密碼,破解。我使用大量for循環做了一個4字符密碼的實現,但現在我想使用遞歸來重構它的一段長度。 如何迭代每個字符組合,從1個字符組合到4個字符的字符串組合?
我想用這條線:
password[i] = string.ascii_letters[j]
但我得到這個錯誤:
TypeError: 'str' object does not support item assignment
代碼片段:
def decrypt(encryptedText):
# reference global variable
global password
# check curr password guess length
pwdlen = len(password)
if pwdlen >= 4:
print("Password is longer than 4 characters, exiting...")
exit(2)
# debug lines
print("password is {}".format(password))
print("length: {}".format(pwdlen))
time.sleep(2)
# first two characters is salt
salt = encryptedText[:2]
# Check hashes for every combination of strings and compare them
# starts with last char
for i in range(pwdlen, 0, -1):
for j in range(0, len(string.ascii_letters)):
password = string.ascii_letters[:pwdlen] + string.ascii_letters[j]
hashed = crypt.crypt(password, salt)
# debug line
print(password)
# if found - print password and exit
if hashed == encryptedText:
print(password)
exit(0)
# this makes recursion go through +1 char combinations on every iteration
password = (pwdlen + 1) * 'a'
return decrypt(encryptedText)
字符串在Python中是不可變的,不支持項目分配。 – Charul