Vigenere encode and decode : Vigenere « Security « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Event
7. Event onMethod
8. Form Control
9. GUI Components
10. HTML
11. Javascript Collections
12. Javascript Objects
13. Language Basics
14. Node Operation
15. Object Oriented
16. Page Components
17. Security
18. Style Layout
19. Table
20. Utilities
21. Window Browser
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
JavaScript DHTML » Security » Vigenere 
Vigenere encode and decode

<html>
  <head>
    <title>Vignere Cipher</title>
    <!--
      CryptoMX Tools
      Copyright (C2004 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 optionany 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=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>






           
       
Related examples in the same category
w_w_w__.j__av__a__2s___.c___o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.