home *** CD-ROM | disk | FTP | other *** search
/ Boldly Go Collection / version40.iso / TS / 17A / DES_1991.ZIP / DESCALC.C < prev    next >
C/C++ Source or Header  |  1991-06-21  |  2KB  |  93 lines

  1. /* DES "desk calculator"
  2.  * Phil Karn
  3.  * January 1987
  4.  */
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. main()
  8. {
  9.     char key[8],work[8];
  10.     char line[80];
  11.     int keyset = 0;
  12.     char ks[16][8];
  13.  
  14.     if(desinit(0) == -1){
  15.         printf("DES initialization failed\n");
  16.         exit(1);
  17.     }
  18.     printf("Enter in hexadecimal:\nk <key>\np <plaintext>\nc <ciphertext>\n");
  19.     printf("s - standard DES mode\n");
  20.     printf("f - fast mode (no IP)\n"); 
  21.     for(;;){
  22.         gets(line);
  23.         if(feof(stdin))
  24.             break;
  25.         if(isupper(line[0]))
  26.             line[0] = tolower(line[0]);
  27.         switch(line[0]){
  28.         case 's':
  29.             desdone();
  30.             desinit(0);
  31.             if(keyset)
  32.                 setkey(ks,key);
  33.             break;
  34.         case 'f':
  35.             desdone();
  36.             desinit(1);
  37.             if(keyset)
  38.                 setkey(ks,key);
  39.             break;
  40.         case 'k':    /* Set key */
  41.             get8(&line[1],key);
  42.             setkey(ks,key);
  43.             keyset = 1;
  44.             break;
  45.         case 'c':    /* Decrypt ciphertext */
  46.             if(!keyset){
  47.                 printf("Enter key\n");
  48.                 break;
  49.             }
  50.             get8(&line[1],work);
  51.             dedes(ks,work);
  52.             printf("Plaintext: ");
  53.             put8(work);
  54.             printf("\n");
  55.             break;
  56.         case 'p':    /* Encrypt plaintext */
  57.             if(!keyset){
  58.                 printf("Enter key\n");
  59.                 break;
  60.             }
  61.             get8(&line[1],work);
  62.             endes(ks,work);
  63.             printf("Ciphertext: ");
  64.             put8(work);
  65.             printf("\n");
  66.             break;
  67.         default:
  68.             printf("eh?\n");
  69.             break;
  70.         }
  71.     }
  72. }
  73. get8(buf,cp)
  74. char *buf;
  75. register char *cp;
  76. {
  77.     int ikey[8],i;
  78.  
  79.     sscanf(buf,"%2x%2x%2x%2x%2x%2x%2x%2x",&ikey[0],&ikey[1],&ikey[2],
  80.         &ikey[3],&ikey[4],&ikey[5],&ikey[6],&ikey[7]);
  81.     for(i=0;i<8;i++)
  82.         *cp++ = ikey[i];
  83. }
  84. put8(cp)
  85. register char *cp;
  86. {
  87.     int i;
  88.  
  89.     for(i=0;i<8;i++){
  90.         printf("%02x ",*cp++ & 0xff);
  91.     }
  92. }
  93.