At this moment, you are not allowed access to the question.

Task 4. One-Time Pad

Available Marks: 14

A cryptographic system converts messages into a form that it is intended can only be converted back into the original message by the intended recipient. The only completely secure cryptographic system is one that uses no algorithm at all, just assigns a code apparently randomly to each component of the message.

Such so-called one-time pads can be simulated (and were in the old spy stories) by the sender and recipient agreeing to use the same edition of a text such as a novel. For each letter and punctuation in the message, the sender finds such a symbol in the book and assigns a series of numbers (page, paragraph and character position) to it. They transmit the numbers instead of the message, as the receiver can painstakingly consult the book to discover each original character.

For this task we will tidy up the requirements for the encoder to remove any ambiguity. The reference book is Project Gutenberg's copyright-free text edition of J M Barrie's Peter Pan, which you will need to download here.

Encoding and decoding rules

Example

For example, if the reference text began
It was a dark and stormy night. The wind howled eerily through the trees.
^ ^ ^                 ^ ^^                ^^   ^        ^        ^  ^^
a message I am now here would use the symbols marked and would be encoded as the numbers 0 1 1 17 1 0 16 0 3 8 8 2 0.

Write a program that can encode and decode messages. The input is either a message to be encoded, provided it starts with a letter or punctuation, or an encoded message to be decoded, if it starts with a number. No error checking is required.

Format

Messages to be encoded are on one line only. Messages to be decoded may be spread over multiple lines that each start with a digit. Your output can be spread over multiple lines if you wish.

Test Cases

There are four test cases, given on the line following the Test number in the box below. Each test is a separate run of the program, starting again at the beginning of the book.
Test 1:
This is a test.

Test 2:
Say, Don't Jackdaws Love My Big Sphinx of Quartz?

Test 3:
87 91 35 126 6 96 16 132 4 153 6 34 11 4 139 4 15 8 1 39 561 44 0 0 56 18 2 12 14

Test 4:
34 7 4 1 17 729 3 2 20 12 14 19 1 0 1 29 24 42 0 631 7 34 40 0 16 3 181 0 27 45 5
224 2 31 3 0 5007 9 53 1 2 4 33 0 3 1 22 3 48 20 27 10 6 21 30 0 16 38 54

Step 0

Refresh the browser window if the page has been idle for some time.

Step 1

Cut and paste the output of your program for each of the test cases into the box below:

Step 2

Paste the source code for your program into the box below.

Step 3

When you are sure all the data has been entered correctly on the form press the submit button below.

You may submit multiple times. Only your most recent submission for each question will be marked.