PDA

View Full Version : making ciphers with javascript

brooklyn
12-07-2009, 10:40 PM
This is what i have from a part of a project but don't know how to make a 5 column transposition cipher

<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. -->
<!-- -->
<!-- substitution cipher (with key entered by the user) -->
<!-- ======================================================= -->

<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)
{
i=0;
while (i<message2.length){
i=i+5;}
}

function secondColumn(message2)
{

i=1;
while (i < message2.length) {
i=i+5;
}

}

function thirdColumn(message2)
{

i=2;
while (i < message2.length) {
i=i+5;
}

}

function fourthColumn(message2)
{

i=3;
while (i < message2.length) {
i=i+5;
}

}

function fifthColumn(message2)
{

i=4;
while (i < message2.length) {
i=i+5;
}

}

function encodeTranspose(message2)
{

var coded2;
coded2 = firstColumn(message2) + secondColumn(message2) +
thirdColumn(message2) + fourthColumn(message2) + fifthColumn(message2);
return coded2;

}

function firstLine(encoded2)
{

i=0;
while (i < encoded2.length) {
i=i+(encoded2.length /= 5);
}
}

function secondLine(encoded2)
{

i=1;
while (i < encoded2.length) {
i=i+(encoded2.length /=5);
}
}

function thirdLine(encoded2)
{

i=2;
while (i < encoded2.length) {
i=i+(encoded2.length /=5);
}
}

function fourthLine(encoded2)
{

i=3;
while (i < encoded2.length) {
i=i+(encoded2.length /=5);
}
}

function decodeTranspose(encoded2)
{

var coded3;
coded3 = firstLine(encoded2) + secondLine(encoded2) + thirdLine(encoded2) +
fourthLine(encoded2);
return coded3;

}

</script>

<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>

steveo4103
12-08-2009, 01:58 AM
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.

djr33
12-08-2009, 02:15 AM
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.

steveo4103
12-08-2009, 03:00 AM
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

djr33
12-09-2009, 12:17 AM
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).