c++学习路线(九)
·
项目需求
项目5:仅实现了登录、菜单的显示
缺少
菜单的选择
具体菜单的实现
项目优化
1.封装login 函数,实现登录
2.封装菜单显示代码
3.添加主循环,以实现反复操作
#include <iostream>
#include <windows.h>
using namespace std;
void login(void) {
string username, password;
while (1) {
system("cls");
cout << "Enter your username: ";
cin >> username;
cout << "Enter your password: ";
cin >> password;
if (username == "admin" && password == "000123") {
cout << "Login successful!" << endl;
break;
}
else {
cout << "Invalid username or password. Please try again." << endl;
system("pause");
}
}
}
int main() {
login();
system("cls");
cout << "1.网站404" << endl;
cout << "2.网站篡改" << endl;
cout << "3.DNS攻击" << endl;
cout << "4.网站攻击记录" << endl;
cout << "5.退出系统" << endl;
return 0;
}
在上面进行改进
#include <iostream>
#include <windows.h>
using namespace std;
void login(void) {
string username, password;
while (1) {
system("cls");
cout << "Enter your username: ";
cin >> username;
cout << "Enter your password: ";
cin >> password;
if (username == "admin" && password == "000123") {
cout << "Login successful!" << endl;
break;
}
else {
cout << "Invalid username or password. Please try again." << endl;
system("pause");
}
}
}
void menuShow(void) {
cout << "1.网站404" << endl;
cout << "2.网站篡改" << endl;
cout << "3.DNS攻击" << endl;
cout << "4.网站攻击记录" << endl;
cout << "5.退出系统" << endl;
}
int menuSelct() {
int n = 0;
while (1) {
cin >> n;
if (cin.fail()){
cin.clear();
cin.sync();
cout << "输入有误,请重新输入!" << endl;
}
else {
break;
}
}
return n;
}
void webAttack404(void) {
system("cls");
cout << "网站404功能" << endl;
system("pause");
}
void dnsAttack(void) {
system("cls");
cout << "DNS攻击功能" << endl;
system("pause");
}
void webEdit(void) {
system("cls");
cout << "网站篡改功能" << endl;
system("pause");
}
void webAttackRecord(void) {
system("cls");
cout << "网站攻击记录功能" << endl;
system("pause");
}
void exitSystem(void) {
system("cls");
cout << "退出系统功能" << endl;
system("pause");
}
int main() {
login();
while (1) {
system("cls");
menuShow();
cout << "请选择要操作的功能: ";
int n = menuSelct();
switch (n) {
case 1:
webAttack404();
break;
case 2:
webEdit();
break;
case 3:
dnsAttack();
break;
case 4:
webAttackRecord();
break;
case 5:
exitSystem();
return 0;
break;
default:
cout << "输入有误,请重新输入!" << endl;
system("pause");
break;
}
}
return 0;
}
菜单居中显示
项目优化:
1.调整终端大小
2.菜单居中显示
命令提示符
mode con cols=80 lines=25

