Aug 28

【Code§C/C++】密码程序   不指定

felix021 @ 2007-8-28 22:16 [IT » 程序设计] 评论(1) , 引用(0) , 阅读(6968) | Via 本站原创 | |
【Code§C/C++】密码程序  
这个程序可以修改密码,依靠一个文件来保存的
密码有经过简单的加密


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int menu();
int verify(int choice);
int change();
int reboot();
int write(char *pass);
int read(char *pass);

FILE *fp=NULL;

int main()
{
  int choice=0,a,b,c;
  while(choice<1||choice>3)choice=menu();
  switch(choice)
  {
    case 1:
      a=verify(1);
      if(a)
      {  
        printf("Right password^_^\n");
        return 0;
      }
      else reboot();
      break;
    case 2:
      a=verify(2);
      if(a)change();
      else reboot();
      break;
    case 3:
      reboot();
      break;
    default:
      reboot();
  }
  return 0;
}

int menu()
{
  char input[200];
  int i;
  printf("\n\nWhat do you want to do?\n");
  printf("\t1. Enter password and use MSDOS\n");
  printf("\t2. Change password\n");
  printf("\t3. I don't know the password, reboot\n");
  printf("Input your choice: ");
  fflush(stdin);
  gets(input);
  fflush(stdin);
  for(i=0;input[i];i++)
    if(input[i]<='9'&input[i]>='1')break;  
  switch(input[i])
  {
    case '1':return 1;
    case '2':return 2;
    case '3':return 3;
    default :return 0;  
  }
}

int verify(int choice)
{
  if((fp=fopen("pass.dat","rb+"))==NULL)  
  { /*无密码文件,创建*/  
    printf("Currently there's no password.\n");
    if((fp=fopen("pass.dat","w"))==NULL)
    { /*无法创建*/
      printf("Unable to create a file.");
      return 0;
    }
    else
    { /*建立好了文件,修改密码*/
      fclose(fp);
      if(choice==2)return 1;
      return change();
    }  
  }
  else /*密码文件存在*/  
  {
    int times=0;
    char pass[20];
    char temp[20];
    read(temp);
    while(times<3)
    {
      printf("Please input the password: ");
      gets(pass);
      fflush(stdin);
      if(strcmp(pass,temp)==0)return 1;
      else printf("Bad password.\n");
      times++;
    }
    printf("\nYou have wrongly inputted the password 3 times.");
    return 0;
  }
}

int change()
{
  char pass1[20];
  char pass2[20];
  if((fp=fopen("pass.dat","w"))==NULL)
  { /*无法创建*/
    printf("Unable to create a file.");
    return 0;
  }
  else
  {
    changepass:
    printf("Please enter  new  password: ");
    gets(pass1);
    if(pass1[0]==0) goto changepass;
    fflush(stdin);
    printf("Please confirm the password: ");
    gets(pass2);
    fflush(stdin);
    if(strcmp(pass1,pass2)==0)
    {
      write(pass1);
      return 1;
    }
    else
    {
      printf("Two passwords don't match!\n");
      goto changepass;
    }
  }
}

int reboot()
{
  printf("\nThis computer will reboot now.\n");
  system("pause");
  system("off r");
}

int read(char *pass)
{
  int i=0;
  fread(pass,sizeof(char),20,fp);
  fclose(fp);
  for(i=0;i<20;i++)
    pass[i]-=i;
  return 0;
}  

int write(char *pass)
{
  int i=0;
  for(i=0;i<20;i++)
    pass[i]+=i;
  fwrite(pass,sizeof(char),20,fp);
  fclose(fp);
  return 0;
}  






欢迎扫码关注:




转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php
ghigui
2010-1-28 22:25
这是felix021的恶作剧,提醒您IE6很不安全。欢迎光临: http://www.felix021.com
felix021 回复于 2010-1-29 09:07
嗯  换浏览器吧。
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   *非必须
网址   电邮   [注册]