Hello,
I have a question regarding this: (a section of a lab assignment)
“We introduce columnar transposition here. The columnar transposition is a rearrangement of the characters of the plaintext into columns. We look specifically at five-column transposition. An example will make the idea clear. Consider the message “THIS IS A MESSAGE”. We arrange the letters in five columns like this:
T H I S ‘ ‘
I S ‘ ‘ A ‘ ‘
M E S S A
G E Z Z Z
Note that we add the infrequently used character “Z” at the end in order to have complete columns. The resulting ciphertext would then be read down the columns as
TIMGHSEEI SZSASZ AZ
EXERCISE 7: Add functions to encode and decode messages using the 5-column transposition. [Hint: create a function which takes a parameter n and a string and returns every nth character from the string.] What is the result of applying 5-column transposition to the following text: “IT WAS THE BEST OF TIMES”?”
[B]This is for a lab assignment, and neither my teacher or my TA is available to help me. This is somewhat of a last resort and I would really appreciate any bit of advice or direction on this( exercise 7) . Bellow is my page thus far
<title> Substitution Cipher</title>
<script type=”text/javascript”>
function Encode(message, key)
// Assumes: message is the string to be encoded, and
// key is a string of the 26 letters in arbitrary order
// Returns: the coded version of message using the substitution key
{
var alphabet, coded, i, ch, index;
alphabet = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
coded = “”;
i = 0;
while (i < message.length) { // for as many letters as there are
ch = message.charAt(i); // access each letter in message
index = alphabet.indexOf(ch); // find its position in alphabet
if (index == -1) { // if it’s not a capital letter,
coded = coded + ch; // then leave it as is & add
} // otherwise,
else { // find the corresponding
coded = coded + key.charAt(index); // letter in the key & add
key = rotateLeft(key);
}
i = i + 1;
}
return coded;
}
function Decode(message, key)
// Assumes: message is the string to be encoded, and
// key is a string of the 26 letters in arbitrary order
// Returns: the coded version of message using the substitution key
{
var alphabet, coded, i, ch, index;
alphabet = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
coded = “”;
i = 0;
while (i < message.length) { // for as many letters as there are
ch = message.charAt(i); // access each letter in message
index = key.indexOf(ch); // find its position in alphabet
if (index == -1) { // if it’s not a capital letter,
coded = coded + ch; // then leave it as is & add
} // otherwise,
else { // find the corresponding
coded = coded + alphabet.charAt(index); // letter in the key & add
key = rotateLeft(key);
}
i = i + 1;
}
return coded;
}
function rotateLeft(key) {
return key.substring(1, key.length) + key.charAt(0);
}
</script>
</head>
<body>
<table>
<tr><td>According to the substitution cipher, each letter: <BR><BR> </td>
<td> <tt>ABCDEFGHIJKLMNOPQRSTUVWXYZ<br /></tt>
<tt>| | | | | | | | | | | | | <br /></tt>
<tt>v v v v v v v v v v v v v </tt> </td>
</tr>
<tr><td>is encoded using the corresponding letter in the key: </td>
<td><input type=”text” id=”key” size=”26″ style=”font-family:Courier,Monospace”
value=”XYZABCDEFGHIJKLMNOPQRSTUVW” onChange=”javascript:this.value=this.value.toUpperCase();”/> </td>
</tr>
</table>
<hr />
<table>
<tr><td><textarea id=”message” rows=”8″ cols=”30″ onChange=”javascript:this.value=this.value.toUpperCase();”></textarea> </td>
<td><input type=”button” value=”Encode ==>”
onclick=”document.getElementById(‘encoded’).value=
Encode(document.getElementById(‘message’).value,
document.getElementById(‘key’).value);” />
</br>
<input type=”button” value=”Decode <==”
onclick=”document.getElementById(‘message’).value=
Decode(document.getElementById(‘encoded’).value,
document.getElementById(‘key’).value);” /> </td>
<td><textarea id=”encoded” rows=”8″ cols=”30″ onChange=”javascript:this.value=this.value.toUpperCase();”></textarea> </td>
</tr>
</table>
</body>
</html>