Trying to create a scrambling script but got into a problem...

Status
Not open for further replies.

Mr. Goodie2Shoes

Active Member
74
2012
11
0
Here's the coding... I am using "preg_replace"
PHP:
<?php
$key = $_SERVER['QUERY_STRING'];
$characters = 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/','/0/','/1/','/2/','/3/','/4/','/5/','/6/','/7/','/8/','/9/');
$scrambles = array('0','1','2','3','4','5','6','7','8','9','a','s','d','f','g','h','j','k','l','q','w','e','r','t','y','u','i','o','p','z','x','c','v','b','n','m');

$result = preg_replace($characters, $scrambles, $key);
echo "Original:<br/>";
echo $key."<br/>";
echo "Scrambled:<br/>";
echo $result;
?>

and here's a result:
Code:
Original:
abcdefghijklmnopqrstuvwxyz0123456789
Scrambled:
iopzxcvbnmaldfghjklqrertyuiopzxcvbnm

as you can see, there are no numerics in the scrambled string... any help?
 
6 comments
It's happening because it changes multiple times during the preg_replace.
try this:
PHP:
<?php
$key = $_SERVER['QUERY_STRING'];
// used for testing
// $key = 'abcdefghijklmnopqrstuvwxyz0123456789';
$characters = 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/','/0/','/1/','/2/','/3/','/4/','/5/','/6/','/7/','/8/','/9/');
$scrambles = array('0','1','2','3','4','5','6','7','8','9','a','s','d','f','g','h','j','k','l','q','w','e','r','t','y','u','i','o','p','z','x','c','v','b','n','m');
$result = '';
$ctr =0;
$one = 1;
while ($ctr < strlen($key)) {
	$k = substr($key,$ctr,1);
	$out = preg_replace($characters[$ctr], $scrambles[$ctr], $k, $one);
  $result .= $out;
$ctr++; }
echo "Original:<br/>";
echo $key."<br/>";
echo "Scrambled:<br/>";
echo $result;
?>
 
Using Regular Expression is not a good choice to do that. You could use str_shuffle to scramble a string
http://php.net/manual/en/function.str-shuffle.php

PHP:
<?php
$key = $_SERVER['QUERY_STRING'];
$result = str_shuffle($key);
echo "Original:<br/>";
echo $key."<br/>";
echo "Scrambled:<br/>";
echo $result;
?>
I also need to unscramble the scrambled string later on... thats why I didn't go for str_shuffle

It's happening because it changes multiple times during the preg_replace.
try this:
PHP:
<?php
$key = $_SERVER['QUERY_STRING'];
// used for testing
// $key = 'abcdefghijklmnopqrstuvwxyz0123456789';
$characters = 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/','/0/','/1/','/2/','/3/','/4/','/5/','/6/','/7/','/8/','/9/');
$scrambles = array('0','1','2','3','4','5','6','7','8','9','a','s','d','f','g','h','j','k','l','q','w','e','r','t','y','u','i','o','p','z','x','c','v','b','n','m');
$result = '';
$ctr =0;
$one = 1;
while ($ctr < strlen($key)) {
    $k = substr($key,$ctr,1);
    $out = preg_replace($characters[$ctr], $scrambles[$ctr], $k, $one);
  $result .= $out;
$ctr++; }
echo "Original:<br/>";
echo $key."<br/>";
echo "Scrambled:<br/>";
echo $result;
?>
oh... awesome! :D
Thanks man! <3

---------- Post added at 07:54 PM ---------- Previous post was at 10:25 AM ----------

okay lockdown.... I got a problem... this doesn't work when I use this:
Code:
Original:
mnacrGfjzbGl1uj
Scrambled:
mnacrGfjzbGi1u
 
Here this will work except to reverse capitals to capitals you need to put them in your tables. If not change the str_replace to str_ireplace .
PHP:
<?php
$key = $_SERVER['QUERY_STRING'];
$characters = 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','0','1','2','3','4','5','6','7','8','9');
$scrambles  = array('0','1','2','3','4','5','6','7','8','9','a','s','d','f','g','h','j','k','l','q','w','e','r','t','y','u','i','o','p','z','x','c','v','b','n','m');
$result = '';
$ctr = $one = 0;
while ($ctr < strlen($key)) 
{
	$k = substr($key,$ctr,1);	
	$ctr1 = $one = 0;
	while ( $one < 1 and $ctr1 < count($characters) )
	{ $out = str_replace($characters[$ctr1], $scrambles[$ctr1], $k, $one); $ctr1++; }
  $result .= $out;
  $ctr++;  
}
echo "Original:<br/>";
echo $key."<br/>";
echo "Scrambled:<br/>";
echo $result;
?>
 
okay... I'll check it out... again thanks for the help :)

---------- Post added at 09:35 PM ---------- Previous post was at 09:29 PM ----------

ooo... it works! :D thanks man!
anyways, to help others, I've found another algorithm:
PHP:
<?php
$key = $_SERVER['QUERY_STRING'];
$encryptionpass = '[a random string here...]';
 
$result = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($encryptionpass), $key, MCRYPT_MODE_CBC, md5(md5($encryptionpass))));

echo "Original:<br/>";
echo $key."<br/>";
echo "Scrambled:<br/>";
echo $result;
?>
to decrypt, you just have to use this:
PHP:
rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($encryptionpass), base64_decode($result), MCRYPT_MODE_CBC, md5(md5($encryptionpass))), "\0");
anyways, I'll be using the one lock down gave as that is the one I was looking for! :D
 
Status
Not open for further replies.
Back
Top