//evil_server.cpp
#include <string>
#include <cstdlib>
#include <iostream>
#include "evil_server.h"
using namespace std;
EvilServer :: EvilServer()
{
hacked[0] = hacked[1] = hacked[2] = false;
passwords[agent_index(MrMean)] =
random_pw(MrMean);
passwords[agent_index(MsChief)] =
random_pw(MsChief);
passwords[agent_index(DrEvil)] =
random_pw(DrEvil);
}
void EvilServer :: change_pw(EvilAgent agent, string
new_pw)
{
int index = agent_index(agent);
if (new_pw == passwords[index])
return;
hacked[index] = false;
passwords[index] = new_pw;
}
string EvilServer :: random_pw(EvilAgent agent)
{
string password;
int length;
switch (agent)
{
case DrEvil:
length = 4 +
(rand() % 5);
for (int i = 0;
i < length - 4; ++i)
password += random_char();
if (rand() %
2)
password = "gato" + password;
else
password += "gato";
break;
case MsChief:
length = 4 +
(rand() % 5);
password =
"haha";
for (int i = 5;
i <= length; ++i)
password += random_char();
break;
case MrMean:
length = 1 +
rand() % 8;
for (int i = 0;
i < length; ++i)
if (rand() % 2)
password += 'g';
else
password += 'r';
break;
}
return password;
}
char EvilServer :: random_char()
{
char c = rand() % 36;
if (c < 10)
c += 48;
else
c += (97-10);
return c;
}
string EvilServer :: agent_name(EvilAgent agent)
{
switch(agent)
{
case DrEvil:
return "Dr.
Evil";
case MsChief:
return "Ms.
Chief";
case MrMean:
return "Mr.
Mean";
}
}
int EvilServer :: agent_index(EvilAgent agent)
{
switch(agent)
{
case DrEvil:
return 0;
case MsChief:
return 1;
case MrMean:
return 2;
}
}
bool EvilServer :: guess(EvilAgent agent, string guess)
{
string name = agent_name(agent);
int index = agent_index(agent);
if (guess != passwords[index] || hacked[index])
return false;
hacked[index] = true;
return true;
}
bool EvilServer :: is_hacked(EvilAgent agent)
{
return hacked[agent_index(agent)];
}
//***************************************************************************
//evil_server.h
#ifndef EVILSERVER_H
#define EVILSERVER_H
#include <string>
using namespace std;
enum EvilAgent {DrEvil, MsChief, MrMean};
class EvilServer
{
public:
EvilServer();
bool guess(EvilAgent agent, string
guess);
void change_pw(EvilAgent agent,
string new_pw);
bool is_hacked(EvilAgent
agent);
private:
static string agent_name(EvilAgent
agent);
static int agent_index(EvilAgent
agent);
static string random_pw(EvilAgent
agent);
static char random_char();
bool hacked[3];
string passwords[3];
};
#endif
//************************************************************
//hack.h
#include "evil_server.h"
void hack(EvilServer &es);
//*******************************************************
//main.cpp
#include <iostream>
#include <cassert>
#include <string>
#include <cstdlib>
#include <ctime>
#include "evil_server.h"
#include "hack.h"
using namespace std;
clock_t test_start;
bool test_time_exceeds(int seconds)
{
clock_t cur = clock();
return seconds <=
static_cast<float>(cur-test_start)/CLOCKS_PER_SEC;
}
int main()
{
// Initialize random number generation, i.e. rand(),
using current millisecond
srand(2017);
test_start = clock();
// Hack something
EvilServer es1;
es1.change_pw(MrMean, "grrr");
es1.change_pw(MsChief, "haha");
es1.change_pw(DrEvil, "gato1");
hack(es1);
assert(es1.is_hacked(DrEvil) || es1.is_hacked(MsChief)
|| es1.is_hacked(MrMean));
cout << "20% earned." <<
endl;
if (test_time_exceeds(60))
exit(0);
// Hack something
es1.change_pw(MrMean, "gggrrr");
es1.change_pw(MsChief, "haha0");
es1.change_pw(DrEvil, "1gato");
hack(es1);
assert(es1.is_hacked(DrEvil) || es1.is_hacked(MsChief)
|| es1.is_hacked(MrMean));
cout << "30% earned." <<
endl;
if (test_time_exceeds(60))
exit(0);
// Hack something
es1.change_pw(MrMean, "grggrrrr");
es1.change_pw(MsChief, "haha0001");
es1.change_pw(DrEvil, "gato0abc");
hack(es1);
assert(es1.is_hacked(DrEvil) || es1.is_hacked(MsChief)
|| es1.is_hacked(MrMean));
cout << "40% earned." <<
endl;
if (test_time_exceeds(60))
exit(0);
// Hack all three
es1.change_pw(MrMean, "grrr");
es1.change_pw(MsChief, "haha");
es1.change_pw(DrEvil, "gato1");
hack(es1);
assert(es1.is_hacked(MrMean));
assert(es1.is_hacked(MsChief));
assert(es1.is_hacked(DrEvil));
cout << "50% earned." <<
endl;
if (test_time_exceeds(60))
exit(0);
// Hack all three
es1.change_pw(MrMean, "gggrrr");
es1.change_pw(MsChief, "haha0");
es1.change_pw(DrEvil, "1gato");
hack(es1);
assert(es1.is_hacked(MrMean));
assert(es1.is_hacked(MsChief));
assert(es1.is_hacked(DrEvil));
cout << "60% earned." <<
endl;
if (test_time_exceeds(60))
exit(0);
// Hack all three
es1.change_pw(MrMean, "grggrrrr");
es1.change_pw(MsChief, "haha0001");
es1.change_pw(DrEvil, "gato0abc");
hack(es1);
assert(es1.is_hacked(MrMean));
assert(es1.is_hacked(MsChief));
assert(es1.is_hacked(DrEvil));
cout << "70% earned." <<
endl;
if (test_time_exceeds(60))
exit(0);
// Hack something (random passwords)
EvilServer es2;
hack(es2);
assert(es2.is_hacked(DrEvil) || es2.is_hacked(MsChief)
|| es2.is_hacked(MrMean));
cout << "75% earned." <<
endl;
if (test_time_exceeds(60))
exit(0);
// Hack all three (random passwords)
assert(es2.is_hacked(MrMean));
assert(es2.is_hacked(MsChief));
assert(es2.is_hacked(DrEvil));
cout << "80% earned." <<
endl;
if (test_time_exceeds(60))
exit(0);
// Hack all three (random passwords, 3x)
for (int i = 0; i < 3; ++i)
{
EvilServer es_tmp;
hack(es_tmp);
assert(es_tmp.is_hacked(MrMean));
assert(es_tmp.is_hacked(MsChief));
assert(es_tmp.is_hacked(DrEvil));
if (test_time_exceeds(60))
exit(0);
}
cout << "90% earned." <<
endl;
// Hack all three (random passwords, 10x)
for (int i = 0; i < 10; ++i)
{
EvilServer es_tmp;
hack(es_tmp);
assert(es_tmp.is_hacked(MrMean));
assert(es_tmp.is_hacked(MsChief));
assert(es_tmp.is_hacked(DrEvil));
if (test_time_exceeds(60))
exit(0);
}
cout << "95% earned." <<
endl;
// Hack all three (random passwords, 50x)
for (int i = 0; i < 50; ++i)
{
EvilServer es_tmp;
hack(es_tmp);
assert(es_tmp.is_hacked(MrMean));
assert(es_tmp.is_hacked(MsChief));
assert(es_tmp.is_hacked(DrEvil));
if (test_time_exceeds(60))
exit(0);
}
cout << "100% earned." <<
endl;
return 0;
}
//*******************************************************
/*
You are a software engineer hired by a coalition of nations to circumvent the security system of an evil, multinational cabal. In particular, you need to write functions that use a brute-force approach to crack the passwords of three high-level cabal members: Mr. Mean, Ms. Chief, and Dr. Evil. As you already know, recursion is often helpful in such a situation.
Previous intel gathering has determined that all three passwords have length at most 8 and consist of only lowercase letters and numbers. Intel has also uncovered the following additional information about each password:
1. Mr. Mean’s password consists of only the characters ’g’ and
’r’. 2. Ms. Chief’s password starts with "haha".
3. Dr. Evil’s password starts or ends with the string "gato".
Because of the cabal’s security system, your code must be able to crack passwords within a few seconds.
Create a new C++ source file named hack.cpp and implement the function declared in hack.h, so that main.cpp, hack.cpp, and evil server.cpp compile into a program that runs with no failed assertions.
*/
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include "hack.h"
using namespace std;
void hackMrMean(EvilComputer* ec);
void hackMsChief(EvilComputer* ec);
void hackDrEvil(EvilComputer* ec);
void aux_hackMrMean(EvilComputer* ec,string password, int d);
void aux_hackMsChief(EvilComputer* ec,string password, int
d);
void aux_hackDrEvil(EvilComputer* ec,string password, int d);
char getChar(int j);
void hack(EvilComputer* ec)
{
hackMrMean(ec);
hackMsChief(ec);
hackDrEvil(ec);
}
void hackMrMean(EvilComputer* ec)
{
aux_hackMrMean(ec,"",0);
}
void aux_hackMrMean(EvilComputer* ec,string password, int d)
{
if(ec->is_hacked(EvilComputer::MrMean)||
ec->guess(EvilComputer::MrMean,password) || d==9)
return;
aux_hackMrMean(ec,password+'g',d+1);
aux_hackMrMean(ec,password+'r',d+1);
}
void hackMsChief(EvilComputer* ec)
{
aux_hackMsChief(ec,"",0);
}
void aux_hackMsChief(EvilComputer* ec,string password, int d)
{
if(ec->is_hacked(EvilComputer::MsChief)||
ec->guess(EvilComputer::MsChief,"haha"+password) || d==5)
return;
for(int j=0;j<36;j++)
{
aux_hackMsChief(ec,password+getChar(j),d+1);
}
}
void hackDrEvil(EvilComputer* ec)
{
aux_hackDrEvil(ec,"",0);
}
void aux_hackDrEvil(EvilComputer* ec,string password, int d)
{
if(ec->is_hacked(EvilComputer::DrEvil)||
ec->guess(EvilComputer::DrEvil,"gato"+password) ||
ec->guess(EvilComputer::DrEvil,password+"gato") || d==5)
return;
for(int j=0;j<36;j++)
aux_hackDrEvil(ec,password+getChar(j),d+1);
}
char getChar(int j)
{
if(j<26)
return 'a'+j;
else
return '0'+j-26;
}
Get Answers For Free
Most questions answered within 1 hours.