Cryptographically "perfect" (information-theoretically secure), this cipher uses a truly random encryption that varies for each character of the message, making it immune to occurrence frequency analysis and brute-force attack.
We create a unique one-use "pad" (originally just a sequence of numbers scrawled on a paper pad, using pen) of individual random letter shifts that are only useful in encrypting/decrypting that particular message. It is discarded after use. Presumably both sender and receiver have some secure methor of exchanging the message and the pad.
The resulting ciphertext will be impossible to decrypt or break (according to wiki) if the following four conditions are met:
This version includes the "space" character in the defined "alphabet" to further mess with the would be message hacker - the net effect is that spaces appear randomly in the ciphertext, regardless of where they were in the cleartext, which is fun. You can arbitrarily decide on the character set included in the "alphabet" to make messages that include other characters I suppose.
Want your own PAD? - GENERATE your own
Example:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cleartext: | C | O | D | E |
---|---|---|---|---|
Pad(random): | 1 | 20 | 14 | 0 |
Ciphertext: | D | I | R | E |
'; } elseif (isset($_POST['decrypt'])) { //let's go with one-time pad decrypt //get message to decrypt $cipherText = strtoupper(trim($_POST['message'])); $howLong = strlen($cipherText); $clearText = ''; //ensure there IS a pad if (isset($_POST['pad']) and (strlen($_POST['pad'])>0)) { //parse pad back into an array $rawpad = $_POST['pad']; $pad = explode(" ",$rawpad); //now let's deal with the ciphertet for ($x=0; $x < $howLong; $x++) { //find char position of encrypted char $currPos = strpos($alphabet,$cipherText[$x]); if ($currPos !== false) { //dial back using pad $newPos = $currPos - $pad[$x]; if ($newPos < 0) { //check for mod wrap $newPos += 27; } //add that characte $clearText .= $alphabet[$newPos]; } } echo 'Encrypted Message: "'.$cipherText.'"'; } else { echo '
'; echo 'Pad: "'.$rawpad.'"
'; echo 'Decrypted Message: "'.$clearText.'"ERROR - we need the PAD to decrypt
'; } } ?>
AlgorithmBEGIN SET answer='' SET alphabet = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ' INPUT mode INPUT uppercase(message) SET msgLength = length of message IF mode = 'encrypt' THEN SET pad = [] FOR each char in message APPEND random(0..length of alphabet) to pad SET pos = character position of current char in alphabet CALCULATE newpos = (pos + corresponding pad entry) mod (length of alphabet) SET answer += alphabet[newpos] ENDFOR OUTPUT answer OUTPUT pad ELSE INPUT pad PARSE pad into array FOR each char in message SET pos = character position of current char in alphabet CALCULATE newpos = (pos - corresponding pad entry) mod (length of alphabet) SET answer += alphabet[newpos] ENDFOR OUTPUT answer ENDIF END