Solved

Converting Javascript code to PHP

Posted on 2006-07-14
11
429 Views
Last Modified: 2008-03-17
I would like this javascript code converted to PHP. The example is shown here with an explanation of how it works:

http://scriptasylum.com/tutorials/encdec/encode-decode.html
0
Comment
Question by:ray-solomon
  • 6
  • 3
  • 2
11 Comments
 
LVL 14

Assisted Solution

by:ThG
ThG earned 100 total points
ID: 17111695

it is rather simple.. the following functions will perform both conversions, given $str as input:

function js_escape($str) {
  return rtrim("%" . chunk_split(bin2hex($str), 2, "%"), "%");
}

function js_unescape($str) {
  return rawurldecode($str);
}
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17112168
Here is the other part that i think finds the unicode values for each character in the string and shifts it. Then the function unshifts it again based on how many times it was shitfed to begin with.

<script language="javascript">
function dF(s){
var s1=unescape(s.substr(0,s.length-1)); var t='';
for(i=0;i<s1.length;i++)t+=String.fromCharCode(s1.charCodeAt(i)-s.substr(s.length-1,1));
document.write(unescape(t));
}
</script>


In other words, if I have an html file that includes a javascript file. And in the javascript file contains an encoded string in a variable and also the function to decode it. I need the php script to generate the encoded string to be written to the javascript file and a javascript function to decode the string in the javascript file.
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17119681
is there a way to convert that javascript function into php? The example is on the webpage about halfway down where it has two text boxes and a drop-down menu in the middle.
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17132828
Actually, this is the function I needed to be converted to PHP. I just need it to take a string and encode it:


var encN=1;
// ENCODES, IN UNICODE FORMAT, ALL TEXT AND THEN ESCAPES THE OUTPUT
function encodeTxt(s){
s=escape(s);
var ta=new Array();
for(i=0;i<s.length;i++)ta[i]=s.charCodeAt(i)+encN;
return ""+escape(eval("String.fromCharCode("+ta+")"))+encN;
}
0
 
LVL 14

Expert Comment

by:ThG
ID: 17134617
what this is about? i think i'm not understanding. this is a sort of rot13 function..but with offset 1.

if you put encN = 13, you get exactly str_rot13() built-in php function.

see http://it.php.net/manual/en/function.str-rot13.php
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Author Comment

by:ray-solomon
ID: 17135054
I looked over the manual too. I see it look very similar.

Here is the explanation of the javascript:
   1. First, all the text is escaped.
(ThG already wrote this)

   2. Then the script finds the Unicode values for each character in the string.
(The escaped string is converted to unicode and stored in a variable)

   3. Then the script adds whatever the Code Key drop-down box value is to each character's Unicode value.
(var encN=1; This is the key code. Key code=1, otherwords, this is how many times the unicode characters get shifted forward)
The php function would convert every character in a string to its unicode equivelent, it would then shift each character forward however many times we want(a variable we determine, we will use 1 for this example).

   4. Then the script derives characters based on the shifted Unicode values.


   5. The Code Key value is also embedded in the decoded text so the script knows how to properly decode the string again.
(return ""+escape(eval("String.fromCharCode("+ta+")"))+encN;) <- notice +encN at the end.

   6. Finally, it escapes the result one more time to remove any special characters. Now, the output looks totally foreign to someone who cannot un-shift Unicode values in their head. :)

<script type="text/javascript">
// I just modified this function to help you understand it
// ENCODES, IN UNICODE FORMAT, ALL TEXT AND THEN ESCAPES THE OUTPUT
var encN=1;
function encodeTxt(s){
s=escape(s);
var ta=new Array();
for(i=0;i<s.length;i++)ta[i]=s.charCodeAt(i)++encN;
return ""+escape(eval("String.fromCharCode("+ta+")"))+encN;
}
var a=encodeTxt('HELLO')
alert (a);
</script>
(This will out put IFMMP) Which is "Hello" with every letter shifted forward once. (definded by encN=1;)

Ray
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17135085
oops change:
for(i=0;i<s.length;i++)ta[i]=s.charCodeAt(i)++encN;

to

for(i=0;i<s.length;i++)ta[i]=s.charCodeAt(i)+encN;
0
 
LVL 6

Expert Comment

by:SysTurn
ID: 17139204
<?php

$encN = 3;

function encodeTxt($s)
{
    // We need to globals the Enc Key to access it inside the function
    global $encN;
   
    // Uncomment to escape the string input
    // urlencode($s);
   
    $ta = '';
    for($i=0; $i<strlen($s); $i++)
    {
        $ta .= chr(ord(substr($s, $i, 1)) + $encN);
    }
   
    // Uncomment to escape the string output
    // urlencode($ta);
   
    return $ta;
}
echo encodeTxt('HELLO');

?>

Kind Regards
Bakr
0
 
LVL 6

Expert Comment

by:SysTurn
ID: 17139212
P.S: I changed $encN to 3 instead of 1 just while testing :)
0
 
LVL 6

Accepted Solution

by:
SysTurn earned 400 total points
ID: 17139285
here are both encode and decode functions with urlencode "escape" enabled:

<?php

$encN = 1;

function encodeTxt($s)
{
    // We need to globals the Enc Key to access it inside the function
    global $encN;
   
    // Uncomment to escape the string input
    $s = urlencode($s);
   
    $ta = '';
    for($i=0; $i<strlen($s); $i++)
    {
        $ta .= chr(ord(substr($s, $i, 1)) + $encN);
    }
   
    // Uncomment to escape the string output
    $ta = urlencode($ta);
   
    return $ta;
}

function decodeTxt($s)
{
    // We need to globals the Enc Key to access it inside the function
    global $encN;

    // Uncomment to escape the string input
    $s = urldecode($s);

    $ta = '';
    for($i=0; $i<strlen($s); $i++)
    {
        $ta .= chr(ord(substr($s, $i, 1)) - $encN);
    }

    // Uncomment to escape the string output
    $ta = urldecode($ta);

    return $ta;
}

echo decodeTxt( encodeTxt('HEL & OO') );

?>
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17140389
This works exactly like its intended. Thank you so much everyone for contributing to this question. I could have not figured this out by myself anytime soon.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now