To give a bit of context I am taking CS50 through EdX in 2019.

I worked on pset2 (crack) the last few days. I got it "right" though I wonder if there are some ways to improve (1) the efficiency and (2) the beauty of my code. So if you haven't cracked pset2 yet don't read further, spoiler alert !

So basically the structure of my code is:

  • first check if the user provided the correct command line argument (just one word with 13 char)

  • implement a dictionary attack using a dictionnary txt file from which I have removed words with 6+ characters with a side program (as the password is supposed to have only 5 chars)

  • implement a bruteforce attack such as the program first tries all the combination for a 1 letter password, then 2 letters, etc... until 5 letters.

As far as I can tell it works. There is no "check50" for crack though I managed to crack all the examples provided, though the longer ones (5 letters) take a lot of time. I tried to improve my implementation along the following axis (which are also my questions)

1 - the dictionnary is only lowercase. So I thought it could be nice to implement a function which checks every word in the dictionnary in every lowercase / uppercase configurations possible. For instance if the word is "anvil" it would also check "Anvil", "aNvil" etc etc... I tried to implement that via using loops and "toupper" "tolower" function though I can't figure it out (yet)

2 - (SPOILER) I implemented the bruteforce attack using nested for loops. Just one loop for the one caracter attack, then two, etc... Though it feels a lot like "copy and pasting". So I thought there might be a recursive solution to that to make the code more elegant ? I tried some things so I cannot figure it out. Maybe it would be possible to implement a unique function who tries all the possible solution from 1 char to 5

3 - related to what I have said just above, maybe there is a way to make the cracking faster ? I have read here and there that some solution can crack any of those password in like 30 seconds. My program take a lot longer for 5 character password (I have not recorded though something like tens of mins)

Edit : also, week 2 is a lot about sorting algorithms though I have not used them in the pset. I wonder if these techniques should be used there ?

That's pretty much it. I hope this is clear enough and that I will receive some insights. Thanks guys !


You must log in to answer this question.

Browse other questions tagged .