I am trying to create an algorithm for bruteForce cracking MD5 hash.
My goal is to measure the time consumption when splitting into fibers for the processor and optionally graphics in compute clastr.
I got stuck in creating an algorithm.
The input should be a string. According to the number of string characters, I need to create the same number of forcycles.
Statically written for 3 digist, it looks like this:
#include <iostream>
#include <string>
#include "md5.h"
using namespace std;
int main()
{
string imput = "slv";
cout << "imput string: "<< imput << endl;
cout << "MD5 HASH: "<< wantedHash << endl;
do
{
cout << '\n' << "Enable BruteForce Craker";
} while (cin.get() != '\n');
string s;
for(int i=0; i != 256; i++)
{
for(int j=0; j != 256; j++)
{
for(int k=0; k != 256; k++)
{
string s = md5(string(1,(char)i) + string(1,(char)j) + string(1,(char)k));
serchCounter++;
if(s == wantedHash)
{
cout << "Find: " << string(1,(char)i) + string(1,(char)j) + string(1,(char)k) << endl;
cout << "Count TestedHash: " << serchCounter << endl;
return 0;
}
}
}
}
return 0;
}
My idea .. something like that ...
#include <iostream>
#include <string>
#include "md5.h"
using namespace std;
string imput = "s";
string wantedHash = md5(imput);
double serchCounter = 0;
int bruteForse(int longString, string s)
{
for(int i=0; i != 256; i++)
{
string s = md5(string(1,(char)i));
serchCounter++;
if(s == wantedHash)
{
cout << "Find: " << string(1,(char)i);
cout << "Count TestedHash: " << serchCounter << endl;
return 0;
}
}
if(longString > 1) bruteForse(--longString, s);
return 0;
}
int main()
{
cout << "imput string: "<< imput << endl;
cout << "MD5 HASH: "<< wantedHash << endl;
bruteForse(imput.length(),imput);
}
I would do:
bool increase(std::string& s)
{
for (auto rit = s.rbegin(); rit != s.rend(); ++rit) {
auto& c = s[i];
if (c == -1) {
c = 0;
continue;
} else if (c == 127) {
c = -128;
} else {
++c;
}
return true;
}
return false;
}
void bruteForce(std::size_t size, const string& wantedHash)
{
std::string s;
s.resize(size);
do {
if (md5(s) == wantedHash) {
cout << "Find: " << s << std::endl;
}
} while (increase(s));
}
Related
I just got started with C++ and I'm currently trying to build an algorith that cracks passwords. I ran into a problem with nested loops for longer passwords. It currently works fine for 4 character passwords, I have to wait for a while, but it works. Is there a way to get rid of the hard coded nested loops, to make the algorithm work for more than for characters without having to copy and paste the if statements over and over again?
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
const char alphanum[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!##$%^&* ";
const string pw = "0000";
bool cracked = false;
int tries = 0;
const int pwLength = pw.length();
string crack();
string guess="0";
const int maxLen = 30;
int main()
{
clock_t begin, end;
double time;
begin = clock();
string crackedPassword = crack();
end = clock();
time = double(end - begin) / (double)CLOCKS_PER_SEC;
cout << "The password is: " << crackedPassword << " The program checked a total of " << tries << " possibilites and took " << time << " seconds to crack the password!";
return 0;
}
string crack() {
int index = 0;
int repeat = 0;
while (!cracked) {
for (int i = 0; i < 71; i++) {
tries++;
guess[index] = alphanum[i];
cout << "Trying: " << guess << endl;
if (repeat > 0) {
for (int i = 0; i < 71; i++) {
tries++;
guess.push_back(alphanum[i]);
cout << "Trying: " << guess << endl;
if (repeat > 1) {
for (int i = 0; i < 71; i++) {
tries++;
guess.push_back(alphanum[i]);
cout << "Trying: " << guess << endl;
if (repeat > 2) {
for (int i = 0; i < 71; i++) {
tries++;
guess.push_back(alphanum[i]);
cout << "Trying: " << guess << endl;
if (guess == pw) {
cracked = true;
break;
}
else {
guess.pop_back();
}
}
}
if (guess == pw) {
cracked = true;
break;
}
else {
guess.pop_back();
}
}
}
if (guess == pw) {
cracked = true;
break;
}
else {
guess.pop_back();
}
}
}
if (guess == pw) {
cracked = true;
break;
}
}
repeat++;
}
return guess;
}
I'm unable to get invalid emails to print in my code. I'm able to call the student roster just fine but that's about it.
#include <iostream>
#include <string>
#include "student.h"
#include "degree.h"
#include "roster.h"
using namespace std;
int main()
{
const string studentData[] =
{
"A1,John,Smith,John1989#gm ail.com,20,30,35,40,SECURITY",
"A2,Suzan,Erickson,Erickson_1990#gmailcom,19,50,30,40,NETWORK",
"A3,Jack,Napoli,The_lawyer99yahoo.com,19,20,40,33,SOFTWARE",
"A4,Erin,Black,Erin.black#comcast.net,22,50,58,40,SECURITY",
};
Roster* classRoster = new Roster(numberOfStudents);
for (int i = 0; i < numberOfStudents; ++i)
{
cout << studentData[i] << "," << endl;
}
cout << endl;
cout << "Invalid emails:";
for (int i = 0; i < numberOfStudents; ++i)
{
classRoster->printInvalidEmails();
}
cout << "Average days per class for each student" << endl;
for (int i = 0; i < numberOfStudents; ++i)
{
cout << endl;
}
cout << endl;
classRoster->printByDegreeProgram();
cout << endl;
system("pause");
return 0;
}
Here is my roster.cpp
#include <iostream>
#include <string>
#include "student.h"
#include "degree.h"
#include "roster.h"
using namespace std;
Roster::Roster()
{
This->rosterSize = 0;
this->lastIndex = -1;
this->classRosterArray = nullptr;
}
Roster::Roster(int rosterSize)
{
this->rosterSize = rosterSize;
this->lastIndex = -1;
this->classRosterArray = new Student * [rosterSize];
}
void Roster::add(string studentId, string firstName, string lastName,
string
emailAddress, int age,
int daysInCourse1, int daysInCourse2, int daysInCourse3, Degree degree)
{
}
void Roster::remove(string studentId)
{
for (int i = 0; i <= lastIndex; ++i)
{
if (classRosterArray[i] == nullptr)
{
cout << "ERROR: Student with ID: " << studentId << " not found" <<
endl;
break;
}
else if (studentId == classRosterArray[i]->getStudentId())
{
classRosterArray[i] = nullptr;
cout << "Student removed" << endl;
}
}
}
void Roster::printAll()
{
for (int i = 0; i <= this->lastIndex; ++i)
{
(this->classRosterArray)[i]->print();
}
}
void Roster::printAverageDaysInCourse(string studentId)
{
bool found = false;
for (int i = 0; i <= lastIndex; ++i) {
if ((*classRosterArray[i]).getStudentId() == studentId) {
found = true;
int average = 0;
average = (classRosterArray[i]->getNumberOfDaysToCompleteCourse()
[0]
+ classRosterArray[i]->getNumberOfDaysToCompleteCourse()[1] +
classRosterArray[i]->getNumberOfDaysToCompleteCourse()[2]) /
3;
cout << "Average days it took student " << studentId << " to
complete
a course: " << average
<< " days" << endl;
}
}
if (!found) cout << "Student not found." << endl;
}
void Roster::printInvalidEmails()
{
for (int i = 0; i < numberOfStudents; ++i)
{
string email = classRosterArray[i]->getEmailAddress();
bool isBad = false;
size_t at = email.find("#");
size_t period = email.find(".");
if (at == string::npos)
{
cout << "Invalid email. Missing # symbol." << endl;
}
else if (period == string::npos)
{
cout << "Invalid email. Missing a period." << endl;
}
else if (email.find(" ") != string::npos)
{
cout << "Invalid email. Spaces not allowed." << endl;
}
}
}
void Roster::printByDegreeProgram()
{
for (int i = 0; i < degreeType; ++i)
{
cout << &Roster::printByDegreeProgram;
}
}
Roster::~Roster()
{
for (int i = 0; i < numberOfStudents; ++i)
{
delete classRosterArray[i];
}
}
I'm just starting out and am not sure if i've totally messed myself up. Any help would be greatly appreciated. I thought might have been able to figure it out and i've tried a few different times but still unable to correct.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
const int MAX_NUMS = 200; // Constant for the maximum number of words.
const int MAX_GUESSES = 8;
const string LETTERS = "abcdefghijklmnopqrstuvwxyz";
char inputLetter();
int findChar(char letter, string&word);
string getGuessedWord(string&secretWord, string&lettersGuessed);
string getLettersGuessed(char letter, string&lettersGuessed, int n);
void display(string&lettersGuessed, string&wordGuessed, int num, int pos);
bool isDone(string wordGuessed);
int main()
{
string oneWord; // holds one word from input file
string secretWord; // holds secret word to be guessed
string words[MAX_NUMS]; // holds list of words from input file
int randomValue; // holds index of secret word
int count = 0; // holds number of words in the file
// Declare an ifstream object named myFile and open an input file
ifstream myFile;
myFile.open("P4Words.txt");
// Exit program if cannot open file for input
if (!myFile)
{
cout << "Error: Unable to open file for input" << endl;
return 0;
}
// Input words from a file into words array
// Add your code here ...
myFile >> oneWord;
while (!myFile.eof())
{
words[count] = oneWord;
count++;
myFile >> oneWord;
}
myFile.close();
cout << count << " words loaded." << endl;
srand(static_cast<unsigned int>(time(0)));
// Select a secret word
// Add your code here ...
secretWord = words[rand() % (count + 1) ];
// Possible useful variables the loop
string lettersGuessed = ""; // holds letters guessed so far
string wordGuessed; // holds current word guessed like �_ pp_ e�
int incorrectGuesses = 0; // holds number of incorrect guesses so far
char letter; // holds a guessed letter
bool done = false; // have not guessed the word yet
int num = 8; int pos;
cout << "Welcome to the game, Hangman V1 by Your Name!" << endl;
cout << "I am thinking of a word that is " << secretWord.length()
<< " letters long." << endl;
// Set up a loop to input guesses and process
// Add your code here ...
do
{
letter = inputLetter();
pos = findChar(letter, secretWord);
wordGuessed = letter;
lettersGuessed = getLettersGuessed(letter, lettersGuessed, 8 - num);
wordGuessed = getGuessedWord(secretWord, lettersGuessed);
display(lettersGuessed, wordGuessed, num, pos);
done = isDone(wordGuessed);
num--;
} while ((num > 1) && (done == false));
// Check for won or lost
// Add your code here ...
if (done == false)
{
cout << "sorry you lose..." << endl;
}
if (done == true)
{
cout << "congratulations! " << endl;
}
system("pause"); // stop program from closing, Windows OS only
return 0;
}
// Add function definitions here ...
char inputLetter()
{
int i;
char letter;
do
{
cout << "please guess a letter: " << endl;
cin >> letter;
for (i = 0; i < 25; i++)
{
if (letter == LETTERS[i])
{
return letter;
}
}
if (letter != LETTERS[i])
{
cout << "Oops! That is an invalid character." << endl;
}
} while (letter != LETTERS[i]);
}
int findChar(char letter, string &word)
{
int i = 0; int pos = 0; bool found = false;
do
{
if (word[pos] == letter)
{
return pos;
found = true;
}
pos++;
} while (pos<word.length() - 1);
if (found == false)
{
return -1;
}
}
string getGuessedWord(string&secretWord, string&letterGuessed)
{
string temp;
temp = secretWord;
for (size_t k = 0; k <= temp.length() - 1; k++)
{
temp[k] = '_';
}
for (size_t i = 0; i <= temp.length() - 1; i++)
{
for (size_t j = 0; j <= temp.length() - 1; j++)
{
if (letterGuessed[i] == secretWord[j])
{
temp[j] = letterGuessed[i];
}
}
}
return temp;
}
string getLettersGuessed(char letter, string&lettersGuessed, int n)
{
string temp;
temp = letter;
lettersGuessed.insert(n, temp);
return lettersGuessed;
}
void display(string&lettersGuessed, string&wordGuessed, int num, int pos)
{
if (pos != -1)
{
cout << "You have " << num << " guesses left." << endl;
cout << "Letters guessed so far: " << lettersGuessed << endl;
cout << "Good guess!: " << wordGuessed << endl;
cout << "-----------------------------------------------------" << endl;
}
if (pos == -1)
{
cout << "You have " << num << " guesses left." << endl;
cout << "Letters guessed so far: " << lettersGuessed << endl;
cout << "Oops! that letter is not my word: " << wordGuessed << endl;
cout << "-----------------------------------------------------" << endl;
}
}
bool isDone(string wordGuessed)
{
bool done = false; int k = 0;
for (size_t i = 0; i <= wordGuessed.length() - 1; i++)
{
if (wordGuessed[i] == '_')
{
k++;
}
}
if (k == 0)
{
done = true;
}
return done;
}
it says subscript is out of range I need help to fix it
let me know how to fix it please its a project that is due very soon
that's all I got so far
Change:
for (size_t i = 0; i <= temp.length() - 1; i++)
to:
for (size_t i = 0; i <= letterGuessed.length() - 1; i++)
I have one more question, I want to add a _ in front of every Capital letter which will be converted to lowercase, plus the first letter cannot be capital!! I cant figure out how to do it... :{ example:
input: loLollL, output: lo_loll_l
and I want it to go backwards too: input: lo_loll_l output: loLollL
code is here:
#include <iostream>
#include <algorithm>
using namespace std;
int main ()
{
const int max = 100;
string slovo;
int pocet_r;
cout << "Zadaj pocet uloh:" << endl;
cin >> pocet_r;
if(pocet_r >= 1 && pocet_r <=100)
{
// funkcia na zabezpecenie minimalneho poctu chars
for (int i = 0; i <pocet_r; i++)
{
cout << "Uloha " << i+1 << ":" << endl;
cin >> slovo;
if(slovo.size() > max)
{
cout << "slovo musi mat minimalne 1 a maximalne 100 znakov" << endl;
}
while( slovo.size() > max)
{
cin >> slovo;
}
for (int i=0; i <= slovo.size(); i++)
{
int s = slovo[i];
while (s > 'A' && s <= 'Z')
{
if(s<='Z' && s>='A'){
return s-('Z'-'_z');
}else{
cout << "chyba";
}
}
}
cout << slovo[i] << endl;
}
}else{
cout << "Minimalne 1 a maximalne 100 uloh" << endl;
}
system("pause");
}
EDIT>
for (int i=0; i <= slovo.size(); i++)
{
while (slovo[i] >= 'A' && slovo[i] <= 'Z')
{
string s = transform(slovo[i]);
cout << s << endl;
s = untransform(s);
cout << s << endl;
}
}
This should work:
#include <string>
#include <cctype>
#include <iostream>
using namespace std;
string
transform(const string& s)
{
const size_t n = s.size();
string t;
for (size_t i = 0; i < n; ++i)
{
const char c = s[i];
if (isupper(c))
{
t.push_back('_');
}
t.push_back(tolower(c));
}
return t;
}
string
untransform(const string& s)
{
string t;
const size_t n = s.size();
size_t i = 0;
while (i < n)
{
char c = s[i++];
if (c != '_')
{
t.push_back(c);
continue;
}
c = s[i++];
t.push_back(toupper(c));
}
return t;
}
int
main()
{
string s = transform("loLollL");
cout << s << endl;
s = untransform(s);
cout << s << endl;
}
I'm suppose to create a Dictionary as a Hash Table with Linked List to spell check a text document. I read in the file "words.txt" to create the dictionary. Also, I have to count/display the number of collisions at each slot in the hash table when I load in the dictionary "words.txt"
I'm given the source code for the HashTable Class with Linked List as followed :
hashtable.cpp (#include "listtools.cpp" since its using templates)
#include <iostream>
#include <string>
#include "listtools.h"
#include "listtools.cpp"
#include "hashtable.h"
using LinkedListSavitch::Node;
using LinkedListSavitch::search;
using LinkedListSavitch::headInsert;
using namespace std;
#define HASH_WEIGHT 31
namespace HashTableSavitch
{
HashTable::HashTable()
{
for (int i = 0; i < SIZE; i++)
{
hashArray[i] = NULL;
//array for collisons
collisionArray[i] = 0;
}
}
HashTable::~HashTable()
{
for (int i=0; i<SIZE; i++)
{
Node<string> *next = hashArray[i];
while (next != NULL)
{
Node<string> *discard = next;
next = next->getLink( );
delete discard;
}
}
}
unsigned int HashTable::computeHash(string s) const
{
unsigned int hash = 0;
for (unsigned int i = 0; i < s.length( ); i++)
{
hash = HASH_WEIGHT * hash + s[i];
}
return hash % SIZE;
}
bool HashTable::containsString(string target) const
{
int hash = this->computeHash(target);
Node<string>* result = search(hashArray[hash], target);
if (result == NULL)
return false;
else
return true;
}
void HashTable::put(string s)
{
int count = 0;
int hash = computeHash(s);
if (search(hashArray[hash], s) == NULL)
{
// Only add the target if it's not in the list
headInsert(hashArray[hash], s);
}
else
{
collisionArray[hash]++;
}
void HashTable::printArray()
{
int number;
for(int i = 0; i < SIZE; i++)
{
number = collisionArray[i];
cout << "----------------\n";
cout << "index = " << i << endl;
cout << "Collisions = " << number << endl;
cout << "----------------\n";
}
}
} // HashTableSavitch
my main.cpp file
#include <iostream>
#include <fstream>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include "hashtable.h"
using namespace std;
using HashTableSavitch::HashTable;
void upToLow(string & str);
void removePunct(string & str);
int main()
{
HashTable h;
string currWord;
string word;
int countMisspelled = 0;
int countCorrect = 0;
//Get input from words.rtf
ifstream dictionary("words.txt");
//File checking
if (dictionary.fail())
{
cout << "File does not exist" << endl;
cout << "Exit program" << endl;
}
//Create the dictionary as a hash table
while(dictionary >> currWord)
{
h.put(currWord);
}
dictionary.close();
//display collisions
h.printArray();
//Get input from gettysburg_address.txt
ifstream input("gettysburg_address.txt");
//File checking
if (input.fail())
{
cout << "File does not exist" << endl;
cout << "Exit program" << endl;
}
//Spell check gettysburg_address.txt
cout << "Misspelled words : " << endl;
cout << endl;
//If a word is not in the dictionary assume misspelled
while(input >> word)
{
removePunct(word);
upToLow(word);
if(h.containsString(word) == false)
{
countMisspelled++; // Increment misspelled words count
cout << word << " ";
if(countMisspelled % 20 == 0) // Display misspelled words 20 per line
{
cout << endl;
}
}
else
{
countCorrect++; // Increment correct words count
}
}
input.close();
cout << endl;
cout << endl;
cout << "Number of misspelled words : " << countMisspelled << endl;
cout << "Number of correct words : " << countCorrect << endl;
return 0;
}
/*Function to convert uppercase letters to lowercase*/
void upToLow(string & str)
{
for (unsigned int i = 0; i < strlen(str.c_str()); i++)
if (str[i] >= 0x41 && str[i] <= 0x5A)
str[i] = str[i] + 0x20;
}
/*Function to remove punctuation from string*/
void removePunct(string & str)
{
str.erase(remove_if(str.begin(), str.end(), static_cast<int(*)(int)>(&ispunct)),str.end());
}
Is there a simple way to count the number of collisions at each slot when loading in "words.txt" ? If I implement a count variable in the "put" function I can get the total number of collisions, but I'm not quite sure how to count/display the number of collisions at each slot of the hash table. Any help/tips is appreciated.
EDIT :
Followed Joe's advice and now I'm wondering how I could display the number of collisions at each slot. I made a void function to do just that but it displays the number of collisions at each slot to be 0. Anyone know what I should do?
Probably the simplest way is declare an array in an appropriate place
int collisionArray[SIZE];
initialize it to 0 in HashTable::HashTable()
HashTable::HashTable()
{
for (int i = 0; i < SIZE; i++)
{
hashArray[i] = NULL;
collisionArray[i] = 0;
}
}
then increment the appropriate element when a collision is found
void HashTable::put(string s)
{
int count = 0;
int hash = computeHash(s);
if (search(hashArray[hash], s) == NULL)
{
// Only add the target if it's not in the list
headInsert(hashArray[hash], s);
collisionArray[hash]++;
}
}