/    Sign up×
Community /Pin to ProfileBookmark

Decoding Cipher

I found this code online and need to find a way to modify it to fit my requirements.

[CODE]<SCRIPT LANGUAGE=”JavaScript”>
function Encode(key, message)
// Given : key is a string of the 26 letters in arbitrary order,
// message is the string to be encoded using the key
// Returns: the coded version of message using the substitution key
{
var alphabet, coded, i, ch, index;

alphabet = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;

coded = “”;
for (i = 0; i < message.length; i++) { // for as many letters as there are
ch = message.charAt(i); // access the letter in the message
index = alphabet.indexOf(ch); // find its position in alphabet
if (index == -1) { // if it’s not a 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
}
}
return coded;
}
</SCRIPT>[/CODE]

It needs to be aranged to decode the following:

[FONT=Fixedsys]
ABCDEFGHIJKLM
|||||||||||||
NOPQRSTUVWXYZ[/FONT]

For instance: If the message you input says ‘hello’, the code would spit it out as ‘uryyb’. It simply substitutes the letters on the top with the letters on the bottom, and visa-versa. Can anyone help?

to post a comment
JavaScript

22 Comments(s)

Copy linkTweet thisAlerts:
@Willy_DuittJul 14.2005 — I found this code online and need to find a way to modify it to fit my requirements.

[CODE]<SCRIPT LANGUAGE="JavaScript">
function Encode(key, message)
// Given : key is a string of the 26 letters in arbitrary order,
// message is the string to be encoded using the key
// Returns: the coded version of message using the substitution key
{
var alphabet, coded, i, ch, index;

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

coded = "";
for (i = 0; i < message.length; i++) { // for as many letters as there are
ch = message.charAt(i); // access the letter in the message
index = alphabet.indexOf(ch); // find its position in alphabet
if (index == -1) { // if it's not a 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
}
}
return coded;
}
</SCRIPT>[/CODE]


It needs to be aranged to decode the following:

[FONT=Fixedsys]

ABCDEFGHIJKLM

|||||||||||||

NOPQRSTUVWXYZ[/FONT]

For instance: If the message you input says 'hello', the code would spit it out as 'uryyb'. It simply substitutes the letters on the top with the letters on the bottom, and visa-versa. Can anyone help?[/QUOTE]


What's the problem??

The script is completely commented explaining what each line is doing...

What don't you understand??

How about explaining what you do understand before someone attempts to write you another [i]encryptor[/i] script that you will more likely than not, not understand either...

Besides - this appears to be [i]homework[/i]....

.....Willy
Copy linkTweet thisAlerts:
@dz_boyauthorJul 14.2005 — I can pretty much understand what it is currently doing, but it needs to be changed. The key in this script is changable, I want mine to be permanent. Mine needs two keys that should be interchangable, 13 letters each. I don't know how to make the logic moving between one key and another.

Does that help?
Copy linkTweet thisAlerts:
@graatzJul 14.2005 — How about explaining what you do understand before someone attempts to write you another encryptor script that you will more likely than not, not understand either...[/QUOTE]
not to mention it already has the capacity to do exactly what you're asking it to....
Mine needs two keys that should be interchangable, 13 letters each. I don't know how to make the logic moving between one key and another.[/QUOTE]
Two simple substitution cyphers is congruent to one ... it's a little tedious to do it twice when once will suffice
Copy linkTweet thisAlerts:
@Willy_DuittJul 14.2005 — I can pretty much understand what it is currently doing, but it needs to be changed. The key in this script is changable, I want mine to be permanent. Mine needs two keys that should be interchangable, 13 letters each. I don't know how to make the logic moving between one key and another.

Does that help?[/QUOTE]


Create an array and either multiply or divide in half...
Copy linkTweet thisAlerts:
@dz_boyauthorJul 14.2005 — not to mention it already has the capacity to do exactly what you're asking it to....[/QUOTE]


But how?.. I'm new at this...
Copy linkTweet thisAlerts:
@graatzJul 14.2005 — If your goal is exactly what you posted, you can use:

Encode("NOPQRSTUVWXYZABCDEFGHIJKLM",[I]string[/I])

so Encode("NOPQRSTUVWXYZABCDEFGHIJKLM","HELLO") == "URYYB"
Copy linkTweet thisAlerts:
@dz_boyauthorJul 14.2005 — If your goal is exactly what you posted, you can use:

Encode("NOPQRSTUVWXYZABCDEFGHIJKLM",[I]string[/I])

so Encode("NOPQRSTUVWXYZABCDEFGHIJKLM","HELLO") == "URYYB"[/QUOTE]


Where would I put it and what should I remove then?
Copy linkTweet thisAlerts:
@graatzJul 14.2005 — Where would I put it and what should I remove then?[/QUOTE]
What text are you trying to encrypt?
Copy linkTweet thisAlerts:
@Willy_DuittJul 14.2005 — Two simple substitution cyphers is congruent to one ... [/QUOTE]

[i]congruent[/i] - that was a most excellent word - ?

.....Willy
Copy linkTweet thisAlerts:
@dz_boyauthorJul 14.2005 — What text are you trying to encrypt?[/quote]

