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

  1. #include <stdio.h>
  2. main()
  3. {
  4.     char key[8],plain[8],cipher[8],answer[8];
  5.     int i;
  6.     int test;
  7.     int fail;
  8.     char ks[16][8];
  9.  
  10.     desinit(0);
  11.  
  12.     for(test=0;!feof(stdin);test++){
  13.  
  14.         get8(key);
  15.         printf(" K: "); put8(key);
  16.         setkey(ks,key);
  17.  
  18.         get8(plain);
  19.         printf(" P: "); put8(plain);
  20.  
  21.         get8(answer);
  22.         printf(" C: "); put8(answer);
  23.  
  24.         for(i=0;i<8;i++)
  25.             cipher[i] = plain[i];
  26.         endes(ks,cipher);
  27.  
  28.         for(i=0;i<8;i++)
  29.             if(cipher[i] != answer[i])
  30.                 break;
  31.         fail = 0;
  32.         if(i != 8){
  33.             printf(" Encrypt FAIL");
  34.             fail++;
  35.         }
  36.         dedes(ks,cipher);
  37.         for(i=0;i<8;i++)
  38.             if(cipher[i] != plain[i])
  39.                 break;
  40.         if(i != 8){
  41.             printf(" Decrypt FAIL");
  42.             fail++;
  43.         }
  44.         if(fail == 0)
  45.             printf(" OK");
  46.         printf("\n");
  47.     }
  48. }
  49. get8(cp)
  50. char *cp;
  51. {
  52.     int i,t;
  53.  
  54.     for(i=0;i<8;i++){
  55.         scanf("%2x",&t);
  56.         if(feof(stdin))
  57.             exit(0);
  58.         *cp++ = t;
  59.     }
  60. }
  61. put8(cp)
  62. char *cp;
  63. {
  64.     int i;
  65.  
  66.     for(i=0;i<8;i++){
  67.         printf("%02x",*cp++ & 0xff);
  68.     }
  69. }
  70.