加密技术编程

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

一.加密技术编程
机密文件的有效销毁程序
文件加密技术
程序加锁
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<io.h>
#include<fcntl.h>
#include <stdlib.h>
#define MAXBUF 2048
#define MAXSIZE 512
static char *password;
char s1[9]="88888888";
void en_decrypt(void);
void pass_creat(void);
void pass_modify(void);
void wipe(void);
int main(void)
{FILE *fp;
int i=0,c;
clrscr();/*清屏*/
if((fp=fopen("KLK.CFG","rb+"))!=NULL)/*保存密码*/
{fseek(fp,0L,SEEK_SET);/*定位文件开始*/
fgets(s1,9,fp);/*把密码写入KLK。

CFG*/
fclose(fp); }
password=getpass("input the password:");/*输入密码屏幕不显示*/
while(strcmp(password,s1)!=0&&i<2)
{password=getpass("input the password:");
i++; }/*重新输入但最多只能两次*/
if(!strcmp(s1,password))/*密码正确*/
{
printf("\n1: creat the password \n"); /*创建密码修改密码加密解密删除文件退出*/
printf("2: modify the password\n");
printf("3: encrypt or decrypt the file\n");
printf("4: wipe the file\n");
printf("0: quit\n");
while(!strcmp(s1,password))
{
printf("\nenter your choice: ");
scanf("%d",&c);
switch(c)
{
case 1: pass_creat(); break;
case 2: pass_modify(); break;
case 3: en_decrypt(); break;
case 4: wipe(); break;
case 0: exit(0); break;
default: printf("\nerror!please choose '0','1','2', '3','4'.\n"); break;
}
}
}
else
printf("\nSorry!The password is wrong!\n");/*对不起!密码错误*/
return 0;
}
void pass_creat(void)/*新建密码*/
{
FILE *fp;
char c[7];
int i=0;
if((fp=fopen(s1,"wb"))==NULL)
{
printf("\nCannot open the file! \n");/*出现错误*/
exit(1);
}
while(i<MAXSIZE)/*文件大小符合*/
{
sprintf(c,"%d",rand());/*随机写值函数,c赋予了随机值*/
fputs(c,fp);/*将c写入文件*/
i+=sizeof(c)+1; }
fclose(fp);
}
void pass_modify(void)/*修改密码*/
{
FILE *fp;
char c[2][9],*p;
int i,j;
if((fp=fopen("KLK.CFG","wb"))==NULL)
{
printf("\nCannot open the file!\n");/*出现错误*/
exit(1);
}
for(j=0;j<2;j++)
{
printf("\ninput the password %s : \n", (j==1) ? "again" : "");/*循环输入新密码,并写入二维
数组中*/
for(i=0;i<8&&(c[j][i]=getch())!='\n';i++)
printf("*");
putchar('\n');
c[j][i]='\0';
}
if(!strcmp(c[0],c[1]))/*如果两次密码相同*/
{
fseek(fp,0L,SEEK_SET);/*定位文件开始*/
fputs(c[0],fp);/*写入文件*/
printf("\nthe new password has taken effect.\n");
}/*新密码生效*/
else
printf("\nTwo passwords are not the same.\n");/*两次输入不同*/
fclose(fp);
}
void en_decrypt(void)/*加密解密*/
{
char filename[13],s[13];
int i=0;
char c1,c2,buffer[MAXBUF];
FILE *fp1,*fp2;
printf("\ninput the file name to encrypt or decrypt: \n");/*输入要加密或解密的文件名*/
scanf("%s",filename);
printf("\ninput the file name which cotains the password: \n");/*输入含有密码的文件名*/
scanf("%s",s);
if((fp1=fopen(filename,"r"))==NULL)
{
printf("\nopen the file error! \n");/*打开文件失败*/
exit(1);
}
if((fp2=fopen(s,"r"))==NULL)
{
printf("\nopen the file error! \n");/*打开文件失败*/
exit(1); }
while(!feof(fp1))/*文件没有到末尾*/
{ c1=fgetc(fp1);/*取出filename文件内容*/
if(!(!feof(fp2)))
rewind(fp2);/*使文件fp的位置指针指向文件开始。

*/
c2=fgetc(fp2);
if(i>=MAXBUF)
{
printf("\nthe file is too large!\n");/*文件过大*/
exit(1); }
buffer[i++]=c1^c2;/*文件内容与密码抑或加密,写入数组中*/ }
if(i<MAXBUF-1)
buffer[i]='\0';/*数组末尾空字符*/
fclose(fp1);
fclose(fp2);
if((fp1=fopen(filename,"w"))==NULL)
{
printf("\nopen the file error! \n");/*打开文件失败*/
exit(1); }
for(i=0;i<MAXBUF&&buffer[i]!='\0';i++)/*加密后的内容写入文件*/ fputc(buffer[i],fp1);
fclose(fp1);
}
void wipe()/*删除文件*/
{
char filename[50];
FILE *fp;
long i, total;
int seed = 1000,file;
double r;
unsigned char temp;
printf("input the filename:\n");
scanf("%s",filename);
if((fp=fopen(filename, "r"))==NULL)
{
printf("cannot open the file!\n");
exit(0);/*打不开则退出*/
}
fseek(fp, 0, SEEK_END);/*定位文件末尾*/
total = ftell(fp);/*找到当前位置*/
/* 1 */
i = 0;
while(i<total)
{
fseek(fp, i, SEEK_SET);
fputc('0', fp);
i++;
}
i = 0;
while(i<total)
{
fseek(fp, i, SEEK_SET);
fputc('1', fp);
i++;
}
/* 2 */
i = 0;
while(i<total)
{
fseek(fp, i, SEEK_SET);
fputc('0', fp);
i++;
}
i = 0;
while(i<total)
{
fseek(fp, i, SEEK_SET);
fputc('1', fp);
i++;
}
/* 3 */
i = 0;
while(i<total)
{
fseek(fp, i, SEEK_SET);
fputc('0', fp);
i++;
}
i = 0;
while(i<total)
{
fseek(fp, i, SEEK_SET);
fputc('1', fp);
i++;
}
/* random */
i = 0;
srand(seed);
while(i<total)
{
fseek(fp, i, SEEK_SET);
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );/*随机写值*/ temp = (int) ( r * 255.0);
fputc(temp, fp);/*将随机值写入文件*/
i++;
}
fclose(fp);
if((file=open(filename,O_TRUNC))==EOF) /*O_TRUNC 如果文件存在,将其长度置为零,但保持其原属性*/
{
printf("open file %s error!\n", filename);/*如果出错输出此语句*/
exit(1);
}
close(file);
unlink(filename);
}。

相关文档
最新文档