1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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
71 }
72
73
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 }