JavaScript DHTML/Security/Vigenere
Версия от 09:59, 26 мая 2010; (обсуждение)
Vigenere encode and decode
<html>
<head>
<title>Vignere Cipher</title>
<!--
CryptoMX Tools
Copyright (C) 2004 - 2006 Derek Buitenhuis
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-->
</head>
<body bgcolor=#FFFFFF text=#000000>
<td>
<script>
function Vigenere (input, key, forward)
{
if (key == null)
key = "";
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz";
// Validate key:
key = key . toUpperCase ();
var key_len = key . length;
var i;
var adjusted_key = "";
for (i = 0; i < key_len; i ++)
{
var key_char = alphabet . indexOf (key . charAt (i));
if (key_char < 0)
continue;
adjusted_key += alphabet . charAt (key_char);
}
key = adjusted_key;
key_len = key . length;
if (key_len == 0)
{
alert ("You forgot to supply a key!");
key = "a";
key_len = 1;
}
// Transform input:
var input_len = input . length;
var output = "";
var key_index = 0;
var in_tag = false;
for (i = 0; i < input_len; i ++)
{
var input_char = input . charAt (i);
if (input_char == "<")
in_tag = true;
else if (input_char == ">")
in_tag = false;
if (in_tag)
{
output += input_char;
continue;
}
var input_char_value = alphabet . indexOf (input_char);
if (input_char_value < 0)
{
output += input_char;
continue;
}
var lowercase = input_char_value >= 26 ? true : false;
if (forward)
input_char_value += alphabet . indexOf (key . charAt (key_index));
else
input_char_value -= alphabet . indexOf (key . charAt (key_index));
input_char_value += 26;
if (lowercase)
input_char_value = input_char_value % 26 + 26;
else
input_char_value %= 26;
output += alphabet . charAt (input_char_value);
key_index = (key_index + 1) % key_len;
}
return output;
}
function runcoder (dir)
{
document . form . output . value = Vigenere (document . form . input . value, document . form . key . value, dir);
/* A bug in IE prevents this section from working correctly.
with (document . form)
{
output . value = Vigenere (input . value, key . value, dir);
}
*/
}
</script>
<form name=form>
<table border cellspacing="2">
<tr>
<td>
Input:<br>
<input type=button onClick="this.form.input.value="";" value="clear">
</td>
<td>
<textarea name=input rows=10 cols=60 wrap=virtual></textarea>
</td>
</tr>
<tr>
<td>
Key:<br>
<input type=button onClick="this.form.key.value="";" value="clear">
</td>
<td>
<textarea name=key rows=5 cols=60 wrap=virtual></textarea>
</td>
</tr>
<tr>
<td>Coding direction:</td>
<td>
<input type=button value="encode" onClick="runcoder (true);">
<input type=button value="decode" onClick="runcoder (false);">
</td>
</tr>
<td>
Output:<br>
<input type=button onClick="this.form.output.value="";" value="clear">
</td>
<td>
<textarea name=output rows=10 cols=60 wrap=virtual></textarea>
</td>
</table>
</form>
</body>
</html>