[原创]密码管理器源代码更新版

作者在 2007-02-26 17:09:00 发布以下内容

/*SK-CHINA 密码管理器 0.0.2 BY S.K*/
/*写于2007.2.16 晚上 于家中       */
/*更新记录:(1)2007.2.25          */
/*          1.对存储的密码进行加密*/
/*          2.使用软件需要输入密码*/

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

/*二进制数据文件结构*/
typedef struct {
  char name[30];
  char password[30];
} password;

int main(void) {
  int i,k;
  password s[100];
  unsigned char num=1;
  FILE *input,*output;
  char p[30];

  printf("正在装入数据...");
  if((input=fopen("data","rb"))!=NULL) {
    fread(&num,sizeof(unsigned char),1,input);
    for(i=0;i<num;i++) {
      fread(&s[i],sizeof(password),1,input);
      /*解密*/
      k=0;
      while(s[i].name[k]!='\0') {
        s[i].name[k]-=k;
        k++;
      }k=0;
      while(s[i].password[k]!='\0') {
        s[i].password[k]+=k;
        k++;
      }
    }
    fclose(input);
    printf("\r数据装入完毕\n");
  } else {
    printf("\r无法找到已有的数据,程序将新建一个文件\n");
    printf("请设定本管理器密码:");
    scanf("%s",s[0].password);
    strcpy(s[0].name,"密码管理器");
    utput=fopen("data","wb");
    fwrite(&num,sizeof(unsigned char),1,output);
    fwrite(&s[i],sizeof(password),1,output);
    fclose(output);
  }

  clrscr();
  printf("请输入密码:");
  scanf("%s",p);
  if(strcmp(p,s[0].password)!=0) {
    printf("密码错误,程序将退出");
    return 0;
  }
  clrscr();

  while(1) {
    char c;
    char a[30];
    int t;
    printf("--------------------------------\n");
    printf("SK-CHINA 密码管理器 0.0.1 BY S.K\n");
    printf("--------------------------------\n");
    printf("A:添加数据 C:更改数据 D:删除数据\n");
    printf("F:查找数据 S:显示数据 E:保存退出\n");
    printf("--------------------------------\n");
    printf("---注意:密码记录0为本程序密码---\n");
    printf("\n请输入你希望的操作:");
    scanf("%c",&c);
    clrscr();
    switch(c) {
      case 'A': { printf("请输入密码名:");
                  scanf("%s",s[num].name);
                  printf("请输入密码:");
                  scanf("%s",s[num].password);
                  num++;
                  break;
                }
      case 'F': { printf("请选择查找数据的方式:\n");
                  printf("1.密码记录号 2.密码名 --->");
                  scanf("%d",&t);
                  switch(t) {
                    case 1: { printf("请输入密码记录号:");
                              scanf("%d",&i);
                              printf("密码记录号  密码名                          密码\n");

                              printf("%-12d%-32s%-30s\n",i,s[i].name,s[i].password);
                              printf("按任意键继续");
                              getch();
                              break;
                            }
                    case 2: { printf("请输入密码名:");
                              scanf("%s",a);
                              printf("密码记录号  密码名                          密码\n");
                              for(i=0;i<num;i++) {
                                if(strcmp(a,s[i].name)==0)
                                  printf("%-12d%-32s%-30s\n",i,s[i].name,s[i].password);
                              }
                              printf("按任意键继续");
                              getch();
                              break;
                            }
                  }
                  break;
                }
      case 'D': { printf("请输入密码记录号:");
                  scanf("%d",&t);
                  if(t>-1 && t<num)
                    num--;
                  else {
                    printf("密码记录号不存在,按任意键继续\n");
                    getch();
                    break;
                  }
                  for(i=t;i<num;i++)
                    s[i]=s[i+1];
                  break;
                }
      case 'C': { printf("请输入密码记录号:");
                  scanf("%d",&t);
                  if(!(t>-1 && t<num)) {
                    printf("密码记录号不存在,按任意键继续\n");
                    getch();
                    break;
                  }
                  printf("请输入更改后的密码名:");
                  scanf("%s",s[t].name);
                  printf("请输入更改后的密码:");
                  scanf("%s",s[t].password);
                  break;
                }
      case 'S': { printf("密码记录号  密码名                          密码\n");
                  for(i=0;i<num;i++)
                    printf("%-12d%-32s%-30s\n",i,s[i].name,s[i].password);
                  printf("\n");
                  printf("按任意键继续");
                  getch();
                  break;
                }
      case 'E': { printf("是否保存更改?(1.保存 2.不存)");
                  scanf("%d",&t);
                  if(t==1) {
                    utput=fopen("data","wb");
                    fwrite(&num,sizeof(unsigned char),1,output);
                    /*加密*/
                    for(i=0;i<num;i++) {
                      k=0;
                      while(s[i].name[k]!='\0') {
                        s[i].name[k]+=k;
                        k++;
                      }k=0;
                      while(s[i].password[k]!='\0') {
                        s[i].password[k]-=k;
                        k++;
                      }
                    }
                    for(i=0;i<num;i++)
                      fwrite(&s[i],sizeof(password),1,output);
                    fclose(output);
                  }
                  printf("按任意键退出");
                  getch();
                  return 0;
                }
    }
  }
  return 0;
}

原创作品 | 阅读 1729 次
文章评论,共1条
卧龙孔明(作者)
2008-01-19 11:37
1
丢失内容已补上,谢谢大家的错误报告
游客请输入验证码