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  import java.text.DecimalFormat;
20  
21  /***
22   * Compute the index of coincidence-related data for XOR ciphers, using
23   * the pseudocode:
24   * for i is 1 to length(cyphertext) do
25   *     cyphertext2 = shift(cyphertext,i)
26   *     for j is 1 to length(cyphertext) do
27   *           if (cyphertext[i] = cyphertext2[j]) then do 
28   *                counter = counter +1
29   *     index = (counter / length(cyphertext))
30   *     if index > .4 then do return i
31   *
32   * (Note: you might have to change .4 to something else depending on your
33   * character set.)
34   * as described on http://md2600.net/newsletter/issue2/122.html
35   * 
36   * @author enigma
37   */
38  public class XorAnalysis implements Plugin {
39      private DecimalFormat format = new DecimalFormat("###");
40      private DecimalFormat dotFormat = new DecimalFormat("0.0000");
41  	public String getName() {
42  		return "XOR:Analysis";
43  	}
44  	public String getDescription() {
45  		return "Calculates the Index of Coincidence for XOR ciphers";
46  	}
47  	public boolean usesKey() {
48  		return false;
49  	}
50  	public boolean isInformational() {
51  		return true;
52  	}
53  	public int[] doAction(int[] in, int[] key) {
54          // String buffer for output
55          StringBuffer result = new StringBuffer();
56          result.append("XOR Index of Coincidence Data\n");
57          for (int shift=1; shift<in.length; shift++) {
58              int counter = 0;
59              //cyphertext2 = shift(cyphertext,i)
60              for (int i=0; i<in.length; i++) {
61                    if (in[i] == in[(i + shift) % in.length]) 
62                         counter++;
63              }
64              double index = (double) counter / (double) in.length;
65              result.append("Shift:");
66              result.append(format.format(shift));
67              result.append("  Index:");
68              result.append(dotFormat.format(index));
69              result.append("\n");
70              //if index > .4 then do return i
71          }
72          // Flatten to an array for returning
73          //System.out.println(result.toString());
74          int[] resultArray = new int[result.length()];
75          for (int i=0; i<resultArray.length; i++)
76              resultArray[i] = result.charAt(i);
77          return resultArray;
78  	}
79  }