The value of a textbox or text area...
Copy linkTweet thisAlerts:
@graatzJul 14.2005 — congruent - that was a most excellent word - ?[/QUOTE]
Guess that's what I get for learning basic/intermediate Cryptology from a former cryptoanalyst for the CIA ?
Copy linkTweet thisAlerts:
@graatzJul 14.2005 — The value of a textbox or text area...[/QUOTE]
[CODE]<form name="theForm">
<input name="theInput" onblur="this.value = Encode('NOPQRSTUVWXYZABCDEFGHIJKLM',this.value)">
</form>[/CODE]
Copy linkTweet thisAlerts:
@dz_boyauthorJul 14.2005 — What does 'onBlur' do?
Copy linkTweet thisAlerts:
@Willy_DuittJul 14.2005 — FWIW: Watch nesting double quotes within double qoutes...

This: [i]onblur="theForm.theInput.value = Encode("NOPQRSTUVWXYZABCDEFGHIJKLM",theForm.theInput.value)"[/i]

Can also be written as ([i]using the keyword[/i] [b]this[/b]):

[b]onblur="this.value = Encode('NOPQRSTUVWXYZABCDEFGHIJKLM',this.value)"[/b]

.....Willy
Copy linkTweet thisAlerts:
@graatzJul 14.2005 — d'oh!

i'll fix it in my post....
Copy linkTweet thisAlerts:
@dz_boyauthorJul 14.2005 — OK: Here is what I have... but it doesn't seem to work.

[CODE]
<form name="theForm">
<input name="theInput">
<input type="button" onclick="document.theForm.theInput.value = Encode('NOPQRSTUVWXYZABCDEFGHIJKLM',document.theForm.theInput.value)" value="Code!">
</form>
[/CODE]
Copy linkTweet thisAlerts:
@dz_boyauthorJul 14.2005 — I switched the action to a button, thinking that it would work then, but it didn't, and I just left the button there.
Copy linkTweet thisAlerts:
@Willy_DuittJul 14.2005 — I switched the action to a button, thinking that it would work then, but it didn't, and I just left the button there.[/QUOTE]

You are missing your original [b]Encode[/b] function...

[i]graatz[/i] merely changed the key thru the function call...

Try to pay attention to what's going on...

.....Willy
Copy linkTweet thisAlerts:
@dz_boyauthorJul 15.2005 — I have tweaked the code a little so I can see variables being made and such... that doesn't really matter. What I'm getting now, is an error saying that 'message.length' is not valid. I tried to fix this using the toString on the variable 'message', but it still isn't working. It's probably something stupid that me, being a newbie, just missed. Can someone point it out? Here is the code as I have it now:

[CODE]<html>
<head>
<title>Decode Encrypted Hints!</title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
var message = document.theForm.theInput.value;
var message = message.toString;
function Encode(key)
// Given : key is a string of the 26 letters in arbitrary order,
// message is the string to be encoded using the key
// Returns: the coded version of message using the substitution key
{
var alphabet, coded, i, ch, index;

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

coded = "";
for (i = 0; i < message.length; i++) { // for as many letters as there are
ch = message.charAt(i); // access the letter in the message
index = alphabet.indexOf(ch); // find its position in alphabet
if (index == -1) { // if it's not a 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
}
}
return coded;
}
function Finish(){
document.theForm.theInput.value = Encode('NOPQRSTUVWXYZABCDEFGHIJKLM')
}
</SCRIPT>
<form name="theForm">
<input name="theInput" onblur="JavaScript:Finish()">
<input type="button" value="Yes!">
</form>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@dz_boyauthorJul 15.2005 — I still can't find the problem...
Copy linkTweet thisAlerts:
@dz_boyauthorJul 15.2005 — I fixed two of the errors, but now it's just removing to text, not encrypting it.



<html>

<head>

<title>Decode Encrypted Hints!</title>

</head>

<body>

<SCRIPT LANGUAGE="JavaScript">

function Encode(key)

{

var message = document.theForm.theInput.value;

var message = message.toString;

// Given : key is a string of the 26 letters in arbitrary order,

// message is the string to be encoded using the key

// Returns: the coded version of message using the substitution key

var alphabet, coded, i, ch, index;

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

coded = "";
for (i = 0; i < message.length; i++) { // for as many letters as there are
ch = message.charAt(i); // access the letter in the message
index = alphabet.indexOf(ch); // find its position in alphabet
if (index == -1) { // if it's not a 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
}
}
return coded;
}
function Finish(){
document.theForm.theInput.value = Encode('NOPQRSTUVWXYZABCDEFGHIJKLM')
}

</SCRIPT>

<form name="theForm">

<input name="theInput" onblur="JavaScript:Finish()">

<input type="button" value="Yes!">

</form>

</body>

</html>
Copy linkTweet thisAlerts:
@graatzJul 15.2005 — Delete this line:

[INDENT][B]var message = message.toString;[/B][/INDENT]

and realize that the function will only convert capital letters and ignore all others......
×

Success!

Help @dz_boy 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...