[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.
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.
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;
}
?>
Re: code encryption in PHP
[code]Use CODE tags![/code]
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
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.
Re: code encryption in PHP
yeah yeah i was editing the post already, I didn't have an example
Re: code encryption in PHP
Ah, sorry, thought you were the OP.
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?
Re: code encryption in PHP
It was an example of formatting. The functionality was unrelated to your problem.
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
Re: code encryption in PHP
Very well. I shall never reply to your posts again and add you to my ignore list.
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'.
:rolleyes:
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!!!
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'.
: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.
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, $k, 3));
}
hope this works :thumb:
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, $k, 3));
}
hope this works :thumb:
Hi, yes I made changes in code and it worked. Thanks :)