bf128.c

来自「加密解密,安全工具!很有意思的代码」· C语言 代码 · 共 449 行 · 第 1/2 页

C
449
字号
  0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, 
  0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51, 
  0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, 
  0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, 
  0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b, 
  0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, 
  0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, 
  0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd, 
  0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, 
  0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, 
  0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, 
  0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, 
  0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, 
  0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32, 
  0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, 
  0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, 
  0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, 
  0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, 
  0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 
  0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, 
  0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, 
  0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, 
  0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, 
  0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, 
  0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, 
  0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, 
  0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, 
  0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, 
  0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, 
  0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, 
  0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 
  0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525, 
  0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, 
  0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, 
  0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, 
  0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, 
  0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 
  0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, 
  0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, 
  0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, 
  0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, 
  0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, 
  0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, 
  0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, 
  0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, 
  0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, 
  0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, 
  0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, 
  0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 
  0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9, 
  0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, 
  0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6}
};


//************ BLOWFISH ECB mode encryption **************

#define S(x,i) ((KeyAddress->Array.sArray)[i][x.Byte.Byte##i])

#define F(x)   (((S(x,0) + S(x,1)) ^ S(x,2)) + S(x,3))

#define ROUND(a,b,n) (a.Dword ^= F(b) ^ (KeyAddress->Array.pArray[n]))


VOID
Blowfish_ECB_Encrypt( PDWORD Left,
                      PDWORD Right,
		             PBKEY  KeyAddress )
{ union unionDWORD  Xl;
  union unionDWORD  Xr;

  Xl.Dword = *Left;
  Xr.Dword = *Right;

  Xl.Dword ^= KeyAddress->Array.pArray[0];

  ROUND (Xr, Xl, 1);  ROUND (Xl, Xr, 2);
  ROUND (Xr, Xl, 3);  ROUND (Xl, Xr, 4);
  ROUND (Xr, Xl, 5);  ROUND (Xl, Xr, 6);
  ROUND (Xr, Xl, 7);  ROUND (Xl, Xr, 8);
  ROUND (Xr, Xl, 9);  ROUND (Xl, Xr, 10);
  ROUND (Xr, Xl, 11); ROUND (Xl, Xr, 12);
  ROUND (Xr, Xl, 13); ROUND (Xl, Xr, 14);
  ROUND (Xr, Xl, 15); ROUND (Xl, Xr, 16);
  
  Xr.Dword ^= KeyAddress->Array.pArray[17];

  *Right = Xl.Dword;
  *Left  = Xr.Dword;
}


//************ BLOWFISH ECB mode decryption **************

VOID
Blowfish_ECB_Decrypt( PDWORD Left,
                      PDWORD Right,
		              PBKEY  KeyAddress )
{ union unionDWORD  Xl;
  union unionDWORD  Xr;

  Xl.Dword = *Left;
  Xr.Dword = *Right;

  Xl.Dword ^= KeyAddress->Array.pArray[17];

  ROUND (Xr, Xl, 16);  ROUND (Xl, Xr, 15);
  ROUND (Xr, Xl, 14);  ROUND (Xl, Xr, 13);
  ROUND (Xr, Xl, 12);  ROUND (Xl, Xr, 11);
  ROUND (Xr, Xl, 10);  ROUND (Xl, Xr, 9);
  ROUND (Xr, Xl, 8);   ROUND (Xl, Xr, 7);
  ROUND (Xr, Xl, 6);   ROUND (Xl, Xr, 5);
  ROUND (Xr, Xl, 4);   ROUND (Xl, Xr, 3);
  ROUND (Xr, Xl, 2);   ROUND (Xl, Xr, 1);
  
  Xr.Dword ^= KeyAddress->Array.pArray[0];

  *Right = Xl.Dword;
  *Left  = Xr.Dword;
}


//************ BLOWFISH CBC mode encryption **************

VOID
Encrypt( DWORD *IVector, 
         DWORD *KeyAddress,
         DWORD *SrcBuffer,
         DWORD *DstBuffer,
         DWORD Length )    // in bytes
{ DWORD i;
  DWORD left, right;

  left  = IVector[0];
  right = IVector[1];

  for(i = 0; i < (Length >> 2); i = i+2)
    { 
	  // do EBC encryption of (Initial_Vector XOR Data)
      left  = SrcBuffer[i] ^ left;
      right = SrcBuffer[i+1] ^ right;

      Blowfish_ECB_Encrypt( &left, &right, (PBKEY)KeyAddress );
            
      DstBuffer[i]   = left;
      DstBuffer[i+1] = right;	  
    }
}


//************ BLOWFISH CBC mode decryption **************

VOID
Decrypt( DWORD *IVector, 
         DWORD *KeyAddress,
         DWORD *SrcBuffer,
         DWORD *DstBuffer,
         DWORD Length )    // in bytes
{ DWORD i;
  DWORD left, right, ivectorL, ivectorR, oldSrcL, oldSrcR;

  ivectorL = IVector[0];
  ivectorR = IVector[1];

  for(i = 0; i < (Length >> 2); i = i+2)
    { 
      left  = oldSrcL = SrcBuffer[i];
      right = oldSrcR = SrcBuffer[i+1];

	  // Encrypted Data -> new IV, 
	  // then do EBC decryption of Encrypted Data, 
      // then XOR decrypted data with old IV
	  Blowfish_ECB_Decrypt( &left, &right, (PBKEY)KeyAddress );
      
      DstBuffer[i]   = left  ^ ivectorL;
      DstBuffer[i+1] = right ^ ivectorR;	  
      
      ivectorL = oldSrcL;
      ivectorR = oldSrcR;
    }
}

//*********** BLOWFISH Key Extension **************

BOOL
KeyExtend( PUCHAR KeySource,          // 32       8-bit  uchars
           PDWORD KeyDestination )    // (4168/4) 32-bit dwords
{
  PBKEY  pKey = (PBKEY)KeyDestination;
  USHORT i,j;
  DWORD  left = 0, right = 0;
  union  unionDWORD tmp;
 
  if ((!KeySource) || (!KeyDestination)) return FALSE;

  // (1)
  for(i=0; i<18; i++) pKey->Array.pArray[i] = bf_P[i];

  for(i=0; i<4; i++) 
	for(j=0; j<256; j++) pKey->Array.sArray[i][j] = bf_S[i][j];
  
  // (2)
  for(i=0, j=0; i<18; i++)
   { tmp.Dword = 0;
     tmp.Byte.Byte0 = KeySource[j];
     tmp.Byte.Byte1 = KeySource[(j+1)%(KEY_LENGTH / 8)];
     tmp.Byte.Byte2 = KeySource[(j+2)%(KEY_LENGTH / 8)];
     tmp.Byte.Byte3 = KeySource[(j+3)%(KEY_LENGTH / 8)];
     pKey->Array.pArray[i] ^= tmp.Dword;
     j = (j + 4) % (KEY_LENGTH / 8);
   }

  // (3) - (7)
  for(i=0; i<1042; i = i+2)
    { Blowfish_ECB_Encrypt( &left, &right, pKey );
      pKey->Key[i]   = left;
      pKey->Key[i+1] = right;
    }

  return TRUE;
}

char bf128_c[]="$Id: bf128.c,v 1.3 2002/10/29 07:11:46 crypt Rel-1.6-3 $";

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?