首页 > 编程资源分享区 > C/C++源代码共享 > 文件加/解密的C实现 [转贴]
2006
05-30

文件加/解密的C实现 [转贴]





点击此处下载

/* 命令行模式下的文件加密 */
#include<stdio.h>
#include<string.h>
#define CHECK_LEN 18

void encrypt(int argc,char *argv[])
{
 FILE *fp,*temp;
 char file_tmp[15],*check=”*@6$!&^<mbq(8@:/#$”,tmp[20],ch;
 int psw_len,i;
 if((fp=fopen(argv[1],”r”))==NULL)   /* opening file error */
        {
         printf(“   Cannot open \”%s\”\n”,argv[1]);
         return;
        }
 strcpy(&file_tmp[1],argv[1]); /* temp file */
 file_tmp[0]=’_';
 temp=fopen(file_tmp,”w+”);  /* creat new temp file */
 if(argc==3) /* encrypt file */
        {
         if(!strcmp(argv[2],”-p”)||!strcmp(argv[2],”-P”))
                 {
                  printf(“Password cannot be \”-p\” or \”-P\”\n”);
                  return;
                 }
         psw_len=strlen(argv[2]);  /* password length */
         for(i=0;i<CHECK_LEN;i++)  /* write checkout bit */
                 fprintf(temp,”%c”,check+20);
         fprintf(temp,”%d”,psw_len*8); /* write password length */
         for(i=0;i<psw_len;i++)  /* write password */
                 fprintf(temp,”%c”,argv[2]+18);
         while(!feof(fp))   /* encrypt file’s content */
                 if((ch=fgetc(fp))!=-1)
                         {
                          ch+=8;
                          fputc(ch,temp);
                         }
         fclose(fp);
         fclose(temp);
         if(!remove(argv[1])&&!rename(file_tmp,argv[1]))
                 printf(“Encrypt \”%s\” scuccessfully!\n”,argv[1]);
        }
 else if(argc==4&&(!strcmp(argv[2],”-p”)||!strcmp(argv[2],”-P”)))  /* unencrypt the file */
        {
         fread(tmp,CHECK_LEN,1,fp); /* read checkout bit */
         for(i=0;i<CHECK_LEN;i++)  /* get back the checkout bit */
                 tmp-=20;
         tmp=’\0′;
         if(strcmp(tmp,check)!=0)
                 {
                  printf(“\nThis file was not encrypted my programme!”);
                  return;
                 }
         fscanf(fp,”%d”,&psw_len); /* read password length */
         psw_len/=8;
         fread(tmp,psw_len,1,fp); /* read password */
         for(i=0;i<psw_len;i++) /* get back the password */
                 tmp-=18;
         tmp=’\0′;
         if(strcmp(tmp,argv[3])!=0)
                 {
                  printf(“Password wrong!\n”);
                  return;
                 }
         while(!feof(fp))   /* unencrypt file’s content */
                 if((ch=fgetc(fp))!=-1)
                         {
                          ch-=8;
                          fputc(ch,temp);
                         }
         fclose(fp);
         fclose(temp);
         if(!remove(argv[1])&&!rename(file_tmp,argv[1]))
                 printf(“Unencrypt \”%s\” scuccessfully!\n”,argv[1]);
        }
}

main(int argc,char *argv[])
{
 if(argc<2||argc>4)
        {
         printf(“  Parameter Error!\n  You can enter \”encrypt /?\” for help!\n”);
         return;
        }
 if(!strcmp(argv[1],”/?”))   /* help information */
        {
         printf(“This programme is used to encrypting file!\n”);
         printf(“***CopyRight By MaBiqiang***\n\n”);
         printf(“   Useage:encrypt filename [-p] password\n”);
         printf(“\n   -p is an optional parameter,if encrypt a file can without “);
         printf(“it,but unencrypt a file must have it!\n”);
         return;
        }
 else if(argc==2)
        {
         printf(“  Parameter Error!\n  You can enter \”encrypt /?\” for help!\n”);
         return;
        }
 encrypt(argc,argv);
}


留下一个回复