实现居中功能
#include <iostream>
#include <windows.h>
using namespace std;
#define MAX_WIDTH 50
#define MAX_HEIGHT 25
void login(void) {
string username, password;
while (1) {
system("cls");
cout << "Enter your username: ";
cin >> username;
cout << "Enter your password: ";
cin >> password;
if (username == "admin" && password == "000123") {
cout << "Login successful!" << endl;
break;
}
else {
cout << "Invalid username or password. Please try again." << endl;
system("pause");
}
}
}
void menuShow(void) {
string menu[] = {
"1.网站404",
"2.网站篡改",
"3.DNS攻击",
"4.网站攻击记录",
"5.退出系统"
};
//调整窗口
int menuSize = sizeof(menu) / sizeof(menu[0]);
int maxStringLength = 0;
//找出最长的字符串 然后用窗口长度-最长字符串 然后除以2
for (int i = 0; i < menuSize; i++)
maxStringLength = max(maxStringLength, menu[i].length());
int spaceLength = (MAX_WIDTH - maxStringLength) / 2;
for (int i = 0; i < menuSize; i++) {
for (int j = 0; j < spaceLength; j++) cout << " ";
cout << menu[i] << endl;
}
}
int menuSelct() {
int n = 0;
while (1) {
cin >> n;
if (cin.fail()){
cin.clear();
cin.sync();
cout << "输入有误,请重新输入!" << endl;
}
else {
break;
}
}
return n;
}
void init() {
char cmd[128];
sprintf_s(cmd, "mode con cols=%d lines=%d", MAX_WIDTH, MAX_HEIGHT);
system(cmd);
}
void webAttack404(void) {
system("cls");
cout << "网站404功能" << endl;
system("pause");
}
void dnsAttack(void) {
system("cls");
cout << "DNS攻击功能" << endl;
system("pause");
}
void webEdit(void) {
system("cls");
cout << "网站篡改功能" << endl;
system("pause");
}
void webAttackRecord(void) {
system("cls");
cout << "网站攻击记录功能" << endl;
system("pause");
}
void exitSystem(void) {
system("cls");
cout << "退出系统功能" << endl;
system("pause");
}
int main() {
init();
login();
while (1) {
system("cls");
menuShow();
cout << "请选择要操作的功能: ";
int n = menuSelct();
switch (n) {
case 1:
webAttack404();
break;
case 2:
webEdit();
break;
case 3:
dnsAttack();
break;
case 4:
webAttackRecord();
break;
case 5:
exitSystem();
return 0;
break;
default:
cout << "输入有误,请重新输入!" << endl;
system("pause");
break;
}
}
return 0;
}
接下俩我们实现hacker功能
这里的hacker功能因为较为复杂,所以以.h和lib的方式或者是.h和cpp的方式引入
具体的实现每个函数当我们学完后面的再进行补充

首先添加头文件hacker.h

如果有lib的话在附加依赖项进行引用
我这边直接引入cpp文件
引入的话,会有问题

在命令行加入

下面是hk404函数
void webAttack404(void) {
system("cls");
cout << "网站404攻击" << endl;
//int hk_404(char *id, char *response) ;
char id[128];
char response[128];
cout << "请输入网站ID: ";
scanf_s("%s", id, sizeof(id));
hk_404(id, response);
string ret = UTF8ToGBK(response);
cout << "网站404响应内容: " << ret << endl;
system("pause");
}
接受一个id号作为输入,然后返回一个response数组,因为服务器返回值是utf8编码 所以得转成gbk(如果是多编码也可以)(这里看不懂没关系 因为没有hk404的函数 这个函数后面讲)
如何制作自己的库?
根据cpp和h生成自己的lib(在接下来的课里面去讲)

可能会有这样的错误(这种应该是指定了utf8数据集)
隐藏密码
#include <conio.h>
这个头文件是为了引入getch
getch的优点
如果读到回车符返回\r
而且不会回显 也就是不会显示到屏幕上

1.改 getch 改成 _getch 即可编译通过
2.将sdl检查关掉

