Example Encoding / Decoding Script

Started on the old forum in September 2012 · view the original in the archive · old links redirect here

2 replies · 528 views · started 14 years ago · 1 min read

2 participants
Ghost Senior · 366 posts #1

a little script to get people started on there own encoding/decoding script

made this post cos alot of people continue to this day to ask how to either make one or how to encode / decode something.

all you need to do is make your own file, add this code to it. then run the script to see the working result.

then you can add the rest of the alphabet/symbols you want to be included into your encode/decode sequence and what type of style you want to decode the letter/symbols in.

example: right now i set it so A = 9E but you could make it stronger by setting it to something like A = 6GH42RR etc etc..

<?php
          /** An Example Encoding / Decoding Script
              By Ghost
           */

/** Encode */
function Encode($Data) 
{ 
$Encode_From = array('A', 'B', 'C'); 
$Encode_To = array('9E', 'R4', 'D1'); 
for($m = 0; $m<count($Encode_From); $m++)  
{ 
$Data = str_replace($Encode_From[$m], rtrim($Encode_To[$m]), $Data); 
}  
Return $Data; 
}
echo '<b>Encoded Data:</b><br/>';
echo Encode('ABC');

echo '<br/><br/>';

/** Decode */
function Decode($Data) 
{ 
$Decode_From = array('9E', 'R4', 'D1'); 
$Decode_To = array('A', 'B', 'C'); 
for($m = 0; $m<count($Decode_From); $m++)  
{ 
$Data = str_replace($Decode_From[$m], rtrim($Decode_To[$m]), $Data); 
}  
Return $Data; 
}
echo '<b>Decoded Data:</b><br/>';
echo Decode('9ER4D1');

?>

you would also want to prob put your own upper/lowercase checking in also. as right now this only looks for uppercase chars, as i put them all in as uppercase in the array.

anyway this should help you make your own version anyhow.

hope it helps people.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Andrew. Newbie · 7 posts #2

Use associative arrays and functions array_keys / array_values instead of that loop. Here's my variant of that code:

class crypt
{
	private static $chars=array(
		'A'		=>		'9E',
		'B'		=>		'R4',
		'C'		=>		'D1', //and so on...
		);

	public static function decode($str)
	{
		return str_replace(array_values(self::$chars),array_keys(self::$chars),$str);
	}

	public static function encode($str)
	{
		return str_replace(array_keys(self::$chars),array_values(self::$chars),$str);
	}
}
echo crypt::decode('9ER4D1');
//^will echo 'ABC'
Ghost Senior · 366 posts #3

Andrew. wrote: Use associative arrays and functions array_keys / array_values instead of that loop. Here's my variant of that code:

class crypt
{
	private static $chars=array(
		'A'		=>		'9E',
		'B'		=>		'R4',
		'C'		=>		'D1', //and so on...
		);

	public static function decode($str)
	{
		return str_replace(array_values(self::$chars),array_keys(self::$chars),$str);
	}

	public static function encode($str)
	{
		return str_replace(array_keys(self::$chars),array_values(self::$chars),$str);
	}
}
echo crypt::decode('9ER4D1');
//^will echo 'ABC'

i like this alot more than my version lol. does exactly the same thing. just not using the loop. thanks. im not that upto date on classes. hardly ever use them. but a great example of how to use one.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close