Math 455  • 26 March 2009

Caesar's additive encryption system

Summary

For quick reference, this section collects all the definitions used in the rest of the notebook.

alph=Characters["ABCDEFGHIJKLMNOPQRSTUVWXYZ"]

toNumber[ch_?letterQ]:=First@First@Position[alph,ch]-1
SetAttributes[toNumber,Listable]

inRangeQ[n_]:=IntegerQ[n]&&0≤n≤25

toCharacter[n_?inRangeQ]:=alph[[n+1]]
SetAttributes[toCharacter,Listable]

encrypt[j_?inRangeQ] := Mod[j + AliceEncryptKey, 26]
SetAttributes[encrypt,Listable]

decrypt[k_?inRangeQ] := Mod[k + BobDecryptKey, 26]
SetAttributes[decrypt,Listable]

Encoding and decoding between characters and numbers

alph=Characters["ABCDEFGHIJKLMNOPQRSTUVWXYZ"]

Characters to numbers

letterQ[ch_]:=MemberQ[alph,ch]

{letterQ["D"], letterQ["d"], letterQ[4]}

{Position[alph,"A"],Position[alph,"Z"]}

Position[alph,"D"]

First[First[%]]

First@First[%%]

toNumber[ch_?letterQ]:=First@First@Position[alph,ch]-1

toNumber["D"]

{toNumber["A"],toNumber["Z"]}

toNumber["HELLO"]

toNumber[Characters["HELLO"]]

SetAttributes[toNumber,Listable]

toNumber[Characters["HELLO"]]

toNumber[{"A","Z"}]

?toNumber

Numbers to characters

toNumber["D"]

alph[[3]]

alph[[3+1]]

inRangeQ[n_]:=IntegerQ[n]&&0≤n≤25

inRangeQ/@{-1,0,3,26}

toCharacter[n_?inRangeQ]:=alph[[n+1]]
SetAttributes[toCharacter,Listable]

toCharacter[{7,4,11,11,14}]

toCharacter[toNumber[{"H","E","L","L","O"}]]
toNumber[toCharacter[{7,4,11,11,14}]]

StringJoin[toCharacter[{7,4,11,11,14}]]

Mod: remainder

?Mod

{Mod[23,26], Mod[30,26]}

Mod[Range[0,40],26]

Alice and Bob

Alice encodes and encrypts

msg = "ICAMEISAWICONQUERED";

ptxt = toNumber[Characters[msg]]

AliceEncryptKey = 3;

encrypt[j_?inRangeQ] := Mod[j + AliceEncryptKey, 26]
SetAttributes[encrypt,Listable]

ctxt=encrypt[ptxt]

Bob decrypts and decodes

Solve[{k+AliceEncryptKey+inverseKey==k,Modulus==26},inverseKey,Mode->Modular]

BobDecryptKey = 23;

ClearAll[decrypt]

decrypt[k_?inRangeQ] := Mod[k + BobDecryptKey, 26]
SetAttributes[decrypt,Listable]

decrypt[8]

decrypt[ctxt]

toCharacter[%]

StringJoin[%]

StringJoin@toCharacter@decrypt[ctxt]

Note

The functions encrypt and decrypt were originally defined so as to each accept just a single integer (in the range 0, 1, 2, …, 25) as input. In order to encrypt or decrypt an entire list of numbers, we gave each of those functions the attribute Listable.

Another way to accomplish the same thing, without using an attribute, is to use Map. Thus after defining encrypt and decrypt to act upon single integers, to handle a list nums of numbers you would use 
	Map[encrypt,nums] and  Map[decrypt,nums] 
or their abbreviated forms: 
	encrypt/@nums  and  decrypt/@nums

Appendix: Getting Alice and Bob's pictures

You won't be able to use the input cells in this section unless you have a directory "E:/Courses/455/Images" that contains files Alice.gif and Bob.gif. Of course you could put the files anywhere and then change the SetDirectory argument to that directory.

SetDirectory["E:/Courses/455/Images"]

?Import

Row[{Import["Alice.gif"], Import["Bob.gif"]},Spacer[140]]