PHP Convert large binary code to string - php

I need to convert a large collection of binary to a string.
The code I used to get the binary code is:
$buffer = file_get_contents('test.php');
$length = filesize('test.php');
if (!$buffer || !$length) {
die("Reading error\n");
}
$_buffer = '';
for ($i = 0; $i < $length; $i++) {
$_buffer .= sprintf("%08b", ord($buffer[$i]));
}
I tried to use base_convert().This gives my the following error message :
<?php
pack('H*', base_convert($_buffer , 2, 16));
?>
Warning: base_convert(): Number too large in
C:\xampp\htdocs\decrypt.php on line 25
Any suggestions?
The binary code is
0011110000111111011100000110100001110000000011010000101000100100011000100111010101100110011001100110010101110010001000000011110100100000011001100110100101101100011001010101111101100111011001010111010001011111011000110110111101101110011101000110010101101110011101000111001100101000001001110111010001100101011100110111010000101110011100000110100001110000001001110010100100111011000011010000101000100100011011000110010101101110011001110111010001101000001000000011110100100000011001100110100101101100011001010111001101101001011110100110010100101000001001110111010001100101011100110111010000101110011100000110100001110000001001110010100100111011000011010000101000001101000010100110100101100110001000000010100000100001001001000110001001110101011001100110011001100101011100100010000001111100011111000010000000100001001001000110110001100101011011100110011101110100011010000010100100100000011110110000110100001010001000000010000001100100011010010110010100101000001000100101001001100101011000010110010001101001011011100110011100100000011001010111001001110010011011110111001001011100011011100010001000101001001110110000110100001010011111010000110100001010000011010000101000100100010111110110001001110101011001100110011001100101011100100010000000111101001000000010011100100111001110110000110100001010011001100110111101110010001000000010100000100100011010010010000000111101001000000011000000111011001000000010010001101001001000000011110000100000001001000110110001100101011011100110011101110100011010000011101100100000001001000110100100101011001010110010100100100000011110110000110100001010001000000010000000100100010111110110001001110101011001100110011001100101011100100010000000101110001111010010000001110011011100000111001001101001011011100111010001100110001010000010001000100101001100000011100001100010001000100010110000100000011011110111001001100100001010000010010001100010011101010110011001100110011001010111001001011011001001000110100101011101001010010010100100111011000011010000101001111101000011010000101001100101011000110110100001101111001000000111000001100001011000110110101100101000001001110100100000101010001001110010110001100010011000010111001101100101010111110110001101101111011011100111011001100101011100100111010000101000001001000101111101100010011101010110011001100110011001010111001000101100001100100010110000110001001101100010100100101001001110110000110100001010011001010110001101101000011011110010000000100100010111110110001001110101011001100110011001100101011100100010111000100010010111000110111000100010001110110000110100001010001001000110011001101001011011000110010100100000001111010010000000100111011101000110010101110011011101000010111001110000011010000111000000100111001110110000110100001010001001000111001101110100011100100110100101101110011001110010000000111101001000000110011001101001011011000110010101011111011001110110010101110100010111110110001101101111011011100111010001100101011011100111010001110011001010000010010001100110011010010110110001100101001010010011101100001101000010100000110100001010011001100110111101110010001010000010010001101100001111010111001101110100011100100110110001100101011011100010100000100100011100110111010001110010011010010110111001100111001010010010110000100000001001000110100100111101001100000011101100100000001001000110100100111100001001000110110000111011001000000010010001101001001010110010101100101001000011010000101001111011000011010000101000100000001000000010000000100000011100000111001001101001011011100111010001100110001010000010011100100101001100000011100001100010001001110010110000100000011011110111001001100100001010000010010001110011011101000111001001101001011011100110011101011011001001000110100101011101001010010010100100111011000011010000101001111101
The output should be :
<?php
$buffer = file_get_contents('test.php');
$length = filesize('test.php');
if (!$buffer || !$length) {
die("Reading error\n");
}
$_buffer = '';
for ($i = 0; $i < $length; $i++) {
$_buffer .= sprintf("%08b", ord($buffer[$i]));
}
echo pack('H*',base_convert($_buffer,2,16));
echo $_buffer."\n";
$file = 'test.php';
$string = file_get_contents($file);
for($l=strlen($string), $i=0; $i<$l; $i++)
{
printf('%08b', ord($string[$i]));
}

