程序基本思路:
创建一个二进制文件,将密码保存在第一行,接着输入正文。在输入密码与正文的同时将文件内容加密。读取文件时要求输入密码,密码正确方允许将文本读出。
程序的优点:
在对密码进行读取的时候,并没有使用scanf函数,而是利用C语言嵌入汇编语言实现对机器低层的调用(int 16和int 21号调用),从而实现对输入密码的"*"化处理
存在问题:
(1) 加密函数过于简单(线性映射),可以采取更复杂的函数将字符等可能地散列到各个样本点处。
(2) 文件创建时没有支持ADT数据类型.
(3) 输入单个字符长度有限制.
运行界面:

图一:输入0退出,1创建一个文件,2打开一个文件.在F盘下创建一个名为ttaikk的文件,密码设置为ttaikk

图二:输入文本,以#字符结束。

图三:输入密码并打开一个存在的文件。
/* Secure.c
Copyright (c) 2002, 2006 by ctu_85
All Rights Reserved.
*/
#include "stdio.h"
#include "string.h"
#define right 5
#define maxpassword 20
#define minpassword 6
#define maxlen 20
void Create();
void Load();
char secure(char);
char desecure(char);
void main()
{
int choice;
printf("Please enter your choice:\n");
printf("0:To quit;\n");
printf("1:To create a security file;\n");
printf("2:To load a security file .\n");
cir:
printf("Your choice:");
scanf("%d",&choice);
if(choice==0)
return;
if(choice==1)
{
Create();
printf("\n");
goto cir;
}
else
if(choice==2)
{
Load();
printf("\n");
goto cir;
}
else
{
printf("Invalid input!\n");
goto cir;
}
}
void Create()
{
FILE *fp;
char *p,ch,s[maxpassword],*t,temp,path[maxlen];
recre:
printf("Please enter the path where you wanna the file to be:");
scanf("%s",path);
p=path;
if(*p<'C'||*p>'F'||*(p+1)!=':'||*(p+2)!=92||strlen(p)>30||strlen(p)<4)
{
printf("Invalid input!\n");
goto recre;
}
if((fp=fopen(p,"wb"))==NULL)
{
printf("Error!");
return;
}
printf("Please enter the password:");
an:
p=s;
lp:
_asm {
mov ah,00h
int 16h
cmp al,0dh
je exit
mov temp,al
mov dl,'*'
mov ah,02h
int 21h
}
*p=temp;
p++;
goto lp;
exit:
*p='\0';
p=s;
if(strlen(p)>maxpassword||strlen(p)<minpassword)
{
printf("The password is too long or too short,please reinput!\n");
goto an;
}
while(*p!='\0')
{
ch=*p;
ch=secure(ch);
p++;
fputc(ch,fp);
}
ch='\n';
ch=secure(ch);
fputc(ch,fp);
printf("\nPlease enter the information,end with char '#':");
ch=getchar();
ch=getchar();
while(ch!='#')
{
ch=secure(ch);
fputc(ch,fp);
ch=getchar();
}
ch=secure(ch);
fputc(ch,fp);
fclose(fp);
}
void Load()
{
FILE *fp;
char *p,ch,s[maxpassword],tc,temp[maxpassword],pass[maxpassword],sign=secure('\n');
int i=0,t=0,lenth=0;
rece:
printf("Please enter the path where you wanna to load:");
scanf("%s",p);
if(*p<'C'||*p>'F'||*(p+1)!=':'||*(p+2)!=92||strlen(p)>30||strlen(p)<4)
{
printf("Invalid input!\n");
goto rece;
}
if((fp=fopen(p,"rb"))==NULL)
{
printf("Error!");
return;
}
printf("Please enter the password:");
an:
p=s;
lp:
_asm {
mov ah,00h
int 16h
cmp al,0dh
je exit
mov tc,al
mov dl,'*'
mov ah,02h
int 21h
}
*p=tc;
p++;
goto lp;
exit:
*p='\0';
p=s;
if(strlen(p)>maxpassword||strlen(p)<minpassword)
{
printf("The password is too long or too short,please reinput!\n");
goto an;
}
while(*p!='\0')
{
temp[i]=secure(*p);
p++;
i++;
}
temp[i]='\0';
printf("\n");
ch=fgetc(fp);
while(ch!=sign)
{
pass[t]=ch;
t++;
ch=fgetc(fp);
}
pass[t]='\0';
ch=desecure(ch);
if(!strcmp(temp,pass))
{
while(ch!='#')
{
ch=fgetc(fp);
ch=desecure(ch);
if(ch!='#')
putchar(ch);
}
}
else
printf("The password is incorrect!\n");
fclose(fp);
}
char secure(char c)
{
if(c+right>254)
return c-255+right;
else
return c+right;
}
char desecure(char c)
{
if(c<right)
return 255-right;
else
return c-right;
}
更多内容:
浙江大学ACM试题解答(四月)
http://blog.csdn.net/ctu_85/archive/2007/04/24/1576831.aspx
浙江大学ACM试题解答(三月)
http://blog.csdn.net/ctu_85/archive/2007/03/20/1535556.aspx
麻省理工算法导论翻译
http://blog.csdn.net/ctu_85/archive/2007/06/08/1643179.aspx
华容道游戏与算法
http://blog.csdn.net/ctu_85/archive/2007/05/15/1610722.aspx
中国象棋对战程序C语言源代码
http://blog.csdn.net/ctu_85/archive/2007/05/04/1596351.aspx
Dijkstra算法代码分析
http://blog.csdn.net/ctu_85/archive/2006/11/17/1393130.aspx
发表于 @ 2006年12月23日 11:03:00|评论(loading...)|编辑