Results 1 to 17 of 17

Thread: [RESOLVED] code encryption in PHP

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    8

    [RESOLVED] code encryption in PHP

    Hi All,

    I have written encode() in php for string.

    below is code
    //
    PHP Code:
    <? 
    function encode($str){
        $alph=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
        $iLen = strlen(strtolower($str));
        $sTemp = "";
        $ival=0;

            for($i=1; $i <=$iLen;$i++){
                $sAsc = ord(substr($str, $i, 1));
                $sAscLen = strlen($sAsc);

                    switch($sAscLen)
                                                    {
                    case $sAscLen=0;
                    $sAsc = "000";
                    break;

                    case $sAscLen=1;
                    $sAsc = "00" . $sAsc;
                    break;

                    case $sAscLen=2;
                    $sAsc = "0" . $sAsc;
                    break;

                    default:
                    $sAsc = $sAsc;
                    }
        
                        $ival=$ival+round(($i / 3),0);

                        if($ival > 25 || $ival < 0){$ival = 0;}

                        if($ival < 10){ 
                            $sTemp = $sTemp . $sAsc .  $alph[$ival + 2];
                        }else{
                            $sTemp = $sTemp . $sAsc;
                        }
            }
        return $sTemp;
        }

    ?>
    //decode function

    PHP Code:
    <? 
          function decode($str){    
            
        $alph=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
        $sTemp = "";
            for($i=0; $i <=25;$i++){
                $str=str_replace(chr($i),"",$str);
            }

            $iLen = strlen($str);
            for($k=0; $k<$iLen; $k+=3){
                $sTemp = $sTemp . chr(substr($str, $k, 3));
                        }        

            return $sTemp;
        }
    ?>
    decode function is not working..it's not giving proper output.
    Last edited by veena das; Jan 19th, 2007 at 04:34 AM.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: code Encryption in PHP

    You have written the encode function and don't know how to write the decoder? I have a hard time believing that.

    Anyway, first you didn't use code tags. That makes your code as posted very unreadable. Second, the function would be unreadable even if it was properly formatted.
    It's barely readable enough to see that your switch is completely broken.

    Anyway, why don't you provide a description of how you encode the string? Then we can give suggestions as how to decode it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    8

    Re: code Encryption in PHP

    I have written decode() also. but it's not working..dat's why i asked for d decode().
    //
    PHP Code:
    <?
            function decode($str){ 

    $alph=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
    $sTemp = "";
    for($i=0; $i <=25;$i++){
    $str=str_replace(chr($i),"",$str);
    }

    $iLen = strlen($str);
    // die("--".$iLen);
    for($k=0; $k<$iLen; $k+=3){
    $sTemp = $sTemp . chr(substr($str, $k, 3));



    return $sTemp;
    }

    ?>
    Last edited by veena das; Jan 16th, 2007 at 12:31 AM.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: code encryption in PHP

    [code]Use CODE tags![/code]
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: code encryption in PHP

    Code:
    function decode($str){ 
    
    $alph=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
    $sTemp = "";
    for($i=0; $i <=25;$i++){
    $str=str_replace(chr($i),"",$str);
    }
    
    $iLen = strlen($str);
    // die("--".$iLen);
    for($k=0; $k<$iLen; $k+3){
    $sTemp = $sTemp . chr(substr($str, $k, 3));
    } 
    
    
    return $sTemp;
    }
    dude...you should be coding like this...

    Code:
    				
    		function remove_escape($data) { //removes quotes and slashes
    			global $dbc;
    			
    			if (ini_get('magic_quote_gpc')) {		
    				$data = stripslashes($data);
    			}//end if
    			
    			return mysql_real_escape_string($data,$dbc);
    		}//end remove_slashes
    Last edited by superbovine; Jan 15th, 2007 at 09:02 AM.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: code encryption in PHP

    You're missing the point. The code tags preserve the indentation of your code. I don't read code without indentation. Just wrapping the poorly formatted code inside code tags doesn't help.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: code encryption in PHP

    yeah yeah i was editing the post already, I didn't have an example

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: code encryption in PHP

    Ah, sorry, thought you were the OP.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    8

    Re: code encryption in PHP

    Hi superbovine !!

    I tried you code, but it's no working and actually i'm not getting your code. I'm new to PHP. I tried to write decode(). is there any mistake in my code? it's not giving proper output. all syntax are ok?

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: code encryption in PHP

    It was an example of formatting. The functionality was unrelated to your problem.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    8

    Re: code encryption in PHP

    To
    CornedBee
    ,

    I knew it was an example and i was not trying it with my code.
    plz, reply to post if u really want to give any solution..don post only suggestions, give solutions..I don want your suggestions. Plz don reply to my Posts

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: code encryption in PHP

    Very well. I shall never reply to your posts again and add you to my ignore list.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: code encryption in PHP

    So you're basically saying "don't help, just do it for me".

    You know, some of us had to, like, learn how to program; not just how to post 'givc0d4meplzkthx'.

  14. #14
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: code encryption in PHP

    Quote Originally Posted by veena das
    To
    CornedBee
    ,

    I knew it was an example and i was not trying it with my code.
    plz, reply to post if u really want to give any solution..don post only suggestions, give solutions..I don want your suggestions. Plz don reply to my Posts
    I can see the problem with your function. It is not working properly.


    Hope this helps!!!
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  15. #15

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    8

    Re: code encryption in PHP

    Quote Originally Posted by penagate
    So you're basically saying "don't help, just do it for me".

    You know, some of us had to, like, learn how to program; not just how to post 'givc0d4meplzkthx'.
    No, it's no like dat I don want any suggestion or help, but I had replied to
    superbovine and I was knowing it was an example. so I was expecting reply from superbovine.

  16. #16
    Lively Member rasana's Avatar
    Join Date
    Jan 2007
    Location
    Mumbai, (India)
    Posts
    94

    Re: code encryption in PHP

    Quote Originally Posted by veena das
    Hi All,

    I have written encode() in php for string.

    below is code
    //
    PHP Code:
    <? 
    function encode($str){
        $alph=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
        $iLen = strlen(strtolower($str));
        $sTemp = "";
        $ival=0;

            for($i=1; $i <=$iLen;$i++){
                $sAsc = ord(substr($str, $i, 1));
                $sAscLen = strlen($sAsc);

                    switch($sAscLen)
                                                    {
                    case $sAscLen=0;
                    $sAsc = "000";
                    break;

                    case $sAscLen=1;
                    $sAsc = "00" . $sAsc;
                    break;

                    case $sAscLen=2;
                    $sAsc = "0" . $sAsc;
                    break;

                    default:
                    $sAsc = $sAsc;
                    }
        
                        $ival=$ival+round(($i / 3),0);

                        if($ival > 25 || $ival < 0){$ival = 0;}

                        if($ival < 10){ 
                            $sTemp = $sTemp . $sAsc .  $alph[$ival + 2];
                        }else{
                            $sTemp = $sTemp . $sAsc;
                        }
            }
        return $sTemp;
        }

    ?>
    //decode function

    PHP Code:
    <? 
          function decode($str){    
            
        $alph=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
        $sTemp = "";
            for($i=0; $i <=25;$i++){
                $str=str_replace(chr($i),"",$str);
            }

            $iLen = strlen($str);
            for($k=0; $k<$iLen; $k+=3){
                $sTemp = $sTemp . chr(substr($str, $k, 3));
                        }        

            return $sTemp;
        }
    ?>
    decode function is not working..it's not giving proper output.
    ========================================================
    Hi Veena,

    your encode() and decode() are correct but make small change in encode() function at the last part in the else statement.. if..else statement should be

    PHP Code:
    if($ival 15)

       
    $sTemp $sTemp $sAsc .  $alph[$ival 2];
    }else
    {
       
    $sTemp $sTemp $sAsc $alph[2];

    make small change in decode() also, in the following part of code


    PHP Code:
    for($k=0$k<$iLen$k+=4)
    {
    $sTemp $sTemp chr(substr($str$k3));
                        } 
    hope this works

  17. #17

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    8

    Re: code encryption in PHP

    Quote Originally Posted by rasana
    ========================================================
    Hi Veena,

    your encode() and decode() are correct but make small change in encode() function at the last part in the else statement.. if..else statement should be

    PHP Code:
    if($ival 15)

       
    $sTemp $sTemp $sAsc .  $alph[$ival 2];
    }else
    {
       
    $sTemp $sTemp $sAsc $alph[2];

    make small change in decode() also, in the following part of code


    PHP Code:
    for($k=0$k<$iLen$k+=4)
    {
    $sTemp $sTemp chr(substr($str$k3));
                        } 
    hope this works
    Hi, yes I made changes in code and it worked. Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width