Related

PHP Script Runs out of memory

My PHP code is supposed to generate 1000 random words with a random length between 3 and 7 chars. And then it is supposed to compare each word to all the words in my dictionary, engmix.txt, and place the matches into an array and all the nonmatches into another array. I know the code functions correctly, but it runs out of memory on both computers that I have tried running it on. I am using XAMPP as my webserver and even tested removing the memory limit to see if it would run. I would like advice on how to optimize this code.
<?php
ini_set('memory_limit', '-1');
function getRandomWord($len = 10) {
$word = range('a', 'z');
shuffle($word);
return substr(implode($word), 0, $len);
}
$words = array();
for ($i = 0; $i < 300; $i++) {
$words[$i] = getRandomWord(rand(3, 7));
}
$matches = array();
$nonmatches = array();
$k = 0;
$dictionary = file("engmix.txt");
for ($i=0; $i < count($dictionary); $i++) {
for ($j = 0; $j < count($words); $j++) {
if ($dictionary[$i] == $words[$j]) {
$matches[$k] = $words[$j];
$k++;
} else {
$nonmatches[$k] = $words[$j];
$k++;
}
}
}
?>
New fixed code:
<?php
function getRandomWord($len = 10) {
$word = range('a', 'z');
shuffle($word);
return substr(implode($word), 0, $len);
}
$words = array();
for ($i = 0; $i < 300; $i++) {
$words[$i] = getRandomWord(rand(3, 7));
}
$matches = array();
$nonmatches = array();
$file = file("engmix.txt");
$i = 0;
$file_handle = fopen("engmix.txt", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
$line = str_replace("\n", "", $line);
$line = str_replace("\r", "", $line);
for ($i = 0; $i < count($words); $i++) {
if ($line == $words[$i]) {
$matches[] = $words[$i];
}
}
}
fclose($file_handle);
$nonmatches = array_diff($words, $file);
print("<pre>");
print_r($matches);
print("<pre>");
print("<pre>");
print_r($nonmatches);
print("<pre>");
?>

PHP Create an encode algorithm?

function deCode_Me($strContent, $cKey){
$hRet = 0;
$sRet = "";
foreach( str_split($strContent) as $nKey ){
$sRet .= chr(ord($nKey) ^ ord($cKey[$hRet++ % strlen($cKey)]));
}
return $sRet;
}
How can I make the encoder for the above resolver.
the exact opposite?
function encode_Me($strContent, $cKey)
{
$key = $cKey;
$text = $strContent;
$outText = '';
for ($i = 0; $i < strlen($text); $i++)
{
for ($j = 0; $j < strlen($key); $j++, $i++)
{
$outText.= $text
{
$i} ^ $key
{
$j};
}
}
return $outText;
}
worked ! :)

XOR string in PHP with key

I need to XOR a string/text in PHP the base64 encode it, but something goes wrong:
<?php
$mustget = 'Kw4SCQ==';
$string = 'Josh';
echo("Must get: " . $mustget . "\n");
echo("We got: " . base64_encode(xor_this($string)) . "\n");
function xor_this($text) {
$key = 'frtkj';
$i = 0;
$encrypted = '';
foreach (str_split($text) as $char) {
$encrypted .= chr(ord($char) ^ ord($key{$i++ % strlen($key)}));
}
return $encrypted;
}
?>
I get the following result, but I need to get the "$mustget" one:
Must get: Kw4SCQ==
We got: LB0HAw==
What do I do wrong?
$mustget = 'Kw4SCQ==';
$key = 'frtkj';
$key_length = strlen($key);
$encoded_data = base64_decode($mustget);
$result = '';
$length = strlen($encoded_data);
for ($i = 0; $i < $length; $i++) {
$tmp = $encoded_data[$i];
for ($j = 0; $j < $key_length; $j++) {
$tmp = chr(ord($tmp) ^ ord($key[$j]));
}
$result .= $tmp;
}
echo $result; // Josh
http://ideone.com/NSIe7K
I'm sure you can reverse it and create a function, that "crypts" the data ;-)

Generate multiple coupon codes at once; separated by comma

