Case Study CS00009: Cipher and Decipher a message for secure transfer
The problem: Cipher and Decipher messages:
Most data transmission and stored data is in the open. When more
security is needed, a random key is used to cipher the data and the same key is
used to decipher it. This example ciphers in two dimensions giving more
variation than more commonly used techniques. It translates the characters in
each position as do other cipherers. Additionally it rearranges the order of
the characters. This makes a message doubly difficult to reconstruct by brute
force plus it makes it more difficult to know if you have a reconstituted
message. In addition, this example cascades the process yielding even more
security as two pass phrases (or random key seeds) must be tried. The method
relies on both the cipher and decipher having the same random number generator.
A more realistic example would send the random variable with the data and not
rely on the random number generator.
The solution:
- Create a random number seed from a key phrase.
- Create a horizontal randomization vector to reposition characters in the
message.
- Create a vertical random translation vector for changing each character.
- Build a reverse procedure to decipher the message.
Note: You can cut and paste these code fragments into the code pane of
the Glee interpreter and experiment as you go along to see the actual
operations live.
The Glee code:
'init'#pgm {t# => n; n %% n?k
``< => hv; 256 %% n ? => vv; hv vv};
'ciph'#pgm {init=>v;(t[v[1]<]) #asc +(v[2]<) % 256 #asc};
'deciph'#pgm {init=>v;t #asc +256 -(v[2]<) %256 [v[1]< ``>] #asc};
$$The Original Message Text
$$-------------------------------------------
$R__$r
Now is the time for all good men to come to the aid of their country
__ => ot;
'$$ Original Text' ot$;
$$ The Ciphering Process yields "ct" coded text
$$-------------------------------------------
'a stitch in 20 times'#crc =>k$;
ot k[.t .k]ciph => ct$;
$$ Do it twice to really make it secure
$$-------------------------------------------
'open says me' #crc =>k$;
ct k[.t .k]ciph => ct$;
$$ The Deciphering Process
$$ Undo in the reverse order
$$-------------------------------------------
'Open says me'#crc =>k$;
ct k[.t .k]deciph=>ct $;
$$ And undo it twice
$$-------------------------------------------
'A stitch in 20 times'#crc =>k$;
ct k[.t .k]deciph $;
The Output:
$$ Original Text
Now is the time for all good men to come to the aid of their country
151536465
k¶êy³f \1.Q.çûö.%.E²".yø..ÃØD®Ü´=6itr1óîCxÜ{8K.°®ø=x3..+c5.9WI.ÙR
69091422
Þhº½òR.â7Oçóú..Ri)5ù/FN¼úkTÉÙâ³&ÑvúD.[sy5\?û÷÷Ý.PHZÆð1/pW¶AÕVÉ
69091422
k¶êy³f \1.Q.çûö.%.E²".yø..ÃØD®Ü´=6itr1óîCxÜ{8K.°®ø=x3..+c5.9WI.ÙR
151536465
Now is the time for all good men to come to the aid of their
country
The play-by-play:
- 'init'#pgm {t# => n; n %% n?k ``< => hv;
256 %% n ? => vv; hv vv};. This is the initialization
function. t# => n; It counts the
characters in the text . n %% n?k It uses
this to create a random horizontal relocation vector using k as the
random seed. ``< => hv;Performing
the grade on the result assures no duplicates while retaining the
randomization. 256 %% n ? => vv; A
vertical translation vector is also created for each character position.
hv vv}; These two integer vectors are
returned as a two item sequence. init is used by both the ciph
ciphering function and the deciph deciphering function. The technique
assumes the Glee interpreter runs both programs and uses the same
random number generator.
- 'ciph'#pgm {init=>v;(t[v[1]<]) #asc
+(v[2]<) % 256 #asc};. The ciphering function ciph runs
the initialization init function to obtain the horizontal and vertical
randomization vectors for the process. All it needs is the length of the
message, which it obtains from the message itself, and a numeric random seed.
The original and ciphered messages are the same length. init=>v; runs the initialization returning
the randomization vectors as a two element sequence which is assigned to
v. (t[v[1]<]) #asc +(v[2]<) % 256
#asc};. The first element of v is the horizontal translation
vector. It is used to rearrange the order of the characters in the message.
#asc +(v[2]<) % 256 #asc};. The
characters are then converted to integers and translated by the vertical
randomization vector. This is done by adding the random variable to the value
of the character and using modulo 256 to contain it in the domain of valid
characters before converting it back from numeric to character form. The result
is the ciphered message which is then sent to the recipient (or to another
ciphering stage for greater security).
- 'deciph'#pgm {init=>v;t #asc +256 -(v[2]<)
%256 [v[1]< ``>] #asc};. Deciphering done by deciph
reverses the process. init=>v; It uses
the same init function to produce the random variables.
t #asc +256 -(v[2]<) %256 The process
is reversed by converting the ciphered message to numeric and biasing by 256 to
keep it positive when the vertical random vector is subtracted. Modulo 256 is
used to confine the result to the domain of characters . [v[1]< ``>] #asc};We index this result by
a vector created by grading the horizontal random vector in the opposite
direction (i.e. up). This puts the message characters back into their original
sequence. The resulting deciphered text is returned.
- $$The Original Message Text
$$-------------------------------------------
$R__$r Now is the time for all good men to come to the aid of their country __
=> ot;
'$$ Original Text' ot$;.
For this example I use Glee's raw capture facility to obtain the
original text in ot.
- $$ The Ciphering Process yields "ct"
coded text
$$-------------------------------------------
'a stitch in 20 times'#crc =>k$; ot k[.t .k]ciph => ct$;
Now running these functions, we take the original text ot and cipher
with a random seed created from a pass phrase.'a
stitch in 20 times'#crc =>k$;. The cyclical redundancy check
function produces an integer from the string which is just what we want for a
seed. Note: #crc operates in the spirit
of Glee. It ignores special characters, case, and extraneous
whitespace. My example pass phrase contains the obligatory numeric which some
seem to think improves security. ot k[.t .k]ciph
=> ct$;. I make a familiar Glee call to the
ciph function giving it a namespace containing the original message in
t and the random seed in k. The result is assigned to ct
and displayed. Just for grins I do the ciphering twice with two different pass
key phrases. That should really scramble things up.
- $$ The Deciphering Process
$$ Undo in the reverse order
$$-------------------------------------------
'Open says me'#crc =>k$; ct k[.t .k]deciph=>ct $;
$$ And undo it twice
$$-------------------------------------------
'A stitch in 20 times'#crc =>k$; ct k[.t .k]deciph $;
Deciphering works exactly like ciphering but we do it in reverse. The first
time I replace the ct input with the result of the deciph
function and display that interem result. The second time, using the other pass
key phrase in an unwinding reverse order fashion, I just display the
deciphered result.
This completes the example. To better understand these operators and other
things you can do with them, consult the operator pages according to the type of
data you see being operated on.