This is what i have from a part of a project but don't know how to make a 5 column transposition cipher
Code:<html> <!-- cipher.html by Dave Reed (davereed@creighton.edu) --> <!-- A lab program to accompany A Balanced Introduction to --> <!-- Computer Science, 2nd ed., Pearson Prentice Hall, 2008. --> <!-- --> <!-- This page encodes/decodes messages using a simple --> <!-- substitution cipher (with key entered by the user) --> <!-- ======================================================= --> <head> <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; } </script> <script type="text/javascript"> function Decode(message, 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; } </script> <script type="text/javascript"> function rotateLeft(key) { return key.substring(1,key.length)+ key.charAt(0); } function firstColumn(message2) { var i, answer1; answer1= ""; i=0; while (i<message2.length){ answer1 = answer1 + message2.charAt(i); i=i+5;} return answer1; } function secondColumn(message2) { var i, answer2; i=1; answer2=""; while (i < message2.length) { answer2 = answer2 + message2.charAt(i); i=i+5; } return answer2; } function thirdColumn(message2) { var i, answer3; i=2; answer3=""; while (i < message2.length) { answer3 = answer3 + message2.charAt(i); i=i+5; } return answer3; } function fourthColumn(message2) { var i, answer4; i=3; answer4=""; while (i < message2.length) { answer4 = answer4 + message2.charAt(i); i=i+5; } return answer4; } function fifthColumn(message2) { var i, answer5; i=4; answer5=""; while (i < message2.length) { answer5 = answer5 + message2.charAt(i); i=i+5; } return answer5; } function encodeTranspose(message2) { var coded2; coded2 = firstColumn(message2) + secondColumn(message2) + thirdColumn(message2) + fourthColumn(message2) + fifthColumn(message2); return coded2; } function firstLine(encoded2) { var i, answer6; i=0; answer6=""; while (i < encoded2.length) { answer6 = answer6 + encoded2.charAt(i); i=i+(encoded2.length /= 5); } return answer6; } function secondLine(encoded2) { var i, answer7; i=1; answer7=""; while (i < encoded2.length) { answer7 = answer7 + encoded2.charAt(i); i=i+(encoded2.length /=5); } return answer7; } function thirdLine(encoded2) { var i, answer8; i=2; answer8=""; while (i < encoded2.length) { answer8 = answer8 + encoded2.charAt(i); i=i+(encoded2.length /=5); } return answer8; } function fourthLine(encoded2) { var i, answer9; i=3; answer9=""; while (i < encoded2.length) { answer9 = answer9 + encoded2.charAt(i); i=i+(encoded2.length /=5); } return answer9; } function decodeTranspose(encoded2) { var coded3; coded3 = firstLine(encoded2) + secondLine(encoded2) + thirdLine(encoded2) + fourthLine(encoded2); return coded3; } </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> <td><textarea id="encoded" rows="8" cols="30" onChange="javascript:this.value=this.value.toUpperCase();"></textarea> </td> </tr> </table> </body> </html>



Reply With Quote

Bookmarks