⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 password.cpp

📁 VC++编程
💻 CPP
字号:
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
/////////////////////Password a2.0////////////////////////

#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "string.h"

///////////////////////////////////////////
///////////////////////////////////////////
#define SHOW_MENU		1
//////:Do we want to show the menu when 
//////:someone clicks on the exe
//////:or do we just show them an error message
//////:This Applies in dos as well
///////////////////////////////////////////
///////////////////////////////////////////
#define NULL_EXIT       0
/////: If NULL_EXIT is enabled when the user
/////: presses enter instead of inputting the
/////: password it will exit the password
/////: program.
/////: Else if NULL_EXIT is not enabled, an
/////: error message will appear and the
/////: program will not quit.
/////: This is,if you use this program in the
/////: start-up process off your computer
/////: you don't wont the program to quit if the
/////: user types nothing so with NULL_EXIT
/////: disabled it will no quit.
/////: But if you are using this with a program
/////: and the user types nothing and NULL_EXIT
/////: is enabled then it will quit back to your program
/////////////////////////////////////////////
/////////////////////////////////////////////
#define MAX_LENGTH		20
#define MASK "*"

char *error[] ={
	"Invalid call Parameter",
	"Could not open passwd.pwd file",
	"Could not write to passwd.pwd"
};

char path[] ="c:\\passwd.pwd";

char user_passwd[MAX_LENGTH];
char read_passwd[MAX_LENGTH];
char test_passwd[MAX_LENGTH];

char temp;

int ctrl	=0;
int ctrl2	=0;
int choice	=0;
int go		=0;
int CalledWithMenu	=0;

FILE *pwd;

void Menu( void );
void Read_PasswdFile( void );
void Write_PasswdFile( void );
void Get_Passwd( void );
void Change_Passwd( void );
void First_Write( void );
void Error( int error_num );


void main(int argc,char *argv[])
{
	Read_PasswdFile();

	if(argc>1)
	{
	if(!strcmp(argv[1],"/e")) Get_Passwd();
	else if(!strcmp(argv[1],"/c"))Change_Passwd();
	}
	else
		if(SHOW_MENU) Menu();
		else
		Error(0);

}

void Read_PasswdFile( void )
{
	if((pwd=fopen(path,"r"))==NULL)
	{
		First_Write();
	}

while(!feof(pwd))
{
	for(ctrl=0;ctrl<MAX_LENGTH;ctrl++)
	{
		temp=fgetc(pwd)+12;

		if(temp=='\n')
		{
			read_passwd[ctrl]=NULL;
			break;
		}
		read_passwd[ctrl]=temp;

		if(ferror(pwd))
		{
			Error(2);
			exit(1);
		}
	}
}

}

void Menu( void )
{
	//system("CLS");
	
	printf("\n\nPassword a2.0 Menu\n");
	printf("------------------\n\n");
	printf("1). Enter Password\n");
	printf("2). Change Paswword\n");
	printf("3). Exit\n\n");
	do
	{
	printf("Enter Choice: ");
	scanf("%d",&choice);
	
	if(choice==1)
	{
		go=2;
		CalledWithMenu=1;
		Get_Passwd();
	}
	else if(choice==2)
	{
		go=2;
		CalledWithMenu=1;
		Change_Passwd();
	}
	else if(choice==3)
	{
		exit(0);
	}	

	}while(!go);
}

void First_Write( void )
{
	if(!CalledWithMenu)
	{
	printf("\n\nEnter Password\n");
	printf("----------------\n\n");
	}
	if(CalledWithMenu)
	{
		printf("\n\n");
	}
	
start:
	printf("Password:\\>");
	
	for(ctrl=0;ctrl<=MAX_LENGTH;ctrl++)
	{
		ctrl2++;
		temp=getch();
		printf(MASK);

		if(temp=='\r' && ctrl !=0)
		{
			user_passwd[ctrl]=NULL;
			break;
		}
		else if(temp=='\r' && ctrl==0)
		{
			printf("\nPassword must be at least 1 char long.\n\n");
			ctrl2=0;
			goto start;
			break;
		}
		else
			user_passwd[ctrl]=temp;
	}

	printf("\nRe-Enter Password:\\>");

	for(ctrl=0;ctrl<=MAX_LENGTH;ctrl++)
	{
		temp=getch();
		printf(MASK);

		if(temp=='\r' && ctrl !=0)
		{
			test_passwd[ctrl]=NULL;

			if(!strcmp(user_passwd,test_passwd))
			{
				break;
			}
			else
			{
				printf("\nPasswords Differ!\n\n");
				ctrl2=0;
				goto start;
			}
		}
		else if(temp=='\r' && ctrl==0)
		{
			printf("\nPassword must be at least 1 char long.\n\n");
			ctrl2=0;
			goto start;
		}
		else
			test_passwd[ctrl]=temp;
	}

	printf("\nCreating Pwd File...");

	if((pwd=fopen(path,"w"))==NULL)
	{
		Error(1);
	}

	for(ctrl=0;ctrl<=ctrl2;ctrl++)
	{
		temp=user_passwd[ctrl];
		fputc(temp-12,pwd);

		if(ferror(pwd))
		{
			Error(2);
		}
	}
		
	printf("\nDone...");
	fclose(pwd);
}

