home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 509.lha / DES / src / descert.c < prev    next >
C/C++ Source or Header  |  1991-05-06  |  887b  |  69 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.  
  9.     desinit(0);
  10.  
  11.     for(test=0;!feof(stdin);test++){
  12.  
  13.         get8(key);
  14.         printf(" K: "); put8(key);
  15.         setkey(key);
  16.  
  17.         get8(plain);
  18.         printf(" P: "); put8(plain);
  19.  
  20.         get8(answer);
  21.         printf(" C: "); put8(answer);
  22.  
  23.         for(i=0;i<8;i++)
  24.             cipher[i] = plain[i];
  25.         endes(cipher);
  26.  
  27.         for(i=0;i<8;i++)
  28.             if(cipher[i] != answer[i])
  29.                 break;
  30.         fail = 0;
  31.         if(i != 8){
  32.             printf(" Encrypt FAIL");
  33.             fail++;
  34.         }
  35.         dedes(cipher);
  36.         for(i=0;i<8;i++)
  37.             if(cipher[i] != plain[i])
  38.                 break;
  39.         if(i != 8){
  40.             printf(" Decrypt FAIL");
  41.             fail++;
  42.         }
  43.         if(fail == 0)
  44.             printf(" OK");
  45.         printf("\n");
  46.     }
  47. }
  48. get8(cp)
  49. char *cp;
  50. {
  51.     int i,t;
  52.  
  53.     for(i=0;i<8;i++){
  54.         scanf("%2x",&t);
  55.         if(feof(stdin))
  56.             exit(0);
  57.         *cp++ = t;
  58.     }
  59. }
  60. put8(cp)
  61. char *cp;
  62. {
  63.     int i;
  64.  
  65.     for(i=0;i<8;i++){
  66.         printf("%02x",*cp++ & 0xff);
  67.     }
  68. }
  69.