| Shared Secret Algorithms | | Print | |
Traditionally, the simplest cryptographic algorithms are the shared secret methods. Let's take a look at a few examples, starting with some that are thousands of years old: replacement/substitution ciphers.
Phrase Substitution
Throughout the centuries, leaders of large military forces have shared a common problem: how to direct troops and subordinates from long distances without risking that if the message courier is captured by the enemy, their plans are revealed. Even today, with advanced computerized encryption and instantaneous satellite communication, army soldiers will refer to targets and resources by code names or false labels so that only friendly forces will understand the message correctly: "Meet me at the disco when the frog jumps" might mean "Start the attack when the sun rises."
Implementing a phrase-substitution algorithm in PHP is as simple as creating an array to hold the code phrases and calling str_replace() to perform the substitution:
<?php
$codebook = array(
'start the attack' => 'meet me at the disco',
'sun' => 'frog',
'rises' => 'jumps'
);
$message = 'Start the attack when the sun rises.';
$encoded_message = str_ireplace(array_keys($codebook), array_values($codebook),
$message);
$decoded_message = str_ireplace(array_values($codebook), array_keys($codebook),
$encoded_message);
?>
In the preceding example, we've defined a set of words or phrases that will be translated by our substitution algorithm in $codebook. Ordinarily we'd expect $codebook to be a much larger dictionary, but for this example these few should be enough.
$encoded_message = str_ireplace(array_keys($codebook), array_values($codebook),
$message);
In our first call to str_ireplace() we're using the keys of our codebook as the search values and their corresponding values as the replacements.
$decoded_message = str_ireplace(array_values($codebook), array_keys($codebook),
$encoded_message);
| Users' Comments (0) |
|
No comment posted