: fatal error LNK1168: 无法打开 E:\c++\rockproject\CP1\x64\Debug\hacker.exe 进行写入
这个是因为前一个窗口没关
#include <iostream>
#include <windows.h>
#include <string>
#include <conio.h>
#include "/c++file/hacker/hacker.h"
using namespace std;
#define MAX_WIDTH 50
#define MAX_HEIGHT 25
void input_pwd(char pwd[], int size) {
char c;
int i = 0;
while (1) {
c = _getch();
if (c == '\r') {
pwd[i] = '\0';
break;
}
pwd[i++] = c;
cout << "*";
}
}
void login(void) {
string username;
char password[128];
while (1) {
system("cls");
cout << "Enter your username: ";
cin >> username;
cout << "Enter your password: ";
input_pwd(password, sizeof(password));
if (username == "admin" && !strcmp(password, "000123")) {
cout << "Login successful!" << endl;
break;
}
else {
cout << "Invalid username or password. Please try again." << endl;
system("pause");
}
}
}
void menuShow(void) {
string menu[] = {
"1.网站404",
"2.网站篡改",
"3.DNS攻击",
"4.网站攻击记录",
"5.退出系统",
"6.恢复网站"
};
int menuSize = sizeof(menu) / sizeof(menu[0]);
int maxStringLength = 0;
for (int i = 0; i < menuSize; i++)
maxStringLength = max(maxStringLength, menu[i].length());
int spaceLength = (MAX_WIDTH - maxStringLength) / 2;
for (int i = 0; i < menuSize; i++) {
for (int j = 0; j < spaceLength; j++) cout << " ";
cout << menu[i] << endl;
}
}
int menuSelct() {
int n = 0;
while (1) {
cin >> n;
if (cin.fail()) {
cin.clear();
cin.sync();
cout << "输入有误,请重新输入!" << endl;
}
else {
break;
}
}
return n;
}
void init() {
char cmd[128];
sprintf_s(cmd, "mode con cols=%d lines=%d", MAX_WIDTH, MAX_HEIGHT);
system(cmd);
}
void webAttack404(void) {
system("cls");
cout << "准备进行网站404攻击" << endl;
//int hk_404(char *id, char *response) ;
char id[128];
char response[128];
cout << "请输入网站ID: ";
scanf_s("%s", id, sizeof(id));
hk_404(id, response);
string ret = UTF8ToGBK(response);
cout << "网站404响应内容: " << response << endl;
system("pause");
}
void dnsAttack(void) {
system("cls");
cout << "DNS攻击功能" << endl;
system("pause");
}
void clearbuffer() {
char ch;
while ((ch = getchar()) != '\n');
}
void webEdit(void) {
system("cls");
cout << "准备进行网站篡改攻击" << endl;
char id[128];
char response[128];
cout << "请输入网站ID: ";
scanf_s("%s", id, sizeof(id));
string attackText;
cout << "请输入攻击内容: ";
clearbuffer(); // 清空缓冲区 因为\n在缓冲区里面
GBKToUTF8(attackText); //
cin >> attackText; // 读取攻击内容
hk_tamper(id, (char *)attackText.c_str(), response);
string ret = UTF8ToGBK(response);
cout << "网站篡改响应内容: " << response << endl;
system("pause");
}
void webAttackRecord(void) {
system("cls");
cout << "网站攻击记录功能" << endl;
//int hk_404(char *id, char *response) ;
char id[128];
char response[128];
cout << "请输入网站ID: ";
scanf_s("%s", id, sizeof(id));
hk_record(id, response);
string ret = UTF8ToGBK(response);
cout << "网站404响应内容: " << response << endl;
system("pause");
}
void exitSystem(void) {
system("cls");
cout << "退出系统功能" << endl;
system("pause");
}
void repaireWeb(void) {
system("cls");
cout << "准备恢复网站功能" << endl;
//int hk_404(char *id, char *response) ;
char id[128];
char response[128];
cout << "请输入网站ID: ";
scanf_s("%s", id, sizeof(id));
hk_restore(id, response);
string ret = UTF8ToGBK(response);
cout << "网站恢复响应内容: " << response << endl;
system("pause");
}
int main() {
init();
login();
while (1) {
system("cls");
menuShow();
cout << "请选择要操作的功能: ";
int n = menuSelct();
switch (n) {
case 1:
webAttack404();
break;
case 2:
webEdit();
break;
case 3:
dnsAttack();
break;
case 4:
webAttackRecord();
break;
case 5:
exitSystem();
return 0;
break;
case 6:
repaireWeb();
break;
default:
cout << "输入有误,请重新输入!" << endl;
system("pause");
break;
}
}
return 0;
}
更多推荐


所有评论(0)