代码如下,实现原理比较简单,通过读取txt文件密码本与内置好的密码口令进行对比
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
char* ReadSpeacialLine(int i)
{
char filename[] = "C:/Users/cy345/Desktop/test.txt"; //文件名
FILE* fp;
errno_t error;
int WhichLine = i; //指定要读取哪一行
int CurrentIndex = 0; //当前读取的行
static char StrLine[1024]; //每行最大读取的字符数,可根据实际情况扩大
if ((error = fopen_s( &fp,filename,"r")) != 0) //判断文件是否存在及可读
{
printf("error!");
return NULL;
}
while (!feof(fp))
{
if (CurrentIndex == WhichLine)
{
while ((fgets(StrLine, 1024, fp)) != NULL)
{
int i = strlen(StrLine);
if (StrLine[i - 1] == '\n') //因为最后一行没有回车符
{
StrLine[i - 1] = '\0';
}
return StrLine;
}
}
fgets(StrLine, 1024, fp); //读取一行,并定位到下一行
CurrentIndex++;
if(i==-1)
fclose(fp);//已找到密码将密码本关闭
}
fclose(fp); //关闭文件
return NULL;
}
int main()
{
string sBuf;
string sName="3132353933";
for (int i = 0;i<1000; i++)
{
sBuf = ReadSpeacialLine(i);
if (sName.compare(sBuf)==0)
{
printf("密码已破解!密码为:");
cout << sBuf;
printf("\n");
ReadSpeacialLine(-1);
}
else continue;
}
return 0;
}