home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / vrac / enigma30.zip / DES3-DIR.ZIP / DES.C < prev    next >
C/C++ Source or Header  |  1994-09-01  |  3KB  |  95 lines

  1. /* 
  2.  * this sample source will give you an idea on how to
  3.  * call up the various library functions. -SW
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <time.h>
  10. #include "des3.h"
  11.  
  12. #define BUFSIZE 4096
  13.  
  14. static char Usage[] = "des [-]{d|e}[2|3] infile outfile key";
  15.  
  16. int main(int argc, char **argv)
  17. {
  18.   register uchar *b, *buf, c, key[8], key2[16], key3[24];
  19.   register int i,j,len,what,mode;
  20.   register time_t t, t1;
  21.   FILE *ih, *oh;
  22.   if(argc == 5) {
  23.     switch (c = *argv[1] == '-' ? *++argv[1] : *argv[1]) {
  24.       case 'e': case 'E': what = 0; break;
  25.       case 'd': case 'D': what = 1; break;
  26.       default : fprintf(stderr,"des : illegal option %c \n",c);
  27.               fprintf(stderr,"Usage : %s \n",Usage);
  28.               return 1;
  29.     }
  30.     switch(*++argv[1]) {
  31.       case '2': mode = 2; break;
  32.       case '3': mode = 3; break;
  33.       default : mode = 1;
  34.     }
  35.   } else {
  36.     fprintf(stderr,"Usage : %s \n",Usage);
  37.     return 1;
  38.   }
  39.   if ((buf = (uchar *) malloc(BUFSIZE)) == NULL) {
  40.     fprintf(stderr,"Error: Cannot alloc memory !\n");
  41.     return 1;
  42.   }                                          
  43.   ih =  fopen(argv[2],"rb"); 
  44.   oh = fopen(argv[3],"wb");
  45.   if (ih == NULL || oh == NULL) {
  46.     perror(ih ? argv[3]:argv[2]);
  47.     return 1;
  48.   }
  49.   if (mode == 1) {
  50.     memset(key,0,8);
  51.     len = strlen(argv[4]);
  52.     memcpy(key,argv[4],len > 7 ? 8:len);
  53.     des1init(key);
  54.   } else if (mode == 2) {
  55.     memset(key2,0,16);
  56.     len = strlen(argv[4]);
  57.     memcpy(key2,argv[4],len > 15 ? 16:len);
  58.     des2init(key2);
  59.   } else if (mode == 3) {
  60.     memset(key3,0,24);
  61.     len = strlen(argv[4]);
  62.     memcpy(key3,argv[4],len > 23 ? 24:len);
  63.     des3init(key3);
  64.   }
  65.   t = time(NULL);
  66.   while ((i = fread(buf,1,BUFSIZE,ih)) != 0) {
  67.     for (b=buf; i > 0; i -=8 , b += 8) {
  68.       if (!what) {
  69.         if (i < 8) b[7] = i;
  70.         if (mode == 1) des1encode(b,b);
  71.         else if (mode == 2) des2encode(b,b);
  72.         else if (mode == 3) des3encode(b,b);
  73.         fwrite(b,1,8,oh);
  74.         if (i < 8) fputc(i,oh);
  75.       } else {
  76.         if (mode == 1) des1decode(b,b);
  77.         else if (mode == 2) des2decode(b,b);
  78.         else if (mode == 3) des3decode(b,b);
  79.         if (i == 9) {
  80.           fwrite (b,1,b[7],oh);
  81.           i = 0;
  82.         }
  83.         else fwrite(b,1,8,oh);
  84.       }
  85.     }
  86.   }
  87.   t1 = time(NULL) - t;
  88.   printf("%scrypted bytes per second using %s DES: %ld\n", what ? "De":"En", 
  89.           mode == 1 ? "single" : mode == 2 ? "double" : mode == 3 ? "triple"
  90.           : "unknown", ftell(ih)/(t1 ? t1 : 1));
  91.   fclose(ih);
  92.   fclose(oh);
  93.   return 0;
  94. }
  95.