Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic encoding function gone wrong

Status
Not open for further replies.

748323

Programmer
Dec 10, 2004
70
US
I'm making an encoding function, but it went wrong already. I know that this would be very easy to crack, but it will become a lot more complicated later on.

Pretty much, the code declares the variables, changes them into arrays, and uses Str_replace to replace all instances in $_GET['x'] with another. "a" with "f", "b" with "c", etc.

But, it doesn't work that way. When I tell it to encode "a", it returns "2". When I try to encode "abcdef", I get "2U#dt2", instead of the results I wanted, which is "f)tIk^". Thanks.

Code:
<?

$abc = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_-+=";
$key = "f)tIk^PQoOhzNJw9-_6ebs!4pxRaEqT$U2LWd07FGl&BZVy3+7#rm=M%5*jYXvD(AcHu1CKn@IgS";

for($i=0; $i < strlen($abc); $i++) {
	$x = $i + 1;
	$abc_array[] = substr($abc, $i, 1);
	$key_array[] = substr($key, $i, 1);
}


$enmsg = str_replace($key_array, $abc_array, $_GET['x']);

echo $enmsg;

?>
 
have slightly changed the code to give the effect you wanted.

Code:
$abc = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_-+=";
$key = "f)tIk^PQoOhzNJw9-_6ebs!4pxRaEqT$U2LWd07FGl&BZVy3+7#rm=M%5*jYXvD(AcHu1CKn@IgS";
$_GET['x']="now is the time for all good me to come to the aid of the party";

for($i=0; $i < strlen($abc); $i++) {
    $key_array[substr($abc, $i, 1)] = substr($key, $i, 1);
}

$encoded = "";

for($i=0; $i < strlen($_GET['x']); $i++) {
	$k = substr($_GET['x'], $i, 1);
    $encoded .= $key_array[$k];

}


echo "The message is " . $encoded;

?>
 
Can you please explain how your code works? Also, why doesn't mine work as expected? Thanks a lot. I will look into your code and try to understand it, though.
 
in pseudocode the array structure is
array[key]=>array[value]

the first foreach loop assigns the encoded value to each array[value] for array[key]. thus array['a'] = array['f'] in your example.

i believe that this is where you were going wrong as you were merely creating two arrays with numeric keys.

the second foreach loop iterates through the incoming _GET variable and sets a new variable equal to the encoded value which is the value assigned to the key that equals the character of the _GET var [sorry - it's easier to explain in the code...]. thus if letter 5 of the _GET var is an "X" then the encoded value will be $key_array['X']

lastly i don't think that you can use str_replace in the manner you did, (given the inputs you had).

hth
Justin


 
I've had another look at your code. one serious mistake you made was the reversal of the search and replace keys in the str_replace function.

i think that there are also issues with some of the characters in your search string (the carat seems to cause problems). as a result if you print_r both of your arrays then you find that there is a large number of keys that are unfilled in the key array.

debugging your code i took things right back to basic and got the following to work just fine.

Code:
<?
$abc = "abcd";
$key = "f)tI"; 
$x="abcd";
echo "<br><br><br>";

for($i=0; $i < strlen($abc); $i++) 
{
    $abc_array[] = substr($abc, $i, 1);
    $key_array[] = substr($key, $i, 1);
}
print_r ($abc_array);
echo "<br>";
print_r ($key_array);

$e = str_replace($abc_array, $key_array, $x);
echo "<br><br>results: $e";
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top