MAPLE Programs for Cryptology

Contest: To practice your skill at decoding Vigenere ciphers, click here!

To download these programs, you should have a floppy disk or some hard drive space accessible to you. Then push shift-click in Netscape over the link to bring up the dialog box for downloading.

After you download it, you can run Maple and use the menu command File-Open to load the worksheets.

crypt.mpl(9/8/99)
This file contains the Maple programs for decoding and encoding monoalphabetic ciphers, and for cryptanalysis of them. You don't use this directly; it is loaded by the other worksheets.

modular.mws(8/27/99)
This is a Maple worksheet that demonstrates modular arithmetic.

basic.mws(8/27/99)
This is a Maple worksheet that loads crypt.mpl and demonstrates affine ciphers.

keyword.mws(8/30/99)
This is a Maple worksheet that loads crypt.mpl and demonstrates cryptanalysis of monoalphabetic ciphers.

homophone.mpl(9/9/99)
This file contains the Maple programs for decoding and encoding the homophonic cipher in Chapter 2. You don't use this directly; it is loaded by the other worksheets.

homophone.mws(9/9/99)
This is a Maple worksheet that loads homophone.mpl and demonstrates homophonic ciphers.

vigenere.mpl(9/9/99)
This file contains the Maple programs for decoding and encoding the Vigenere cipher. You don't use this directly; it is loaded by the other worksheets.

vigenere.mws(9/9/99)
This is a Maple worksheet that loads vigenere.mpl and demonstrates Vigenere ciphers.

testbug.txt(9/9/99)
This is a plain text file containing a chunk of "The Goldbug" which is read into some of the Maple worksheets.

goldbug.mws(9/9/99)
This experiments with the Vigenere cipher applied to "The Goldbug".

vigen_decode.mws(9/30/99)
Maple worksheet for Vigenere codes. Has complete programs in buttoned up sections. Click the buttons to see the sections, and press enter on each command to activate it.

shiftregister.mws(11/4/99)
Maple worksheet for experimenting with shift registers.

fiat-shamir.mws(11/4/99)
Maple worksheet for experimenting with the Fiat-Shamir zero-knowledge protocol.

RSA.mws(11/4/99)
Maple worksheet for experimenting with the RSA cryptosystem.

Some instructions

The worksheets (files ending with .mws) load the cryptology programs in the file crypt.mpl. You may need to change the beginning line of the worksheet to be able to find the program file crypt.mpl to something like

read `C:/windows/desktop/crypt.mpl`;

depending on where you have put the file. Be sure to enclose the entire pathname in single backquotes.

Here are some descriptions of the individual commands found in crypt.mpl.

numbers(string)
string is a string of uppercase letters enclosed in single backquotes, e.g. `THECATINTHEHAT`. The function numbers(string) returns the list of numbers from 1 to 26 associated to the letters, e.g. [20, 8, 5, 3, 1, 20, 9, 14, 20, 8, 5, 8, 1, 20]
Note that spaces or lowercase letters are not allowed. One could and should allow that, but the modular arithmetic would no longer be modulo 26.

letters(numberlist)
letters does the reverse of numbers, i.e. convert a list of numbers from 1 to 26 back to a string of letters.

caesar(plaintext,key)
This carries out the additive cipher on the plaintext. plaintext is a string in UPPERCASE enclosed in `backquotes`; that's for all our strings in these programs.
key is an integer. So caesar(`HELLO`,3) produces `KHOOR` (the backquotes won't show in the output; but they will be there for the purposes of using the output).
To save that ciphertext, give the command as ciphertext:=caesar(plaintext,key). Decoding can be accomplished by caesar(ciphertext, -key). Check it out!

tryall(string)
This tries all caesar shifts on the string and gives all 26 results.

affine(plaintext,[addkey,multkey])
This carries out the affine cipher with additive key addkey and multiplicative key multkey. It takes the numbers associated to each letter in the plaintext and carries out multkey * (number + addkey) mod 26. (* means multiplication in Maple). So affine(`HELLO`,[1,3]) gives `ARMMV`

monoencode(plaintext,keyword)
This carries out a general monoalphabetic cipher. The keyword should be a scrambled form of `ABCDE...XYZ`, giving the letters that should be substituted for A, B, C, etc. So monoencode(`HELLO`,`VWXZCRYPTANLSIBDEFGHJKMOQU`); yields `PCLLB`

monodecode(ciphertext,keyword)
This does the deciphering of the previous command. So monodecode(`PCLLB`,`VWXZCRYPTANLSIBDEFGHJKMOQU`); gives `HELLO`.

freq(msg)
This counts the number of times each letter occurs in the given string msg. The result is a chart of these numbers.

percent(msg)
This gives the percentages of occurrence of each letter in msg.

sortfreq(msg)
This gives the frequencies sorted from high to low. The first letter listed is the most frequently appearing, etc.

sortpercent(msg)
This gives the percentages sorted from high to low.

bigramfreq(msg)
This counts the number of times bigrams occur in the given string msg. Only the ones that occur are listed.

analyze(ciphertext,keyword)
This procedure is for analysis of a ciphertext obtained from monoalphabetic cipher. keyword is your current guess for the keyword that unlocks the cipher. If you are right, then monodecode(ciphertext,keyword) should give a sensible message. If it doesn't, then analyze gives a chart that shows the cipher letters, the plaintext letters they are translated into, and the frequencies of occurrence in msg. That may give you clues on how to rearrange the keyword to make sense of the ciphertext.

chart, schart
These are charts of the frequencies of English letters as given in the textbook. schart is the chart sorted from high to low. To see them just give the command chart;, or in prettier format evalm(chart);


Here are some descriptions of the individual commands found in homophone.mpl.

homophoneenc(plaintext)
This uses the homophonic cipher of Chapter 2 to encode the plaintext.

homophonedec(ciphertext)
This uses the homophonic cipher of Chapter 2 to decode the ciphertext.

hfreq(ciphertext)
This counts the frequency of 2 digit blocks in the ciphertext.

hfreqsort(ciphertext)
Frequencies of 2 digit blocks sorted from high to low.

hpairfreq(ciphertext)
Frequencies of 4 digit blocks.

hpairfreqsort(ciphertext)
Frequencies of 4 digit blocks sorted from high to low.

readin(`filename`)
Reads in a text file, strips off all nonletter characters, and converts to uppercase. Returns a single string.


Here are some descriptions of the individual commands found in vigenere.mpl.

vigen_enc(plaintext,keyword)
This uses the Vigenere cipher of Chapter 2 to encode the plaintext.

vigen_dec(ciphertext,keyword)
This uses the Vigenere cipher of Chapter 2 to decode the ciphertext.

fig2_2
This the text of the cipher example in Figure 2.2 all typed in.

exercise6
This the text of the Exercise 6 in Chapter 2 all typed in. (Don't mention it.)

index_of_coincidence(text)
This computes the index of coincidence of the given text.

friedman(text)
This is Friedman's complicated formula for the length of the keyword.

friedman2(text)
This is the simplified Friedman formula for the length of the keyword.

subfreq(text,[l,i])
This is the frequency table for all letters in the text numbered l*x+i for x=0,1,2,3,.... From this we can guess which is E

readin(`filename`)
Reads in a text file, strips off all nonletter characters, and converts to uppercase. Returns a single string.


David Wright
Last modified: Mon Dec 4 14:27:48 CET 2006