C语言写的一个简单文件加密程序


C语言写的一个简单文件加密程序:

  1. #include<stdio.h>   
  2. #include<stdlib.h>   
  3. #include<string.h>   
  4.   
  5. int decrypt(FILE *in,FILE *out);  
  6. int encrypt(FILE *in,FILE *out);  
  7. unsigned char atoh(char *hexstr);  
  8.   
  9. int main(int argc,char **argv)  
  10. {  
  11.     if(argc > 1 && strcmp(argv[1],"--help") == 0)  
  12.     {  
  13.         printf("\nusage: linkrules_god [-e|-d] inputfile outputfile\n");  
  14.         printf("    -e encrypt inputfile to outputfile.\n");  
  15.         printf("    -d decrypt inputfile to outputfile.\n\n");  
  16.         exit(0);  
  17.     }  
  18.     if(argc != 4)  
  19.     {  
  20.         fprintf(stderr,"%s\n","please using --help go get help.");  
  21.         exit(-1);  
  22.     }  
  23.   
  24.       
  25.     FILE *in = fopen(argv[2],"rb");  
  26.     FILE *out = fopen(argv[3],"w");  
  27.     if(in == NULL || out == NULL)  
  28.     {  
  29.         fprintf(stderr,"%s\n","open file error!");  
  30.         exit(1);  
  31.     }  
  32.   
  33.     if(argv[1][1] == 'e')  
  34.     {  
  35.         encrypt(in,out);  
  36.     }  
  37.     else if(argv[1][1] == 'd')  
  38.     {  
  39.         decrypt(in,out);  
  40.     }  
  41.   
  42.     fclose(in);  
  43.     fclose(out);  
  44.     return 0;  
  45. }  
  46.   
  47. int encrypt(FILE *in,FILE *out)  
  48. {  
  49.     if(in == NULL || out == NULL)  
  50.     {  
  51.         fprintf(stderr,"%s\n","file error!\n");  
  52.         return -1;  
  53.     }  
  54.   
  55.     unsigned char hex;  
  56.     while(fread(&hex,1,1,in))  
  57.     {  
  58.         hex = ~hex^0x92;  
  59.         fprintf(out,"%02X",hex);  
  60.     }  
  61.     return 0;  
  62. }  
  63.   
  64.   
  65.   
  66. int decrypt(FILE *in,FILE *out)  
  67. {  
  68.     if(in == NULL || out == NULL)  
  69.     {  
  70.         fprintf(stderr,"%s\n","file error!");  
  71.         return -1;  
  72.     }  
  73.       
  74.     unsigned char hexstr[3];  
  75.     unsigned char hex = 0;  
  76.     int i = 0;  
  77.     while(fread(&hexstr,2,1,in))  
  78.     {  
  79.         hex = atoh(hexstr);  
  80.         hex = ~(hex ^ 0x92);  
  81.         fwrite(&hex,1,1,out);  
  82.     }  
  83.   
  84.     return 0;  
  85. }  
  86.   
  87.   
  88. /* convert string to hex */  
  89. unsigned char atoh(char *hexstr)  
  90. {  
  91.     int hextodec[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};  
  92.     char chtodec[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};  
  93.     unsigned char hexnum = 0;  
  94.     for(int i = 0; i < sizeof(chtodec); ++i)  
  95.     {  
  96.         if(hexstr[0] == chtodec[i])  
  97.         {  
  98.             hexnum += hextodec[i]*16;  
  99.         }  
  100.     }  
  101.   
  102.     for(int i = 0; i < sizeof(chtodec); ++i)  
  103.     {  
  104.         if(hexstr[1] == chtodec[i])  
  105.         {  
  106.             hexnum += hextodec[i];  
  107.         }  
  108.     }  
  109.   
  110.     return hexnum;  
  111. }  

encrypt -e sourcefile outputfile   加密

encrypt -d sourcefile outputfile   解密

encrypt --help    帮助

输出的加密文件中的内容:

相关内容