void Change_Passwd( void )
{
	//system("CLS");
	if(!CalledWithMenu)
	{
	printf("\n\nChange Password\n");
	printf("---------------\n\n");
	}
	if(CalledWithMenu)
	{
		printf("\n\n");
	}
	start:
	printf("Password:\\>");
	
	for(ctrl=0;ctrl<=MAX_LENGTH;ctrl++)
	{
		temp=getch();
		printf(MASK);

		if(temp=='\r' && ctrl !=0)
		{
			user_passwd[ctrl]=NULL;
			break;
		}
		else if(temp=='\r' && ctrl==0)
		{
			if(SHOW_MENU)
			{
				Menu();
				break;
			}
			if(NULL_EXIT) exit(0);
			else
			{
				printf("\nPassword must be at least 1 char long.\n\n");
				goto start;
			}
				
		}
		else
			user_passwd[ctrl]=temp;
	}

	if(strcmp(user_passwd,read_passwd))
	{
		printf("\nPassword incorrect\n\n");
		goto start;
	}
	else
	{
	printf("\n\nEnter New Password:\\>");
	
	for(ctrl=0;ctrl<=MAX_LENGTH;ctrl++)
	{
		ctrl2++;
		temp=getch();
		printf(MASK);

		if(temp=='\r' && ctrl !=0)
		{
			user_passwd[ctrl]=NULL;
			break;
		}
		else if(temp=='\r' && ctrl==0)
		{
			printf("\nPassword must be at least 1 char long.\n\n");
			ctrl2=0;
			goto start;
			break;
		}
		else
			user_passwd[ctrl]=temp;
	}
	printf("\nRe-Enter New Password:\\>");

	for(ctrl=0;ctrl<=MAX_LENGTH;ctrl++)
	{
		temp=getch();
		printf(MASK);

		if(temp=='\r' && ctrl !=0)
		{
			test_passwd[ctrl]=NULL;

			if(!strcmp(user_passwd,test_passwd))
			{
				break;
			}
			else
			{
				printf("\nPasswords Differ!\n\n");
				ctrl2=0;
				goto start;
			}
		}
		else if(temp=='\r' && ctrl==0)
		{
			printf("\nPassword must be at least 1 char long.\n\n");
			ctrl2=0;
			goto start;
		}
		else
			test_passwd[ctrl]=temp;
	}
	if((pwd=fopen(path,"w"))==NULL)
	{
		Error(1);
	}

	for(ctrl=0;ctrl<=ctrl2;ctrl++)
	{
		temp=user_passwd[ctrl];
		fputc(temp-12,pwd);

		if(ferror(pwd))
		{
			Error(2);
		}
	}
		
	printf("\nPassword Changed...\n\n");
	fclose(pwd);
	}

		
}

void Get_Passwd( void )
{
	//system("CLS");
	if(!CalledWithMenu)
	{
	printf("\n\nEnter Password\n");
	printf("---------------\n\n");
	}
	if(CalledWithMenu)
	{
		printf("\n\n");
	}
start:
	printf("Password:\\>");
	
	for(ctrl=0;ctrl<=MAX_LENGTH;ctrl++)
	{
		temp=getch();
		printf(MASK);

		if(temp=='\r' && ctrl !=0)
		{
			user_passwd[ctrl]=NULL;
			break;
		}
		else if(temp=='\r' && ctrl==0)
		{
			if(SHOW_MENU)
			{
				Menu();
				break;
			}
			if(NULL_EXIT) exit(0);
			else
			{
				printf("\nPassword must be at least 1 char long.\n\n");
				goto start;
			}
		}
		else
			user_passwd[ctrl]=temp;
	}

	if(strcmp(user_passwd,read_passwd))
	{
		//////////////////////////////////
		printf("\nPassword incorrect\n\n");
		//////////////////////////////////
		//ENTER YOUR CODE HERE
		goto start;
		
	}
	else
		////////////////////////////
		printf("\nPassword Correct\n\n");
		//ENTER YOUR OTHER CODE HERE
}

void Error( int error_num )
{
	printf("%s",error[error_num]);
}





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -