/    Sign up×
Community /Pin to ProfileBookmark

Transposition cyphers(…long…)

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[/B]

<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>&nbsp;<tt>ABCDEFGHIJKLMNOPQRSTUVWXYZ<br /></tt>
&nbsp;<tt>| | | | | | | | | | | | | <br /></tt>
&nbsp;<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>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@justinbarneskinDec 07.2009 — That code doesn't do anything like what I had expected from the direction?
Copy linkTweet thisAlerts:
@steveo4103Dec 08.2009 — Hey man I'm working on the same thing as you, we're probably in the same class. Let me know if you figure it out and I'll do the same.
Copy linkTweet thisAlerts:
@astupidnameDec 08.2009 — Yeah, the code you posted appears to have nothing to do with this particular assignment, maybe that was another assignment?. I don't normally do peoples homework, but what can I say, I was bored and felt like sharing my outcome. Besides, if you need to write a function to decode the resulting string(s) from below, I'm sure that will still impress your teacher (after I'm sure he/she reads this)!
The resulting ciphertext would then be read down the columns as

TIMGHSEEI SZSASZ AZ
[/quote]

Actually, (change of heart) I was going to post the code for the final answer for doing the (not really) 'encoding', but figure I should really just give you the

TIPS (for the [not really] encoding):[LIST=1]
  • [*]Thinking of it precisely as a table, who's 'cells' you wish to fill, where 5 is n, determine the next number divisible by n (to integer) above the given strings length. This gives you the total number of theoretical 'cells'. Think of n as the number of columns.

  • [*]Two loops are necessary, one within the other. We'll call the outer loop 'i' and the inner one 'j' (use those as the loop counter variables). The outer loop essentially does the theoretical 'rows', and only runs as long as it's less than n, starting from 0 and incrementing by 1 each loop, and the inner loop does the theoretical 'columns', starting from i and adding n to j on each loop, running until not less than value from point 1 above.

  • [*]Starting with a fresh, blank string from the beginning- '', only the inner 'j' loop adds to it with characters from the given string (at j position in the given string) if j is less than the given strings length, else it add's the fill character (in this case 'z') -this will only happen when j is between strings length and total number of theoretical 'cells' (n from point 1 above).

  • [*]Only one function is required, though if you wanted to write a seperate function for determining next number divisible by n which is above y, that would be ok, for determining point number 1 above. Extra tip as far as writing that is concerned, no looping needed, you'll use a '+' a '-' and a '&#37;' operators once each.

  • [/LIST]
    [Hint: create a function which takes a parameter n and a string and returns every nth character from the string.] [/quote]
    As per my tips, the above hint is not at all necessary or required. Also, once you have the 'encoding' figured out, it should not be too much harder to do the decoding. Let me know what you come up with!
    What is the result of applying 5-column transposition to the following text: "IT WAS THE BEST OF TIMES"?"[/quote]
    The answer to that is, if you use the capital letter 'Z' as filler:
    IS IT BOM TEFEWHS SAETTZ
    Copy linkTweet thisAlerts:
    @astupidnameDec 08.2009 — determine the next number divisible by n (to integer) above the given strings length[/quote]
    Actually, oops, I'm slightly incorrect there, should have said[B]:[/B] next number divisible by n (to integer) above the given strings length [B]unless[/B] the given strings length &#37; n is equal to zero, then use the strings length.
    ×

    Success!

    Help @GFUNK383 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.21,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...