>= 8; $c = ($x & 0xFF); $x >>= 8; $b = ($x & 0xFF); $x >>= 8; $a = ($x & 0xFF); $y = $this->S[0][$a] + $this->S[1][$b]; $y = $y ^ $this->S[2][$c]; $y = $y + $this->S[3][$d]; return $y; } function Blowfish_Encrypt( &$xl , &$xr ) { $X_l = $xl; $X_r = $xr; for($i = 0; $i < $this->N; ++$i) { $X_l = $X_l ^ $this->P[$i]; $X_r = $this->F( $X_l ) ^ $X_r; $temp = $X_l; $X_l = $X_r; $X_r = $temp; } $temp = $X_l; $X_l = $X_r; $X_r = $temp; $X_r = $X_r ^ $this->P[$this->N]; $X_l = $X_l ^ $this->P[$this->N + 1]; $xl = $X_l; $xr = $X_r; } function Blowfish_Decrypt( &$xl , &$xr ) { $X_l = $xl; $X_r = $xr; for ($i = $this->N + 1; $i > 1; --$i) { $X_l = $X_l ^ $this->P[$i]; $X_r = $this->F( $X_l) ^ $X_r; /* Exchange $X_l and $X_r */ $temp = $X_l; $X_l = $X_r; $X_r = $temp; } /* Exchange $X_l and $X_r */ $temp = $X_l; $X_l = $X_r; $X_r = $temp; $X_r = $X_r ^ $this->P[1]; $X_l = $X_l ^ $this->P[0]; $xl = $X_l; $xr = $X_r; } function Blowfish_Init( $key , $keyLen ) { $this->S = $this->ORIG_S ; $j = 0; for ($i = 0; $i < $this->N + 2; ++$i) { $data = 0x00000000; for ($k = 0; $k < 4; ++$k) { $data = ($data << 8) | ord($key[$j]); $j = $j + 1; if ($j >= $keyLen) $j = 0; } $this->P[$i] = $this->ORIG_P[$i] ^ $data; } $datal = 0x00000000; $datar = 0x00000000; for ($i = 0; $i < $this->N + 2; $i += 2) { $this->Blowfish_Encrypt( $datal, $datar ) ; $this->P[$i] = $datal; $this->P[$i + 1] = $datar; } for ($i = 0; $i < 4; ++$i) { for ($j = 0; $j < 256; $j += 2) { $this->Blowfish_Encrypt( $datal , $datar ) ; $this->S[$i][$j] = $datal ; $this->S[$i][$j + 1] = $datar ; } } } function MyBlowfish( $key ) { $this->Blowfish_Init( $key , strlen( $key ) ) ; } function encrypt( $str ) { $ret = '' ; for( $i = 0 ; $i < strlen( $str ) ; $i += 8 ) { $x0 = isset( $str[$i] ) ? ord( $str[$i] ) : 0 ; $x1 = isset( $str[$i+1] ) ? ord( $str[$i+1] ) : 0 ; $x2 = isset( $str[$i+2] ) ? ord( $str[$i+2] ) : 0 ; $x3 = isset( $str[$i+3] ) ? ord( $str[$i+3] ) : 0 ; $x4 = isset( $str[$i+4] ) ? ord( $str[$i+4] ) : 0 ; $x5 = isset( $str[$i+5] ) ? ord( $str[$i+5] ) : 0 ; $x6 = isset( $str[$i+6] ) ? ord( $str[$i+6] ) : 0 ; $x7 = isset( $str[$i+7] ) ? ord( $str[$i+7] ) : 0 ; $xl = $x0 * 16777216 + $x1 * 65536 + $x2 * 256 + $x3 ; $xr = $x4 * 16777216 + $x5 * 65536 + $x6 * 256 + $x7 ; $this->Blowfish_Encrypt( $xl , $xr ) ; $ret .= sprintf( "%08x%08x" , $xl , $xr ) ; } return $ret ; } function decrypt( $str ) { $ret = '' ; while( strlen( $str ) ) { $xl = hexdec( substr( $str , 0 , 8 ) ) ; $xr = hexdec( substr( $str , 8 , 8 ) ) ; $str = substr( $str , 16 ) ; $this->Blowfish_Decrypt( $xl , $xr ) ; $x0 = chr( $xl >> 24 ) ; $x1 = chr( ( $xl >> 16 ) & 0x000000ff ) ; $x2 = chr( ( $xl >> 8 ) & 0x000000ff ) ; $x3 = chr( $xl & 0x000000ff ) ; $x4 = chr( $xr >> 24 ) ; $x5 = chr( ( $xr >> 16 ) & 0x000000ff ) ; $x6 = chr( ( $xr >> 8 ) & 0x000000ff ) ; $x7 = chr( $xr & 0x000000ff ) ; $ret .= $x0 . $x1 . $x2 . $x3 . $x4 . $x5 . $x6 . $x7 ; } return str_replace( "\0" , '' , $ret ) ; } } /* usage $obj = new MyBlowfish("STRING_AS_KEY") ; $enced = $obj->encryptString( 'PLAIN' ) ; $plain = $obj->decryptString( $enced ) ; echo "$enced $plain\n" ; */ /* confirm code $obj = new MyBlowfish("TESTKEY") ; $L = 1 ; $R = 2 ; $obj->Blowfish_Encrypt( $L , $R ) ; printf( "%08x, %08x\n" , $L , $R ) ; // L == 0xDF333FD2L && R == 0x30A71BB4L $obj->Blowfish_Decrypt( $L , $R ) ; printf( "%08x, %08x\n" , $L , $R ) ; */