dechex(ord($text[$i])) doesn't work witch new lines...

Discussion in 'PHP' started by ignas2526, Sep 14, 2008.

  1. #1
    Hello,
    i have code what converts html to javascript encoded code:
    function javascriptencode($text)
    {
    	$output = '';
    	for ($i = 0; $i < strlen($text); $i++)
    	$output .= '%' . dechex(ord($text[$i]));
    	$crypto = strtoupper($output);
    	return $crypto;
    }
    PHP:
    and i using it:
    
    	$txt = "
            text
            text
            text
    txt
    	" ;
    	header( "Content-type: application/javascript" ) ;
    	echo javascriptencode($txt) ;
    PHP:
    The output will be:
    %A%20%20%20%20%20%20%20%20%74%65%78%74%A%20%20%20%20%20%20%20%20%74%65%78%74%A%20%20%20%20%20%20%20%20%74%65%78%74%A%74%78%74%A%9
    Code (markup):
    Wan it must be:
    %0A%20%20%20%20%20%20%20%20%74%65%78%74%0A%20%20%20%20%20%20%20%20%74%65%78%74%0A%20%20%20%20%20%20%20%20%74%65%78%74%0A%74%78%74%0A%09
    Code (markup):
    Now how javascript decodes it, first what was generated by php:
    %A        text%A        text%A        text%Atxt%A%9
    Code (markup):
    And second:
    
            text
            text
            text
    txt
    	
    Code (markup):
    Problem is here:
    %A%20%20...
    %0A%20%20...
    and
    %74%A%9
    %74%0A%09
    How to fix what problem?
    Thanks.
     
    ignas2526, Sep 14, 2008 IP
  2. mNova

    mNova Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The problem is that dechex is returning only as many characters as it needs to. It's like 0500 in a decimal number system is the same as 500, because that leading zero doesn't need to be there.

    Try this:
    
    function javascriptencode($text)
    {
        $output = '';
        for ($i = 0; $i < strlen($text); $i++)
        {
        	$hexdigit = dechex(ord($text[$i]));
        	if(strlen($hexdigit) < 2) { $hexdigit = "0". $hexdigit; }
    
        	$output .= '%' . $hexdigit;
        	$crypto = strtoupper($output);
        }
        return $crypto;
    }
    
    PHP:
     
    mNova, Sep 14, 2008 IP
  3. cipals15

    cipals15 Well-Known Member

    Messages:
    1,085
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    100
    #3
    What code is this?
     
    cipals15, Sep 14, 2008 IP
  4. mNova

    mNova Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Uhhh, PHP. :confused:
     
    mNova, Sep 14, 2008 IP
  5. ignas2526

    ignas2526 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well actually its php, who works wt javascript. What code will encode javascript code ore html to something that normal people cant read. for example: "lol" will be "%6C%6F%6C". That code will be unescaped by javascript, but if person will view source it will see %6C%6F%6C, but javascript will output "lol" :D . Thats not good way to hide source, but its good way to show content only if javascript turned on :cool: .
    That next?
    Convent all your javascript files to php and than output encrypted code.
    Next?
    Add domain lock and on self die, so if victim will try to open it directly it will see blank page. Ore if victim will try to link to your javascript file from other web, nothing will be showed.
     
    ignas2526, Sep 14, 2008 IP