Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <cs50.h>
- #include <unistd.h>
- #include <crypt.h>
- #define _XOPEN_SOURCE
- char *crackh(char *current, char *encrypt, char *salt, int i, int limit);
- char *crack(char *encrypt, char *salt, int len);
- int main(int argsc, char *argsv[])
- {
- if (argsc != 2)
- {
- printf("Expected 1 command line argument");
- return 1;
- }
- char salt[3];
- salt[0] = argsv[1][0];
- salt[1] = argsv[1][1];
- salt[2] = '\0';
- //printf("");
- printf("%s\n", crack(argsv[1], salt, 4));
- return 0;
- }
- char *crackh(char *s, char *encrypt, char *salt, int i, int limit)
- {
- for (char c = 'a'; c < 'z'; c++)
- {
- //Runs each letter twice; Uppercase and lowercase
- for (int u = 0; u < 2; u++)
- {
- if (u == 0)
- {
- s[i] = c;
- }
- else
- {
- s[i] = toupper(c);
- }
- //This means that it is looking at passwords of the correct length and should start testing them
- if (i == limit)
- {
- if (strcmp(crypt(s, salt), encrypt) == 0)
- {
- return s;
- }
- }
- //If you aren't at the correct length of password, go deeper
- else
- {
- char *plain = crackh(s, encrypt, salt, i + 1, limit);
- if (strcmp(plain, "") != 0)
- {
- return plain;
- }
- }
- }
- }
- return "";
- }
- char *crack(char *encrypt, char *salt, int len)
- {
- char s[len + 2];
- //Calls crack for a 1 digit password, than a 2 digit password...
- for (int i = 0; i < len; i++)
- {
- //Sets \0 to be 1 after the last character that will be changed.
- s[i + 1] = '\0';
- char *plainText = crackh(s, encrypt, salt, 0, i);
- //If plaintext is "" crack couldn't find a password
- if (strcmp(plainText, "") != 0)
- {
- return plainText;
- }
- }
- return "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement