身份注册加部分审计功能

 // 注册.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#include <windows.h>
#include <fstream>
#include<iostream>

#include "md5.h"
using namespace std;

void update();
void record();

struct inf  //用户存储的信息结构
{
	char uid[10]; //用户名
	char password[200]; //密码

};
inf infor[300];  //最多有300个用户 
int i,n=0;       //全局变量n,用于表示是目前系统中用户数,目前是五个 
FILE *p,*p2;

typedef struct str_type
{
	int majuscule;
	int lower;
	int number;
	int othertype;
}str_type;

void init()    
{
	char uid[50],password[100] ;
 
	fstream file; 
	file.open("d:\\lib.txt",ios::in);
	if(!file);
	else
	{
		while(file>>uid>>password)     //将文件中的uid、password赋值给结构体 
		{
			strcpy(infor[n].uid,uid);
			strcpy(infor[n].password,password);


			//cout<<infor[n].uid<<"  "<<infor[n].password<<"  "<<endl;  //读出原来文件的uid、password
			n++;
		}
	}
	file.close();
}

int main()
{ 
	init();
	int i,j,r,N,temp;
	FILE *fp;
	char uid[10]={'\0'},password[100]={'\0'} ;
	int length=0;
	str_type strtype;
	strtype.lower=strtype.majuscule=strtype.number=strtype.othertype=0;

	fp=fopen("d:\\id.txt","r");
	fscanf(fp,"%s",uid) ;
	fclose(fp);
	fp=fopen("d:\\pwd.txt","rb");	
	fscanf(fp,"%s",password);
	fclose(fp);

	for(i=0;i<n;i++)
	{
		if(strcmp(infor[i].uid,uid)==0)
		{

			MessageBox(NULL,TEXT("该用户名已存在"),TEXT("提示"),MB_OK | MB_SYSTEMMODAL);
			record();
			p=fopen("d:\\log.txt","a+") ; 
			fwrite(uid,1, sizeof(uid), p);
			char buf[ ]="user reg failed\n\n";
			fwrite(buf, 1,sizeof(buf),p);
			fclose(p);
			return 0;

		}
	}
	if(i==n)   //如果没有用户名不重复,则下一步 



		fp=fopen("d:\\pwd.txt","r");
	fscanf(fp,"%s",password);	 
	fclose(fp);

	while(password[length]!='\0')
	{
		length++;
		if('A'<=password[length]&&password[length]<='Z')
		{
			strtype.majuscule=1;
		}
		else if('a'<=password[length]&&password[length]<='z')
		{
			strtype.lower=1;
		}
		else if('0'<=password[length]&&password[length]<='9')
		{
			strtype.number=1;
		}
		else 
		{
			strtype.othertype=1;
		}
	}
	if(length<8)
	{
		MessageBox(NULL,TEXT("您输入的密码太短"),TEXT("提示"),MB_OK | MB_SYSTEMMODAL);
		system("pause");
		return 0;
	}
	if((strtype.lower+strtype.majuscule+strtype.number+strtype.othertype)<3)
	{
		MessageBox(NULL,TEXT(" 你输入的密码过于简单,需要大写小写或数字组合!"),TEXT("提示"),MB_OK | MB_SYSTEMMODAL);
		system("pause");
		return 0;
	}
	for(i=0;i<length-3;i++)
	{
		for(j=i+1;j<length-2;j++)
		{
			if(password[i]==password[j]&&password[i+1]==password[j+1]&&password[i+2]==password[j+2])
			{
				MessageBox(NULL,TEXT(" 密码过于简单,重复性太高!"),TEXT("提示"),MB_OK | MB_SYSTEMMODAL);
				system("pause");
				return 0;
			}
		}

	}

	MessageBox(NULL,TEXT("用户注册成功!"),TEXT("提示"),MB_OK | MB_SYSTEMMODAL);


	strcpy(infor[n].uid,uid);
	strcpy(infor[n].password,MD5String(password)); 
	n++;
	update();


	record();
	p=fopen("d:\\log.txt","a+") ;
	char buf[ ]="user reg succeed\n\n";
	fwrite(uid,1, sizeof(uid), p);
	fwrite(buf,1, sizeof(buf), p);
	fclose(p);
	return 0;
}   

void record()
{
	FILE *fp;
	time_t t;
	struct tm *st;
	char buffer[1024];
	int index=0;
	fp=fopen("d:\\log.txt","w+");
	if (fp!=NULL)
	{
		while(!feof(fp))
			fgets(buffer,1024,fp);
		//sscanf(buffer,"%d",&index);
		//index++;
		fclose(fp);
	}
	fp=fopen("d:\\log.txt","a");
	t=time(NULL);
	st=localtime(&t);
	fprintf(fp," %d-%d-%d %2d:%2d:%2d\n",st->tm_year+1900,st->tm_mon+1,st->tm_mday,st->tm_hour,st->tm_min,st->tm_sec);
	index++;
	fflush(NULL);

}


void update()
{

	FILE *fp;
	fp = fopen("d:\\lib.txt","w+");//打开模式可以换成其他
	for(i=0;i<n;i++)
	{
		fprintf(fp,"%s %s  \n",infor[i].uid,infor[i].password );//就这么简单用fprintf函数写数据
	}
	fclose(fp);
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值