Results 1 to 5 of 5

Thread: making ciphers with javascript

  1. #1
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default making ciphers with javascript

    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>
    Last edited by djr33; 12-08-2009 at 02:17 AM. Reason: Added [code] tags.

  2. #2
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I am working on the same exact thing right now....I'm guessing we are in the same class haha. Let me know if you figure it out and I'll do the same.

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You must use [code] tags so that we can read your code.

    Additionally, we HELP, but do not solve homework problems for you. Ask a specific question.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    From the assignment:
    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"?

    It is a portion of the assignment, there are several other exercises leading up to this one

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Yes, but the idea is that you should figure it out for yourself (that's what homework is for). You need to ask specific questions so we are helping, rather than just doing (whether or not it is just a small part of the assignment).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •