Online
 
Thursday, 20 November 2008
 
 

Shared Secret Algorithms | Print |  E-Mail
 

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);


This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Jumbo Coklat
 
Top! Top!