Skip to content

Instantly share code, notes, and snippets.

@alisha
Created September 25, 2014 13:57
Show Gist options
  • Save alisha/caed7edf585bd167b57c to your computer and use it in GitHub Desktop.
Save alisha/caed7edf585bd167b57c to your computer and use it in GitHub Desktop.
/**
* crack.c
*
* Alisha Ukani
*
* Cracks DES-encrypted passwords
*/
#define _XOPEN_SOURCE
#include <unistd.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
bool testPassword(string encrypted, string password, string salt);
int main(int argc, string argv[]) {
// make sure there are only 2 command line arguments
if (argc != 2) {
return 1;
}
// encrypted is the actual given password
string encrypted = argv[1];
// generate salt
string salt = {(char)(encrypted[0]), (char)(encrypted[1])};
/* check passwords in dictionary
FILE *dictionary = fopen("/usr/share/dict/words", "r");
string line = "";
while (fscanf(dictionary, "%s", line) != EOF) {
if (testPassword(encrypted, line, salt)) {
return 0;
}
}
fclose(dictionary);*/
// for loops find all possible passwords
for (int a = 32; a <= 126; a++) {
for (int b = 32; b <= 126; b++) {
for (int c = 32; c <= 126; c++) {
for (int d = 32; d <= 126; d++) {
for (int e = 32; e <= 126; e++) {
for (int f = 32; f <= 126; f++) {
for (int g = 32; g <= 126; g++) {
for (int h = 32; h <= 126; h++) {
char password_arr[] = {a, b, c, d, e, f, g, h};
string password = password_arr;
if (testPassword(encrypted, password, salt)) {
return 0;
}
// check password with whitespace removed
}
}
}
}
}
}
}
}
}
// returns true if "password" is the correct password
bool testPassword(string encrypted, string password, string salt) {
if (crypt(password, salt) == encrypted) {
printf("%s\n", password);
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment