home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / APR94_1.ZIP / FISH.ASC < prev    next >
Text File  |  1994-03-08  |  4KB  |  199 lines

  1. _THE BLOWFISH ENCRYPTION ALGORITHM_
  2. by Bruce Schneier
  3.  
  4.  
  5. Listing One
  6.  
  7. /* Blowfish.h */
  8.  
  9. #define MAXKEYBYTES 56              /* 448 bits */
  10.  
  11. short opensubkeyfile(void);
  12. unsigned long F(unsigned long x);
  13. void Blowfish_encipher(unsigned long *xl, unsigned long *xr);
  14. void Blowfish_decipher(unsigned long *xl, unsigned long *xr);
  15. short InitializeBlowfish(char key[], short keybytes);
  16.  
  17.  
  18. Listings Two 
  19.  
  20. /* Blowfish.c */
  21.  
  22. #include <dos.h>
  23. #include <graphics.h>
  24. #include <io.h>
  25. #include <math.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <time.h>
  29. #include <alloc.h>
  30. #include <ctype.h>
  31. #include <dir.h>
  32. #include <bios.h>
  33. #include <Types.h>
  34.  
  35. #define  N  16
  36. #define  noErr 0
  37. #define  DATAERROR -1
  38. #define  KEYBYTES 8
  39. #define  subkeyfilename "Blowfish.dat"
  40.  
  41. static unsigned long P[18];
  42. static unsigned long S[4,256];
  43. static FILE*         SubkeyFile;
  44.  
  45. short opensubkeyfile(void) /* read only */
  46. {
  47.    short error;
  48.    error = noErr;
  49.    if((SubkeyFile = fopen(subkeyfilename,"rb")) == NULL) {
  50.       error = DATAERROR;
  51.    }
  52.    return error;
  53. }
  54. unsigned long F(unsigned long x)
  55. {
  56.    unsigned short a;
  57.    unsigned short b;
  58.    unsigned short c;
  59.    unsigned short d;
  60.    unsigned long  y;
  61.    
  62.    d = x & 0x00FF;
  63.    x >>= 8;
  64.    c = x & 0x00FF;
  65.    x >>= 8;
  66.    b = x & 0x00FF;
  67.    x >>= 8;
  68.    a = x & 0x00FF;
  69.  
  70.     y = ((S[0,a] + S[1,b]) ^ S[2,c]) + S[3,d]
  71.    return y;
  72. }
  73. void Blowfish_encipher(unsigned long *xl, unsigned long *xr)
  74. {
  75.    unsigned long  Xl;
  76.    unsigned long  Xr;
  77.    unsigned long  temp;
  78.    short          i;
  79.  
  80.    Xl = *xl;
  81.    Xr = *xr;
  82.    for (i = 0; i < N; ++i) {
  83.       Xl = Xl ^ P[i];
  84.       Xr = F(Xl) ^ Xr;
  85.  
  86.       temp = Xl;
  87.       Xl = Xr;
  88.       Xr = temp;
  89.    }
  90.    temp = Xl;
  91.    Xl = Xr;
  92.    Xr = temp;
  93.  
  94.    Xr = Xr ^ P[N];
  95.    Xl = Xl ^ P[N + 1];
  96.    
  97.    *xl = Xl;
  98.    *xr = Xr;
  99. }
  100. void Blowfish_decipher(unsigned long *xl, unsigned long *xr)
  101. {
  102.    unsigned long  Xl;
  103.    unsigned long  Xr;
  104.    unsigned long  temp;
  105.    short          i;
  106.  
  107.    Xl = *xl;
  108.    Xr = *xr;
  109.  
  110.    for (i = N + 1; i > 1; --i) {
  111.       Xl = Xl ^ P[i];
  112.       Xr = F(Xl) ^ Xr;
  113.  
  114.       /* Exchange Xl and Xr */
  115.       temp = Xl;
  116.       Xl = Xr;
  117.       Xr = temp;
  118.    }
  119.    /* Exchange Xl and Xr */
  120.    temp = Xl;
  121.    Xl = Xr;
  122.    Xr = temp;
  123.  
  124.    Xr = Xr ^ P[1];
  125.    Xl = Xl ^ P[0];
  126.    
  127.    *xl = Xl;
  128.    *xr = Xr;
  129. }
  130. short InitializeBlowfish(char key[], short keybytes)
  131. {
  132.    short          i;
  133.    short          j;
  134.    short          k;
  135.    short          error;
  136.    short          numread;
  137.    unsigned long  data;
  138.    unsigned long  datal;
  139.    unsigned long  datar;
  140.    /* First, open the file containing the array initialization data */
  141.    error = opensubkeyfile();
  142.    if (error == noErr) {
  143.       for (i = 0; i < N + 1; ++i) {
  144.          numread = fread(&data, 4, 1, SubkeyFile);
  145.          printf("%d : %d : %.4s\n", numread, i, &data);
  146.          if (numread != 1) {
  147.             return DATAERROR;
  148.          } else {
  149.             P[i] = data;
  150.          }
  151.       }
  152.       for (i = 0; i < 4; ++i) {
  153.          for (j = 0; j < 256; ++j) {
  154.             numread = fread(&data, 4, 1, SubkeyFile);
  155.             printf("[%d, %d] : %.4s\n", i, j, &data);
  156.             if (numread != 1) {
  157.                return DATAERROR;
  158.             } else {
  159.                S[i, j] = data;
  160.             }
  161.          }
  162.       }
  163.       fclose(SubkeyFile);
  164.       j = 0;
  165.       for (i = 0; i < 18; ++i) {
  166.          data = 0x00000000;
  167.          for (k = 0; k < 4; ++k) {
  168.             data = (data << 8) | key[j];
  169.             j = j + 1;
  170.             if (j > keybytes) {
  171.                j = 0;
  172.             }
  173.          }
  174.          P[i] = P[i] ^ data;
  175.       }
  176.       datal = 0x00000000;
  177.       datar = 0x00000000;
  178.       for (i = 0; i < 18; i += 2) {
  179.          Blowfish_encipher(&datal, &datar);
  180.  
  181.          P[i] = datal;
  182.          P[i + 1] = datar;
  183.       }
  184.       for (j = 0; i < 4; ++j) {
  185.          for (i = 0; i < 256; i += 2) {
  186.             Blowfish_encipher(&datal, &datar);
  187.    
  188.             S[j, i] = datal;
  189.             S[j, i + 1] = datar;
  190.          }
  191.       }
  192.    } else {
  193.       printf("Unable to open subkey initialization file : %d\n", error);
  194.    }
  195.    return error;
  196. }
  197.  
  198.  
  199.