Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <crypt.h>
- // easy passwords for testing:
- // aaaaa:50XcgR31jl/4M
- // from problem set docs:
- // Z = 50R.6FuTGui8U
- // https://cs50.stackexchange.com/questions/24856/pset2-crack-vs-pointers
- // anushree:50xcIMJ0y.RXo = YES
- // brian:50mjprEcqC/ts = CA
- // bjbrown:50GApilQSG3E2 = UPenn (takes a while)
- // lloyd:50n0AAUD.pL8g = lloyd (takes a while)
- // malan:50CcfIk1QrPr6 = maybe (takes a while)
- // maria:509nVI8B9VfuA = TF
- // natmelo:50JIIyhDORqMU = nope
- // rob:50JGnXUgaafgc = ROFL
- // stelios:51u8F0dkeDSbY = NO
- // zamyla:50cI2vYkF0YU2 = LOL
- int main(int argc, char *argv[]) // declaring char *argv[] here is telling the function to expect a char type input & a its gonna be a pointer to an array of characters (remember how these work?)
- {
- if (argc != 2) // prevent the user from not entering the correct number of command line arguments
- {
- printf("Invalid Hash Needed!:\n ");
- return 1;
- }
- char *user_input; // declare a pointer (it's own variable in memory) called user_input
- user_input = argv[1]; // pointing to the array of characters, that the user inputted in each index of memory from arg[1]
- char salt[3] = {user_input[0], user_input[1], '\0'}; // declare an array of size 3. To store each of the two characters of the salt. The user input at the [0]th index of user_input & [1]st index of user_input + the null character at the end
- string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // declare an array of ALL OF THE test letters in the alphabet (a string is also an array of chars remember)
- char guess[5]; // declare an array of size FIVE that will store our guesses spat out (X to XXXX)
- for (int i = 0; i < strlen(alphabet); i++) // check your onenote for how these forloops work. This loops through 1 char (a-Z)
- {
- guess[0] = alphabet[i];
- guess[1] = '\0';
- if (strcmp(crypt(guess, salt), argv[1]) == 0) // check to compare the plaintext guess + salt & the hash in argv[1] with the strcmp() function AFTER it's encrypted into a hash with the crypt() function
- {
- printf("Password: %s\n", guess); // print the guess on the last iteration of the loop if the above condition is true
- return 0; // program is kill if the loop if above was successful
- }
- }
- for (int i = 0; i < strlen(alphabet); i++) // check onenote for how these nested forloops work. This loops through 2 chars (a-ZZ)
- {
- guess[0] = alphabet[i];
- for (int j = 0; j < strlen(alphabet); j++)
- {
- guess[1] = alphabet[j];
- guess[2] = '\0';
- if (strcmp(crypt(guess, salt), argv[1]) == 0) // check to compare the plaintext guess + salt & the hash in argv[1] with the strcmp() function AFTER it's encrypted into a hash with the crypt() function
- {
- printf("Password: %s\n", guess); // print the guess on the last iteration of the loop if the above condition is true
- return 0; // program is kill if the loop if above was successful
- }
- }
- }
- for (int i = 0; i < strlen(alphabet); i++) // check onenote for how thes enested forloops work. This loops through 3 chars (a-ZZZ)
- {
- guess[0] = alphabet[i];
- for (int j = 0; j < strlen(alphabet); j++)
- {
- guess[1] = alphabet[j];
- for (int k = 0; k < strlen(alphabet); k++)
- {
- guess[2] = alphabet[k];
- guess[3] = '\0';
- if (strcmp(crypt(guess, salt), argv[1]) == 0) // check to compare the plaintext guess + salt & the hash in argv[1] with the strcmp() function AFTER it's encrypted into a hash with the crypt() function
- {
- printf("Password: %s\n", guess); // print the guess on the last iteration of the loop if the above condition is true
- return 0; // program is kill if the loop if above was successful
- }
- }
- }
- }
- for (int i = 0; i < strlen(alphabet); i++) // check onenote for how these nested forloops work. This loops through 4 chars (a-ZZZZ)
- {
- guess[0] = alphabet[i];
- for (int j = 0; j < strlen(alphabet); j++)
- {
- guess[1] = alphabet[j];
- for (int k = 0; k < strlen(alphabet); k++)
- {
- guess[2] = alphabet[k];
- for (int l = 0; l < strlen(alphabet); l++)
- {
- guess[3] = alphabet[l];
- guess[4] = '\0';
- if (strcmp(crypt(guess, salt), argv[1]) == 0) // check to compare the plaintext guess + salt & the hash in argv[1] with the strcmp() function AFTER it's encrypted into a hash with the crypt() function
- {
- printf("Password: %s\n", guess); // print the guess on the last iteration of the loop if the above condition is true
- return 0; // program is kill if the loop if above was successful
- }
- }
- }
- }
- }
- printf("\n");
- }
Add Comment
Please, Sign In to add comment