View Javadoc

1   //Copyright (C) 2004, Brian Enigma <enigma at netninja.com>
2   //This file is part of MagicCodes.
3   //
4   //MagicCodes is free software; you can redistribute it and/or modify
5   //it under the terms of the GNU General Public License as published by
6   //the Free Software Foundation; either version 2 of the License, or
7   //(at your option) any later version.
8   //
9   //MagicCodes is distributed in the hope that it will be useful,
10  //but WITHOUT ANY WARRANTY; without even the implied warranty of
11  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  //GNU General Public License for more details.
13  //
14  //You should have received a copy of the GNU General Public License
15  //along with Foobar; if not, write to the Free Software
16  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  package org.ninjasoft.magiccodes.plugins;
18  
19  /***
20   * Vigenere Decryption
21   * @author enigma
22   */
23  public class VigenereDecrypt implements Plugin {
24  	public String getName() {
25  		return "Vigenere Decryption";
26  	}
27  	public String getDescription() {
28  		return "Decrypt Vigenere ciphers";
29  	}
30  	public boolean usesKey() {
31  		return true;
32  	}
33  	public boolean isInformational() {
34  		return false;
35  	}
36  	public int[] doAction(int[] in, int[] key) {
37          int[] result = new int[in.length];
38          String keyStr = strip(key);
39          for (int i=0; i<in.length; i++) {
40              char letter = (char) in[i];
41              if (((letter >= 'a') && (letter <= 'z')) ||
42                  ((letter >= 'A') && (letter <= 'Z'))) {
43                      letter = decodeLetters(letter, keyStr.charAt(i % keyStr.length()));
44              }
45              result[i] = letter;
46          }    
47          return result;
48  	}
49      public char decodeLetters(char letter1, char letter2) {
50          int l1 = Character.toUpperCase(letter1) - 'A';
51          int l2 = Character.toUpperCase(letter2) - 'A';
52          int l3 = (l1 - l2 + 26) % 26;
53          return (char) (l3 + 'A');
54      }
55      public String strip(int[] s) {
56          StringBuffer result = new StringBuffer();
57          for (int i=0; i<s.length; i++) {
58              char c = Character.toUpperCase((char) s[i]);
59              if ((c >= 'A') && (c <= 'Z'))
60                  result.append(c);
61          }
62          return result.toString();
63      }
64  }