function rowsToTextTable($rows, $header = true, $delimiter = "\t", $max = 0) { //empty result if (!$rows || !is_array($rows) || !count($rows)) { return "\nNo Results\n"; } //header row $lenghts = getMaxColumnLenghts($rows, $header, $max); $result = ""; if ($header) { $sep = "\n"; $headerRow = reset($rows); foreach ($headerRow as $key => $value) { $result .= $sep . setLength($key, $lenghts[$key]); $sep = $delimiter; } } // output data of each row foreach ($rows as $row) { $sep = "\n"; foreach ($row as $key => $value) { $result .= $sep . setLength($value, $lenghts[$key]); $sep = $delimiter; } } $result .= "\n\n"; return $result; }
function AESDecryptCtr($ciphertext, $password, $nBits) { if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { return ''; // standard allows 128/192/256 bit keys } $nBytes = $nBits / 8; // no bytes in key setLength($pwBytes, $nBytes); for ($i = 0; $i < $nBytes; $i++) { $pwBytes[$i] = ord($password[$i]) & 0xff; } $pwKeySchedule = KeyExpansion($pwBytes); $key = Cipher($pwBytes, $pwKeySchedule); $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // key is now 16/24/32 bytes long $keySchedule = KeyExpansion($key); $ciphertext = explode('-', $ciphertext); // split ciphertext into array of block-length strings // recover nonce from 1st element of ciphertext $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES setLength($counterBlock, $blockSize); $ctrTxt = unescCtrlChars($ciphertext[0]); for ($i = 0; $i < 8; $i++) { $counterBlock[$i] = ord($ctrTxt[$i]); } setLength($plaintext, count($ciphertext) - 1); for ($b = 1; $b < count($ciphertext); $b++) { // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) for ($c = 0; $c < 4; $c++) { $counterBlock[15 - $c] = zeroFill($b - 1, $c * 8) & 0xff; } for ($c = 0; $c < 4; $c++) { $counterBlock[15 - $c - 4] = zeroFill($b / 0x100000000 - 1, $c * 8) & 0xff; } $cipherCntr = Cipher($counterBlock, $keySchedule); // encrypt counter block $ciphertext[$b] = unescCtrlChars($ciphertext[$b]); $pt = ''; for ($i = 0; $i < strlen($ciphertext[$b]); $i++) { // -- xor plaintext with ciphered counter byte-by-byte -- $ciphertextByte = ord($ciphertext[$b][$i]); $plaintextByte = $ciphertextByte ^ $cipherCntr[$i]; $pt .= chr($plaintextByte); } // pt is now plaintext for this block $plaintext[$b - 1] = $pt; // b-1 'cos no initial nonce block in plaintext } return implode('', $plaintext); }
function fromNumber($a, $b, $c) { if ("number" == gettype($b)) { // new BigInteger(int,int,RNG) if ($a < 2) { $this->fromInt(1); } else { $this->fromNumber($a, $c); if (!$this->testBit($a - 1)) { // force MSB set $this->bitwiseTo($GLOBALS['BigIntegerOne']->shiftLeft($a - 1), op_or, $this); } if ($this->isEven()) { $this->dAddOffset(1, 0); // force odd } while (!$this->isProbablePrime($b)) { $this->dAddOffset(2, 0); if ($this->bitLength() > $a) { $this->subTo($GLOBALS['BigIntegerOne']->shiftLeft($a - 1), $this); } } } } else { // new BigInteger(int,RNG) $x = array(); $t = $a & 7; //$x.length = ($a>>3)+1; setLength($x, ($a >> 3) + 1); $b->nextBytes($x); if ($t > 0) { $x[0] &= (1 << $t) - 1; } else { $x[0] = 0; } $this->fromString($x, 256); } }