2.4.x) for 128-bit Rijndael. // generates a 256-bit sequence key by hashing the passed string with SHA256 // it isn't recommended to use this method, rather, you should generate a random // key with GenerateRandomSequenceKey() instead // returns the sequence key as a hex string function GenerateSequenceKeyFromString($passphrase) { return hash('sha256', $passphrase); } // generates a random 256-bit sequence key // returns the sequence key as a hex string function GenerateRandomSequenceKey() { $randomness = get_loaded_extensions(); $randomness[] = php_uname(); $randomness[] = memory_get_usage(); $randomness = implode(microtime(), $randomness); return hash('sha256', $randomness); } // pack the 128 bit number into a binary string (bcmath style number to binary) function pack128( $num ) { $pack = '' ; while( $num ) { $pack .= chr( bcmod( $num, 256 ) ) ; $num = bcdiv( $num, 256 ) ; } return $pack ; } // unpack the 128 bit integer from a binary string (binary to bcmath style number) function unpack128( $pack ) { $pack = str_split( strrev( $pack )) ; $num = '0' ; foreach( $pack as $char ) { $num = bcmul( $num, 256 ) ; $num = bcadd( $num, ord( $char )) ; } return $num ; } // calculate the number of characters in a crypto block for a character set of given length. function blockchars( $length ) { return floor(128/(log($length, 2))); } // sort the character set function sortchars($charset) { $newchars = str_split($charset,1); sort($newchars); return implode('',$newchars); } // returns lotto numbers function getlotto($key, $code) { $sk = pack("H*", $key); $n_bits = pack128($code); $enc_bits = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $sk, $n_bits, MCRYPT_MODE_ECB, str_repeat( "\0", 16 )); $numdec = unpack128($enc_bits); $chars = ""; for ($i = 0; $i < 5; $i++) { $chars .= bcadd(bcmod($numdec,56),1); if ($i < 4) $chars .= ", "; else $chars .= " / "; $numdec = bcdiv($numdec,56); } $chars .= bcadd(bcmod($numdec,46),1); return $chars; } // returns the nth number (Ex.: Port numbers, etc.) function getnum($key, $code, $codemin, $codemax) { $length = 1+$codemax-$codemin; $sk = pack("H*", $key); $n_bits = pack128($code); $enc_bits = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $sk, $n_bits, MCRYPT_MODE_ECB, str_repeat( "\0", 16 )); $numdec = unpack128($enc_bits); return bcadd(bcmod($numdec,$length),$codemin); } // returns the nth port and code - Based on idea from Hank Beaver in the GRC newsgroups function getportcode($key, $code, $codemin, $codemax) { $codes = array(); $length = 1+$codemax-$codemin; $sk = pack("H*", $key); $n_bits = pack128($code); $enc_bits = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $sk, $n_bits, MCRYPT_MODE_ECB, str_repeat( "\0", 16 )); $numdec = unpack128($enc_bits); array_push($codes, bcadd(bcmod($numdec,$length),$codemin)); $numdec = bcdiv($numdec,$length); $charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $length = strlen($charset); $codelength = 2; $chars = ""; for ($i = 0; $i < $codelength; $i++) { $chars .= substr($charset,bcmod($numdec,$length),1); $numdec = bcdiv($numdec,$length); } array_push($codes, $chars); return $codes; } // returns the nth code function getcode($key, $code, $charset, $codelength) { $charset = sortchars($charset); $length = strlen($charset); $blockchars = blockchars($length); $sk = pack("H*", $key); $n_bits = pack128($code); $enc_bits = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $sk, $n_bits, MCRYPT_MODE_ECB, str_repeat( "\0", 16 )); $numdec = unpack128($enc_bits); $chars = ""; for ($i = 0; $i < $codelength; $i++) { $chars .= substr($charset,bcmod($numdec,$length),1); $numdec = bcdiv($numdec,$length); } return $chars; } // return an array of the codes requested function getcodes($key, $code, $num, $charset, $codelength) { $codes = array(); $first = $code; $last = bcadd($code, $num); $charset = sortchars($charset); $length = strlen($charset); $blockchars = blockchars($length); $sk = pack("H*", $key); for ($h = $first; bccomp($h,$last) < 0; $h = bcadd($h,1)) { $n_bits = pack128($h); $enc_bits = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $sk, $n_bits, MCRYPT_MODE_ECB, str_repeat( "\0", 16 )); $numdec = unpack128($enc_bits); $chars = ""; for ($i = 0; $i < $codelength; $i++) { $chars .= substr($charset,bcmod($numdec,$length),1); $numdec = bcdiv($numdec,$length); } array_push($codes, $chars); } return $codes; } // prints a card in ascii function printcard($key,$charset,$codelength,$cardnum,$title="PPP Card") { printf("%-30.30s%8s\n",$title,"[".$cardnum."]"); $rows = 10; $cols = floor(35/($codelength+1)); $total = $rows*$cols; echo " "; for ($i = 0; $i < $cols; $i++) { echo str_pad(chr(ord("A")+$i), $codelength+1, " ", STR_PAD_BOTH); } echo "\n"; $codes = getcodes($key,bcmul($cardnum,$total),$total,$charset,$codelength); for ($i = 0; $i < $total; $i++) { $code = $codes[$i]; if ($i % $cols == 0) printf("%2s: ",ceil(($i+1)/$cols)); if ($i % $cols < $cols-1) echo "$code "; else echo "$code\n"; } } // prints a card in html function printcardhtml($key,$charset,$codelength,$cardnum,$title="PPP Card") { $rows = 10; $cols = floor(35/($codelength+1)); $total = $rows*$cols; echo "\n"; echo "\n"; echo ""; for ($i = 0; $i < $cols; $i++) { echo ""; } echo "\n"; $codes = getcodes($key,bcmul($cardnum,$total),$total,$charset,$codelength); for ($i = 0; $i < $rows; $i++) { echo ""; for ($j = -1; $j < $cols; $j++) { if ($j == -1) echo ""; else echo ""; } echo "\n"; } echo "
"; echo "PPPv3.1\n$title\n"; echo "[$cardnum]
 ".chr(ord("A")+$i)."
