Click to See Complete Forum and Search --> : [RESOLVED] code encryption in PHP
veena das
Jan 15th, 2007, 05:59 AM
Hi All,
I have written encode() in php for string.
below is 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
<?
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.
CornedBee
Jan 15th, 2007, 06:14 AM
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.
veena das
Jan 15th, 2007, 06:27 AM
I have written decode() also. but it's not working..dat's why i asked for d decode().
//
<?
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;
}
?>
CornedBee
Jan 15th, 2007, 06:35 AM
Use CODE tags!
superbovine
Jan 15th, 2007, 07:56 AM
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...
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
CornedBee
Jan 15th, 2007, 08:01 AM
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.
superbovine
Jan 15th, 2007, 08:04 AM
yeah yeah i was editing the post already, I didn't have an example
CornedBee
Jan 15th, 2007, 08:06 AM
Ah, sorry, thought you were the OP.
veena das
Jan 15th, 2007, 11:34 PM
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?
CornedBee
Jan 16th, 2007, 02:54 AM
It was an example of formatting. The functionality was unrelated to your problem.
veena das
Jan 16th, 2007, 06:20 AM
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
CornedBee
Jan 16th, 2007, 07:48 AM
Very well. I shall never reply to your posts again and add you to my ignore list.
penagate
Jan 16th, 2007, 08:02 AM
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'.
:rolleyes:
visualAd
Jan 16th, 2007, 11:58 AM
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!!!
veena das
Jan 17th, 2007, 02:49 AM
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'.
:rolleyes:
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.
rasana
Jan 19th, 2007, 03:12 AM
Hi All,
I have written encode() in php for string.
below is 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
<?
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
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
for($k=0; $k<$iLen; $k+=4)
{
$sTemp = $sTemp . chr(substr($str, $k, 3));
}
hope this works :thumb:
veena das
Jan 19th, 2007, 03:33 AM
========================================================
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
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
for($k=0; $k<$iLen; $k+=4)
{
$sTemp = $sTemp . chr(substr($str, $k, 3));
}
hope this works :thumb:
Hi, yes I made changes in code and it worked. Thanks :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.