> (7 - $j)) & 1; } } return $bits; } function cifs_des_desfunc($r, $k) { global $des_permute_e, $des_sbox, $des_sbox_permute; $r_e = cifs_des_permute($r, $des_permute_e); $r_e_plus_k = array(); for($i = 0; $i < sizeof($r_e); $i++) { $r_e_plus_k[$i] = ($r_e[$i] + $k[$i]) % 2; } $output = array(); for($box = 0; $box < 8; $box++) { $i = ($r_e_plus_k[$box*6] << 1) | ($r_e_plus_k[$box*6+5]); $j = ($r_e_plus_k[$box*6+1] << 3) | ($r_e_plus_k[$box*6+2] << 2) | ($r_e_plus_k[$box*6+3] << 1) | ($r_e_plus_k[$box*6+4]); for($sbox_bit = 0; $sbox_bit < 4; $sbox_bit++) { $sbox_output_bit = ($des_sbox[$box][$i][$j] & (1<<(3-$sbox_bit))) ? 1 : 0; $output[$box*4+$sbox_bit] = $sbox_output_bit; } } $result = cifs_des_permute($output, $des_sbox_permute); return $result; } function cifs_des($message, $key) { global $des_shifts, $des_permute1, $des_permute2, $des_initial_permute, $des_final_permute; $keybits = cifs_des_str2bits($key); $pkey = cifs_des_permute($keybits, $des_permute1); $c = array(); $c[0] = array(); $d = array(); $d[0] = array(); $k = array(); $k[0] = array(); for($i = 0; $i < 28; $i++) { $c[0][$i] = $pkey[$i]; $d[0][$i] = $pkey[$i+28]; } for($i = 1; $i < sizeof($des_shifts); $i++) { $c[$i] = cifs_des_rotate($c[$i-1], $des_shifts[$i]); $d[$i] = cifs_des_rotate($d[$i-1], $des_shifts[$i]); $inkey = $c[$i]; for($j = 0; $j < sizeof($d[$i]); $j++) { $inkey[sizeof($c[$i])+$j] = $d[$i][$j]; } $k[$i] = cifs_des_permute($inkey, $des_permute2); } $msgbits = cifs_des_str2bits($message); $pmessage = cifs_des_permute($msgbits, $des_initial_permute); $l = array(); $r = array(); $l[0] = array(); $r[0] = array(); for($i = 0; $i < 32; $i++) { $l[0][$i] = $pmessage[$i]; $r[0][$i] = $pmessage[32+$i]; } for($i = 1; $i <= 16; $i++) { $l[$i] = $r[$i - 1]; $f = cifs_des_desfunc($r[$i-1], $k[$i]); for($j = 0; $j < sizeof($l[$i-1]); $j++) { $r[$i][$j] = ($l[$i-1][$j] + $f[$j]) % 2; } } $lastblock = array(); for($i = 0; $i < 32; $i++) { $lastblock[$i] = $r[16][$i]; $lastblock[$i+32] = $l[16][$i]; } $cipherbits = cifs_des_permute($lastblock, $des_final_permute); $ciphertext = ""; for($i = 0; $i < sizeof($cipherbits)/8; $i++) { $cipherbyte = 0; for($j = 0; $j < 8; $j++) { $cipherbyte |= ($cipherbits[$i*8+$j] << (7 - $j)); } $ciphertext .= chr($cipherbyte); } return $ciphertext; } ?>