Question
You have been hired by a coalition of nations to circumvent the security system of an evil cabal! In particular, you need to write a
You have been hired by a coalition of nations to circumvent the security system of an evil cabal! In particular, you need to write a function that uses a brute-force approach to guess all possible passwords of three high-level cabal members: Mr. Mean, Ms. Chief, and Dr. Evil, and two other accounts, Guest and Dave. As you may know, recursion is often helpful in such a situation. Previous intelligence gathering has determined that all five passwords have length at most 8 and contain only lowercase letters (’a’-’z’) and digits (’0’-’9’. The following additional information about each password has also been uncovered:
1. Guest’s password is "passw123". 2. Dave’s password consists of exactly 4 digits. 3. Mr. Mean’s password consists of only the characters ’g’ and ’r’. 4. Ms. Chief’s password starts with "haha". 5. Dr. Evil’s password starts or ends with the string "gato". Because of the cabal’s security system, your function must be able to correctly guess all five passwords in a reasonable amount of time (around 15 seconds).
The following files have been given to you:
1. A C++ header file (evilcomputer.h) declaring the EvilComputer class (represents a replica of the cabal’s computer system). 2. A C++ source file (evilcomputer.cpp) implementing the EvilComputer class. 3. A C++ header file (hack.h) defining a password hacking function. 4. A C++ source file (main.cpp) containing a main() function with tests.
Create new C++ source file named hack.cpp that implements the function declared in hack.h
MAIN.CPP
//
#include <iostream>
#include <string>
#include <cstdlib>
#include <chrono>
#include "evilcomputer.h"
#include "hack.h"
using namespace std;
using namespace chrono;
inline void _test(const char* expression, const char* file, int line)
{
cerr << "test(" << expression << ") failed in file " << file;
cerr << ", line " << line << "." << endl;
abort();
}
#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))
int main()
{
cout << "These tests may take up to a minute." << endl;
// Initialize random number generation for later password generation.
srand(2017);
// Create four computers with specific passwords
EvilComputer ec1("1234", "grrr", "haha0", "gato1");
EvilComputer ec2("1235", "gggrrr", "haha456", "gato123");
EvilComputer ec3("2345", "gr", "haha0abc", "123gato");
EvilComputer ec4("6789", "grggrrrg", "hahaz123", "abc9gato");
EvilComputer ec5("9999", "rrrrrrrr", "hahahaha", "gatogato");
// Try to hack each of the four computers
// Check that each hack takes less than 3 seconds
system_clock::time_point start = system_clock::now();
hack(&ec1);
system_clock::time_point end = system_clock::now();
float dur = duration<float>(end - start).count();
test(dur < 3.0);
start = system_clock::now();
hack(&ec2);
end = system_clock::now();
dur = duration<float>(end - start).count();
test(dur < 3.0);
start = system_clock::now();
hack(&ec3);
end = system_clock::now();
dur = duration<float>(end - start).count();
test(dur < 3.0);
start = system_clock::now();
hack(&ec4);
end = system_clock::now();
dur = duration<float>(end - start).count();
test(dur < 3.0);
start = system_clock::now();
hack(&ec5);
end = system_clock::now();
dur = duration<float>(end - start).count();
test(dur < 3.0);
// Test that Guest's account is always hacked
test(ec1.is_hacked("Guest"));
test(ec2.is_hacked("Guest"));
test(ec3.is_hacked("Guest"));
test(ec4.is_hacked("Guest"));
test(ec5.is_hacked("Guest"));
// Test that Dave's account is always hacked
test(ec1.is_hacked("Dave"));
test(ec2.is_hacked("Dave"));
test(ec3.is_hacked("Dave"));
test(ec4.is_hacked("Dave"));
test(ec5.is_hacked("Dave"));
// Test that Mr. Mean's account was always hacked
test(ec1.is_hacked("Mr. Mean"));
test(ec2.is_hacked("Mr. Mean"));
test(ec3.is_hacked("Mr. Mean"));
test(ec4.is_hacked("Mr. Mean"));
test(ec5.is_hacked("Mr. Mean"));
// Test that Ms. Chief's account was always hacked
test(ec1.is_hacked("Ms. Chief"));
test(ec2.is_hacked("Ms. Chief"));
test(ec3.is_hacked("Ms. Chief"));
test(ec4.is_hacked("Ms. Chief"));
test(ec5.is_hacked("Ms. Chief"));
// Test that Dr. Evil's account was always hacked
test(ec1.is_hacked("Dr. Evil"));
test(ec2.is_hacked("Dr. Evil"));
test(ec3.is_hacked("Dr. Evil"));
test(ec4.is_hacked("Dr. Evil"));
test(ec5.is_hacked("Dr. Evil"));
// Test hacking some "randomly" chosen passwords
for (int i = 0; i < 10; ++i)
{
// Try to hack all three users with randomly chosen passwords
// (matching each user's password rules)
EvilComputer ec;
start = system_clock::now();
hack(&ec);
end = system_clock::now();
dur = duration<float>(end - start).count();
test(dur < 3.0);
test(ec.is_hacked("Guest"));
test(ec.is_hacked("Dave"));
test(ec.is_hacked("Mr. Mean"));
test(ec.is_hacked("Ms. Chief"));
test(ec.is_hacked("Dr. Evil"));
}
cout << "Assignment complete." << endl;
}
// END MAIN CPP
EVILCOMPUTER.H
//
#ifndef EVILCOMPUTER_H
#define EVILCOMPUTER_H
#include <string>
using namespace std;
class EvilComputer
{
public:
// Creates a new evil computer with random passwords.
EvilComputer();
// Creates a new evil computer with specific passwords.
EvilComputer(string dave_pw, string mr_mean_pw, string ms_chief_pw, string dr_evil_pw);
// Guess a password for an agent.
// Returns whether the password guessed is correct.
bool guess(string user, string guess);
// Returns whether an agent's password has been correctly guessed.
bool is_hacked(string user);
private:
int index(string user);
char random_char();
string passwords[5];
bool hacked[5];
};
#endif
//END EVILCOMPUTER.H
EVILCOMPUTER.CPP
//
#include <string>
#include <cstdlib>
#include <iostream>
#include "evilcomputer.h"
using namespace std;
EvilComputer :: EvilComputer(string dave_pw, string mr_mean_pw, string ms_chief_pw, string dr_evil_pw)
{
for (int i = 0; i < 5; ++i)
hacked[i] = false;
passwords[index("Guest")] = "passw123";
passwords[index("Dave")] = dave_pw;
passwords[index("Mr. Mean")] = mr_mean_pw;
passwords[index("Ms. Chief")] = ms_chief_pw;
passwords[index("Dr. Evil")] = dr_evil_pw;
}
EvilComputer :: EvilComputer()
{
for (int i = 0; i < 5; ++i)
hacked[i] = false;
passwords[index("Guest")] = "passw123";
string password;
int length;
password = "";
while (password.length() < 4)
password += '0' + (rand() % 10);
passwords[index("Dave")] = password;
password = "";
length = 1 + rand() % 8;
while (password.length() < length)
{
if (rand() % 2)
password += 'g';
else
password += 'r';
}
passwords[index("Mr. Mean")] = password;
password = "haha";
length = 1 + rand() % 8;
while (password.length() < length)
password += random_char();
passwords[index("Ms. Chief")] = password;
password = "";
length = 4 + rand() % 5;
while (password.length() < length - 4)
password += random_char();
if (rand() % 2 == 0)
password = "gato" + password;
else
password = password + "gato";
passwords[index("Dr. Evil")] = password;
}
char EvilComputer :: random_char()
{
char c = rand() % 36;
if (c < 10)
c += 48;
else
c += (97-10);
return c;
}
bool EvilComputer :: guess(string user, string guess)
{
int user_index = index(user);
if (user_index == -1)
return false;
if (guess != passwords[user_index])
return false;
hacked[user_index] = true;
return true;
}
bool EvilComputer :: is_hacked(string user)
{
return hacked[index(user)];
}
int EvilComputer :: index(string user)
{
if (user == "Mr. Mean")
return 0;
else if (user == "Ms. Chief")
return 1;
else if (user == "Dr. Evil")
return 2;
else if (user == "Dave")
return 3;
else if (user == "Guest")
return 4;
return -1;
}
//END EVILCOMPUTER.CPP
I NEED HELP TO IMPLEMENT
HACK.CPP
//
#include "hack.h"
void hack::hack(EvilComputer* ec)
{
}
//END HACK.CPP
Step by Step Solution
3.40 Rating (166 Votes )
There are 3 Steps involved in it
Step: 1
Working code implemented in C and appropriate comments provided for better understanding Here code for all files evilcomputercpp evilcomputerh hackcpp ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started