Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*****PYTHON LANGUAGE***** CS 2302 - Data Structures Lab 1 - Option C (Password C

ID: 3746822 • Letter: #

Question

*****PYTHON LANGUAGE*****

CS 2302 - Data Structures
Lab 1 - Option C (Password Cracking)

A cyber-security company called UltraHackz is interested in finding talented computer science students. To do so, they post a challenge you find very interesting. They upload a txt file that contains 100 records containing information about 100 system accounts. Each record has a username, a salt value, and a hashed password.

Each record is stored as follows: <username>,<salt value>,<hashed password>

That is, there is one line per record in the file, where the three values are separated by commas. Your job is to find the real password associated with each of the accounts. They tell you that all passwords contain only numbers (0-9), and that each password is at least 3 characters long, and at most 7 characters long. Your task is to implement a recursive method to generate all possible passwords (brute force). To make it interesting, UltraHackz posted the following rules:

The method MUST be recursive

Numbers 3 and 7 are not be hard-coded inside the method that generates the

passwords; they should be parameters of the method

You can have at most two nested loops inside this method (less than two is perfectly fine

as well).

Every time you generate a string s , you need to check if s is the password of any of the usernames in the file. To do so, concatenate s with a user’s salt value, and apply the hashlib.sha256 m ethod to the resulting string. If the output generated by sha256 matches the hashed string for that account, string s is the real account’s password! You accept UltraHackz challenge as you think this is a perfect opportunity to show off your recursion skills!

***Starter Code***

import hashlib

def hash_with_sha256(str):

hash_object = hashlib.sha256(str.encode('utf-8'))

hex_dig = hash_object.hexdigest()

return hex_dig

def main():

hex_dig = hash_with_sha256('This is how you hash a string with sha256')

print(hex_dig)

main()

***Records txt file***

Explanation / Answer

please give thumbs up, thanks

code:

import hashlib
import itertools
def hash_with_sha256(str):
hash_object = hashlib.sha256(str.encode('utf-8'))
hex_dig = hash_object.hexdigest()
return hex_dig

def main():
x=input("Enter minimum password length ");
y=input("Enter maximum password length");
li = []
#password generated of maximum length J
for j in range(int(x),int(y)+1):
for i in itertools.product([0,1,2,3,4,5,6,7,8,9], repeat=int(j)):
li.append(''.join(map(str, i)))
print("PSWRD GNRTED")
input_file =open("user_data.txt","r")
for line in input_file:
name,salt,hashed_value=line.split(",");
salt.replace(" ","");
hashed_value.replace(" ","")
print("Applying bruteforce attack for ",name)
for password in li:
password.replace(" ","");
#in question they mention that pasord and salt and concatinted, but salt is at first position or generated string password, ask you teacher and then correct it, it is not mention in question
hex_dig = hash_with_sha256(salt+password);
if(hex_dig==hashed_value):
print(user+"has password"+password)
break
main()