". ($i+1) ."".$codes[$i*$cols+$j]."
\n"; } // prints a card in html, mikeboers.com style function printcardmikeb($key,$charset,$codelength,$cardnum,$title="PPP Card") { $rows = 10; $cols = floor(35/($codelength+1)); $total = $rows*$cols; echo "\n"; echo "\n"; echo ""; for ($i = 0; $i < $cols; $i++) { echo ""; } echo "\n"; $codes = getcodes($key,bcmul($cardnum,$total),$total,$charset,$codelength); for ($i = 0; $i < $rows; $i++) { echo ""; for ($j = -1; $j < $cols; $j++) { if ($j == -1) echo ""; else echo ""; } echo "\n"; } echo "
"; echo "PPPv3.1\n$title\n"; echo "[$cardnum]
 ".chr(ord("A")+$i)."
". ($i+1) ."".$codes[$i*$cols+$j]."
\n"; } if (isset($_GET[6]) && ereg("[0-9]+", $_GET[6]) && $_GET[6] > 1) { $fontsize = $_GET[6]; } else { $fontsize = 14; } ?> PPPv3.1 Card Printout \n"; } else { echo "\n"; } ?> sha256 is available!\n"; } else { echo "

sha256 is not available.

\n"; $allgood = false; } // check bcmath availability if (function_exists('bcadd')) { # echo "
  • bcmath is available!
  • \n"; } else { echo "

    bcmath is not available!

    \n"; $allgood = false; } // check mcrypt/rijndael-128 availability if (function_exists('mcrypt_list_algorithms')) { $mcrypt_algos = mcrypt_list_algorithms(); if (in_array("rijndael-128", $mcrypt_algos)) { # echo "
  • mcrypt support for rijndael-128!
  • \n"; } else { echo "

    This mcrypt does not support rijndael-128!

    \n"; $allgood = false; } } else { echo "

    mcrypt not available.

    \n"; $allgood = false; } // End environment self check. if (isset($_GET[1]) && ereg("[0-9a-hA-H]{64}", $_GET[1])) { $key = strtolower($_GET[1]); } else { echo "

    No key specified

    \n"; echo "

    Try a sample card.

    \n"; $allgood = false; } if ($allgood) { if (isset($_GET[2]) && ereg("[0-9]+", $_GET[2])) { $card = $_GET[2]; } else { $card = 0; } if (isset($_GET[3]) && ereg("[0-9]+", $_GET[3]) && $_GET[3] > 1) { $length = $_GET[3]; } else { $length = 4; } if (isset($_GET[4]) && strlen($_GET[4]) > 1 && strlen($_GET[4]) < 256) { $charset = $_GET[4]; } else { $charset = "23456789!@#%+=:?abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ"; } if (isset($_GET[5]) && strlen($_GET[5]) > 0 && strlen($_GET[5]) < 31) { $label = htmlentities($_GET[5]); } else { $label = ""; } if (isset($_GET["num"]) and ereg("[0-9]+", $_GET["num"]) and ($_GET["num"] >= 1) and ($_GET["num"] <= 10)) { $numcards = $_GET["num"]; } else { $numcards = 3; } if ($style == "plain") { echo "
    ";
    		for ($i = 0; $i < $numcards; $i++) {
    			printcard($key,$charset,$length,$card+$i,$label);
    			echo "\n";
    		}
    		echo "
    \n"; } if ($style == "html") { for ($i = 0; $i < $numcards; $i++) { printcardhtml($key,$charset,$length,$card+$i,$label); } } if ($style == "mikeb" or $style == "html2") { for ($i = 0; $i < $numcards; $i++) { printcardmikeb($key,$charset,$length,$card+$i,$label); } } ?>
    Alternate Styles: ">Plain &style=html">HTML &style=html2">HTML2 &style=mikeb">MikeB
    Previous $numcards Cards"; ?> Next $numcards Cards"; ?>
    ">Smaller ">Larger
    Number of cards ">Fewer ">More