If elephants never forget, then I have mouse memory. As the years go by I strain at times to remember early childhood events. I do recall however a certain Christmas gift I received long ago, and when I consider how much fun I had with it I can't help but wax nostalgic. It was a book of ciphers and codes, and had instructions on how to create a multitude of substitution ciphers of varying difficulty. The excitement I felt when I scrawled an encoded message and rolled it up inside a hollowed out pen was not lessened in anyway by the fact that nobody would ever receive said encoded message, nor would anything happen when some time later the hollowed out pen was eventually fished out from the junk drawer to then be thrown away after the quick conclusion that it was broken. But heck, I was like 9 or something. When you are that age the day after tomorrow may as well be 50 years from now, and the reason for doing things need not make a whole lot of sense. At least when I was 9. As I reread this I can't help but think that my recollection of fond memories is coming out more pathetic than heartwarming, so lets jump to the present and get on with the post.
So we have established that I think encryption is neat, and that I believe communication via encoded messages delivered in hollowed out pens is VERY neat. So why blather about it here? Recently I have been working on a way to encrypt a value(s) that I then need to pass around via a URL. For my own purposes I really want the value to be very clean and contain no characters that might interfere with normal URL delimiters or special characters. The value I need to pass around is already strongly encrypted so I don't need another level of encryption, but the crypt text contains ASCII characters ranging from 1 to 255 so it's not very URL friendly (and another level of encryption can't hurt!). First I thought about base64 encoding it, but that is not very URL friendly either. Next I used base32, which works very well but does not have a built in php function. Finally I realized a simple substitution cipher would do the trick. Substitution ciphers are not strong encryption, but they are actual 2 way encryption routines, and really if you throw some other permutations into the routine one could possibly argue that block cipher algorithms are an extension of substitution ciphers. So without further off topic rambling, here is a simple PHP class that lets you encoce/decode a string with a substitution cipher:
class cipher {
var $chrs;
function cipher() {
$this->chrs = $this->set_url_codes();
}
function set_url_codes() {
$chrs = array(
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'Aa', 'Ab', 'Ac', 'Ad', 'Ae', 'Af', 'Ag', 'Ah', 'Ai', 'Aj', 'Ak',
'Al', 'Am', 'An', 'Ao', 'Ap', 'Aq', 'Ar', 'As', 'At', 'Au', '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 'Po', 'Pp',
'Pq', 'Pr', 'Ps', 'Pt', 'Pu', 'Pv', 'Pw', 'Px', 'Py', 'Pz', 'Qa',
'Qb', 'Qc', 'Qd', 'Qe', 'Qf', 'Qg', 'Qh', 'Qi', 'Qj', 'Qk', 'Ql',
'Qm', 'Qn', 'Qo', 'Qp', 'Qq', 'Qr', 'Qs', 'Qt', 'Qu'
);
return $chrs;
}
function code_it($string) {
$this->chrs = $this->set_url_codes();
$res = '';
while ($string) {
$char = $string{0};
$string = substr($string, 1);
$res .= $this->chrs[ord($char)];
}
return $res;
}
function uncode_it($string) {
$keys = array();
foreach ($this->chrs as $i => $v) {
$keys[$v] = $i;
}
$res = '';
while ($string) {
$char = $string{0};
if (intval($char) == 0 && strtoupper($char) == $char) {
$char .= $string{1};
$string = substr($string, 2);
}
else {
$string = substr($string, 1);
}
if (isset($keys[$char])) {
$id = $keys[$char];
$res .= chr ($id);
}
}
return $res;
}
}
Usage is straightforward:
$string = 'The quick brown fox jumped over the lazy dog';
$cipher = new cipher();
$hidden = $cipher->code_it($string);
$original = $cipher->uncode_it($hidden);
The array of characters (the cipher key) can be altered however you like, except the leading capital letter are used as an indicator to the decode routine that the replacement character is actually 2 characters. It was a fun trip down memory lane and it resulted in a nifty bit of code that solves a current day problem. I guess the only thing left to say is:
BvAgDaDhDiCxAgDrDhDnAgCyDbDgCwAgDmDaDbDlAgCvDhCwCx
AgDnDlCxCyDnDeAgDhDkAgDaCxDeDiCyDnDeAgDbDgAgDlDhDfCx
AgDpCtDrAhAgDmDaCtDgDdDlAgCyDhDkAgDkCxCtCwDbDgCzAgDfDrAgCuDeDhCzAh
| No Images with this post |
| No comments posted yet |