Advertisement
Guest User

blowfish.c

a guest
Mar 17th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.95 KB | None | 0 0
  1. /*
  2.  * Author     :  Paul Kocher
  3.  * E-mail     :  pck@netcom.com
  4.  * Date       :  1997
  5.  * Description:  C implementation of the Blowfish algorithm.
  6.  */
  7.  
  8. #include "includes.h"
  9. #include "defines.h"
  10. #include "blowfish.h"
  11. #define N               16/* Exchange Xl and Xr */
  12.     temp = Xl;
  13.     Xl = Xr;
  14.     Xr = temp;
  15.   }
  16.  
  17.   /* Exchange Xl and Xr */
  18.   temp = Xl;
  19.   Xl = Xr;
  20.   Xr = temp;
  21.   Xr = Xr ^ ctx->P[1];
  22.   Xl = Xl ^ ctx->P[0];
  23.   *xl = Xl;
  24.   *xr = Xr;
  25. }
  26.  
  27. void Blowfish_Init(BLOWFISH_CTX *ctx, unsigned char *key, int keyLen) {
  28.   int i, j, k;
  29.   uInt32 data, datal, datar;
  30.  
  31.   for (i = 0; i < 4; i++) {
  32.  
  33.     for (j = 0; j < 256; j++)
  34.       ctx->S[i][j] = ORIG_S[i][j];
  35.   }
  36.  
  37.   j = 0;
  38.  
  39.   for (i = 0; i < N + 2; ++i) {
  40.     data = 0x00000000;
  41.  
  42.     for (k = 0; k < 4; ++k) {
  43.       data = (data << 8) | key[j];
  44.       j = j + 1;
  45.       if (j >= keyLen)
  46.         j = 0;
  47.     }
  48.  
  49.     ctx->P[i] = ORIG_P[i] ^ data;
  50.   }
  51.  
  52.   datal = 0x00000000;
  53.   datar = 0x00000000;
  54.  
  55.   for (i = 0; i < N + 2; i += 2) {
  56.     Blowfish_Encrypt(ctx, &datal, &datar);
  57.     ctx->P[i] = datal;
  58.     ctx->P[i + 1] = datar;
  59.   }
  60.  
  61.   for (i = 0; i < 4; ++i) {
  62.  
  63.     for (j = 0; j < 256; j += 2) {
  64.       Blowfish_Encrypt(ctx, &datal, &datar);
  65.       ctx->S[i][j] = datal;
  66.       ctx->S[i][j + 1] = datar;
  67.     }
  68.   }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement