As I said earlier in my Programming for kids post, I started to teach my kids some basic programming stuff.
We were playing around with some word games and came up with a simple encrypting trick which I learned when I was a kid.
Basically, you type a word GERMANIKUS and substitute every character in that word with number, starting from 0.
So, G=0, E=1, R=2, M=3, A=4, N=5, I=6, K=7, U=8, S=9. After that, you write your message and substitute every character found in a word GERMANIKUS with a number and all other characters are just plain text.
For example, the word Codeforest would be COD1F1219T. Nice and interesting.
Now, my kids wanted to program a simple script that would take a message as a parameter and encrypt it using the above method. We chose PHP and started with writing down how the program should look and work. My kids were too eager to see the results, we first tried to work this out. This is the first version:
[code lang=”php”]
[/code]
Very simple and effective. I explained everything to my kids and they were eager to continue.
Next step was to build a form so you can enter the message to be encrypted.
[code lang=”php”]
‘;
echo nl2br(strtoupper($encodedMessage));
}
?>
[/code]
Very nice, now we have a basic encrypting mechanism. But what if we want to make this even harder to break? My older kid had an idea: we will allow user to enter a key which will be a number. If a user enters number 3, that would move number 0 (zero) to the third letter of germanikus word.
Great, let’s do it:
[code lang=”php”]
$word) {
$r[$count] = $secretWord[$key] . ‘ => ‘ . $word;
$count++;
}
echo implode(‘, ‘, $r) . ‘
‘;
$message = strip_tags($_POST[‘message’]);
//encode
$encodedMessage = str_replace($secretWordArray, $codeArray, strtoupper($message));
//decode
$decodedMessage = str_replace($codeArray, $secretWordArray, $encodedMessage);
echo ‘Encrypted message:
‘;
echo nl2br(strtoupper($encodedMessage));
echo ‘
‘;
echo ‘Decrypted message:
‘;
echo nl2br(strtoupper($decodedMessage));
}
?>
[/code]
When you try this demo, you will see that if you enter 3 as a key, the fourth letter will be 0. That is ok, as we want to start counting from zero.
That’s it, my kids were delighted with this and have learned a lot about string manipulation and a little bit about arrays in PHP.
Please share your thoughts in comments below.