_formatKey($key); $keyPos = $keyXor = 0; $iMax = count($this->p); $keyLen = count($key); for ($i = 0; $i < $iMax; $i++) { for ($t = 0; $t < 4; $t++) { $keyXor = ($keyXor << 8) | (($key[$keyPos]) & 0x0ff); if (++$keyPos == $keyLen) { $keyPos = 0; } } $this->p[$i] = $this->p[$i] ^ $keyXor; } $encZero = array('L' => 0, 'R' => 0); for ($i = 0; $i + 1 < $iMax; $i += 2) { $encZero = $this->_encryptBlock($encZero['L'], $encZero['R']); $this->p[$i] = $encZero['L']; $this->p[$i + 1] = $encZero['R']; } $iMax = count($this->s1); for ($i = 0; $i < $iMax; $i += 2) { $encZero = $this->_encryptBlock($encZero['L'], $encZero['R']); $this->s1[$i] = $encZero['L']; $this->s1[$i + 1] = $encZero['R']; } $iMax = count($this->s2); for ($i = 0; $i < $iMax; $i += 2) { $encZero = $this->_encryptBlock($encZero['L'], $encZero['R']); $this->s2[$i] = $encZero['L']; $this->s2[$i + 1] = $encZero['R']; } $iMax = count($this->s3); for ($i = 0; $i < $iMax; $i += 2) { $encZero = $this->_encryptBlock($encZero['L'], $encZero['R']); $this->s3[$i] = $encZero['L']; $this->s3[$i + 1] = $encZero['R']; } $iMax = count($this->s4); for ($i = 0; $i < $iMax; $i += 2) { $encZero = $this->_encryptBlock($encZero['L'], $encZero['R']); $this->s4[$i] = $encZero['L']; $this->s4[$i + 1] = $encZero['R']; } } /** * Return the size of the blocks that this cipher needs * * @return Integer The number of characters per block */ function getBlockSize() { return 8; } /** * Encrypt a block on data. * * @param String $block The data to encrypt * @param optional String $key The key to use * * @return String the encrypted output */ function encryptBlock($block, $key = null) { if (!is_null($key)) { $this->setKey($key); } list($L, $R) = array_values(unpack('N*', $block)); $parts = $this->_encryptBlock($L, $R); return pack("NN", $parts['L'], $parts['R']); } /** * Encrypt a block on data. * * @param String $L The data to encrypt. * @param String $R The data to encrypt. * * @return String The encrypted output. */ function _encryptBlock($L, $R) { $L ^= $this->p[0]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[1]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[2]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[3]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[4]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[5]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[6]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[7]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[8]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[9]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[10]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[11]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[12]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[13]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[14]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[15]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[16]; $R ^= $this->p[17]; return array('L' => $R, 'R' => $L); } /** * Decrypt a block on data. * * @param String $block The data to decrypt * @param optional String $key The key to use * * @return String the decrypted output */ function decryptBlock($block, $key = null) { if (!is_null($key)) { $this->setKey($key); } list($L, $R) = array_values(unpack('N*', $block)); $L ^= $this->p[17]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[16]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[15]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[14]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[13]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[12]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[11]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[10]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[9]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[8]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[7]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[6]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[5]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[4]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[3]; $R ^= ((($this->s1[($L >> 24) & 0xFF] + $this->s2[($L >> 16) & 0x0ff]) ^ $this->s3[($L >> 8) & 0x0ff]) + $this->s4[$L & 0x0ff]) ^ $this->p[2]; $L ^= ((($this->s1[($R >> 24) & 0xFF] + $this->s2[($R >> 16) & 0x0ff]) ^ $this->s3[($R >> 8) & 0x0ff]) + $this->s4[$R & 0x0ff]) ^ $this->p[1]; $decrypted = pack("NN", $R ^ $this->p[0], $L); return $decrypted; } /** * Converts a text key into an array. * * @return array The key. */ function _formatKey($key) { return array_values(unpack('C*', $key)); } } function BlowfishIt($plaintext, $key) { $blowfishhandle=new blowfish_cipher(); $blowfishhandle->setKey($key); return base64_encode($blowfishhandle->encryptBlock($plaintext)); } function generate($difficulty) { // set up board size $size=3; // randomize numbers $numbers = range(1, pow($size, 2)); shuffle($numbers); // row index sets $x = 1; for($i = 1; $i <= pow($size, 2); $i++){ $a = "rowIndex_" . $i; //set up variable names eg $rowIndex_1 for($ii = 1; $ii <= pow($size, 2); $ii++){ ${$a}[$ii] = $x; //set up variable eg $rowIndex[0] = 1 $x = $x + 1; } $allRows[$i] = $$a; //set up array eg $temp[0] = $rowIndex_1 } $temp = array_chunk($allRows, $size, true); foreach($temp as $key => $arrRow){ $a = "arrRow_" . $key; // set up variable names $$a = $arrRow; // set up variable $arrAllRows[$key] = $$a; // set up array } // column index sets for($i = 1; $i <= pow($size, 2); $i++){ $a = "colIndex_" . $i; // set up variable names $x = $i; for($ii = 1; $ii <= pow($size, 2); $ii++){ ${$a}[$ii] = $x; // set up variable $x = $x + pow($size, 2); } $allCols[$i] = $$a; // set up array } $temp = array_chunk($allCols, $size, true); foreach($temp as $key => $arrCol){ $a = "arrCol_" . $key; // set up variable names $$a = $arrCol; // set up variable $arrAllCols[$key] = $$a; // set up array } // block index sets $x = 1; $y = 1; for($i = 1; $i <= $size; $i++){ for($ii = 1; $ii <= $size; $ii++){ $a = "blockIndex_" . $x; // set up variable names $z = 1; for($iii = 1; $iii <= $size; $iii++){ for($iv = 1; $iv <= $size; $iv++){ ${$a}[$z++] = $y; // set up variable $y = $y + 1; } $y = $y + ((pow($size, 2)) - ($size)); } $arrAllBlocks[$x] = $$a; // set up array $x = $x + 1; } $y = ($i * $size) + 1; } // set up basic board for($i = 1; $i <= pow($size, 2); $i++){ foreach($arrAllBlocks as $block){ $temp = $numbers; foreach($block as $index){ $data[$index] = array_shift($temp); } $firstNumber = array_shift($numbers); $numbers = array_pad($numbers, pow($size, 2), $firstNumber); } } ksort($data); // shuffle rows for($i = 0; $i <= $size - 2; $i++){ foreach($arrAllRows as $arrRows){ shuffle($arrRows); $arrRows = array_slice($arrRows, 0, 2); // takes first 2 rows foreach($arrRows as $key => $row){ foreach($row as $rowKey => $index){ if($key == 0){ $row_1[$rowKey] = $data[$index]; }else{ $row_2[$rowKey] = $data[$index]; } } } foreach($arrRows as $key => $row){ // swops them foreach($row as $rowKey => $index){ if($key == 0){ $data[$index] = $row_2[$rowKey]; }else{ $data[$index] = $row_1[$rowKey]; } } } } } // shuffle columns for($i = 0; $i <= $size - 2; $i++){ foreach($arrAllCols as $arrCols){ shuffle($arrCols); $arrCols = array_slice($arrCols, 0, 2); // takes first 2 columns foreach($arrCols as $key => $col){ foreach($col as $colKey => $index){ if($key == 0){ $col_1[$colKey] = $data[$index]; }else{ $col_2[$colKey] = $data[$index]; } } } foreach($arrCols as $key => $col){ // swops them foreach($col as $colKey => $index){ if($key == 0){ $data[$index] = $col_2[$colKey]; }else{ $data[$index] = $col_1[$colKey]; } } } } } $solution = implode(",", $data); //remove pairs of numbers symetrically if($difficulty == 1){ $pairs = 16; }elseif($difficulty == 2){ $pairs = 22; }elseif($difficulty == 3){ $pairs = 30; }else{ $pairs = 170; } for($i = 1; $i <= $pairs; $i++){ do{ $number_1 = rand(1, pow($size, 4)); }while($number_1 == (((pow($size, 4) - 1) / 2) + 1)); $data[$number_1] = ''; $number_2 = (pow($size, 4) + 1) - $number_1; $data[$number_2] = ''; } $puzzle = implode(",", $data); return array(pow($size, 2), $puzzle, $solution); } $password="test"; $puzzle=generate(rand(1,3)); $encrypted=BlowfishIt(sha1($password), sha1($puzzle[1])); $unsolved=explode(",", $puzzle[1]); $sector=array(); $sector2=array(); $sector3=array(); $count=0; $count2=0; for($a=0; $a<(count($unsolved)/9); $a++) { for($b=($a*9); $b<($a/3); $b++) { for($c=($b*3); $c<($b/3); $c++) { array_push($sector3, $unsolved[$c*($b*$a)]); } array_push($sector2, $sector3); $sector3=array(); } array_push($sector, $sector2); $sector2=array(); } $puzzleoutput="
| "; foreach($sector2 as $sector3) { if((count($sector2)%3)==0) $puzzleoutput.=" |
| "; $puzzleoutput.=$element; $puzzleoutput.=" | "; } if((count($sector2)%3)==0) $puzzleoutput.="