I found this Stack Overflow post explaining how you can generate random coupon codes.
I'm looking into using that code and generate multiple coupons at once (e.g. 50), while separate them by a comma.
The output would be: COUPON-HMECN, COUPON-UYSNC, etc.
Code below and codepad example available.
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$res = "COUPON-";
for ($i = 0; $i < 5; $i++) {
$res .= $chars[mt_rand(0, strlen($chars)-1)];
}
echo $res . ",";
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$numCodesToGenerate = 5;
for ($n = 0; $n < $numCodesToGenerate; $n++)
{
$res = "COUPON-";
for ($i = 0; $i < 5; $i++) {
$res .= $chars[mt_rand(0, strlen($chars)-1)];
}
echo $res . ",";
}
Why not use uniqid()?
$coupon_str = '';
$seperator = '';
for($i = 0; $i < 50; $i++) {
$coupon_str .= $seperator . uniqid('COUPON-');
$seperator = ',';
}
echo $coupon_str;
Output:
COUPON-502373ac95dd2,COUPON-502373ac95de8,COUPON-502373ac95ded,....
Here is a much neater version (and faster) that does what you need:
function MakeCouponCode() {
$res = "COUPON-";
for($i = 0; $i < 5; ++$i)
$res .= chr(mt_rand(0, 1) == 0 ? mt_rand(65, 90) : mt_rand(48, 57));
return $res;
}
$coupons = array();
for($i = 0; $i < 5; ++$i)
$coupons[] = MakeCouponCode();
echo implode(', ', $coupons);
Output:
COUPON-D707Y, COUPON-4B37E, COUPON-3O397, COUPON-M799X, COUPON-24Q36
You can use the coupon code generator PHP class file to generate N number of coupons and its customizable, with various options of adding own mask with own prefix and suffix. The coupon codes are separated by comma. Simple PHP coupon code generator
Example:
coupon::generate(8); // J5BST6NQ

PHP Random Number

I want to generate a random number in PHP where the digits itself should not repeat in that number.
Is that possible?
Can you paste sample code here?
Ex: 674930, 145289. [i.e Same digit shouldn't come]
Thanks
Here is a good way of doing it:
$amountOfDigits = 6;
$numbers = range(0,9);
shuffle($numbers);
for($i = 0;$i < $amountOfDigits;$i++)
$digits .= $numbers[$i];
echo $digits; //prints 217356
If you wanted it in a neat function you could create something like this:
function randomDigits($length){
$numbers = range(0,9);
shuffle($numbers);
for($i = 0;$i < $length;$i++)
$digits .= $numbers[$i];
return $digits;
}
function randomize($len = false)
{
$ints = array();
$len = $len ? $len : rand(2,9);
if($len > 9)
{
trigger_error('Maximum length should not exceed 9');
return 0;
}
while(true)
{
$current = rand(0,9);
if(!in_array($current,$ints))
{
$ints[] = $current;
}
if(count($ints) == $len)
{
return implode($ints);
}
}
}
echo randomize(); //Numbers that are all unique with a random length.
echo randomize(7); //Numbers that are all unique with a length of 7
Something along those lines should do it
<?php
function genRandomString() {
$length = 10; // set length of string
$characters = '0123456789'; // for undefined string
$string ="";
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}
$s = genRandomString(); //this is your random print var
or
function rand_string( $length )
{
$chars = "0123456789";
$size = strlen( $chars );
for( $i = 0; $i < $length; $i++ )
{
$str .= $chars[ rand( 0, $size – 1 ) ];
}
return $str;
}
$rid= rand_string( 6 ); // 6 means length of generate string
?>
$result= "";
$numbers= "0123456789";
$length = 8;
$i = 0;
while ($i < $length)
{
$char = substr($numbers, mt_rand(0, strlen($numbers)-1), 1);
//prevents duplicates
if (!strstr($result, $char))
{
$result .= $char;
$i++;
}
}
This should do the trick. In $numbers you can put any char you want, for example: I have used this to generate random passwords, productcodes etc.
The least amount of code I saw for something like this was:
function random_num($n=5)
{
return rand(0, pow(10, $n));
}
But I'm assuming it requires more processing to do this than these other methods.

Categories