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.io.*;
20
21 /***
22 * Plugin to analyze letter frequency
23 * @author enigma
24 */
25 public class AnalyzeFrequency implements Plugin {
26 public String getName() {
27 return "Analyze Frequency";
28 }
29 public String getDescription() {
30 return "Analyzes Letter Frequency";
31 }
32 public boolean usesKey() {
33 return false;
34 }
35 public boolean isInformational() {
36 return true;
37 }
38 public int[] doAction(int[] in, int[] key) {
39 int frequencies[] = new int[256];
40 int max = 0;
41 ByteArrayOutputStream out = new ByteArrayOutputStream();
42
43 for (int i=0; i<frequencies.length; i++)
44 frequencies[0] = 0;
45
46 for (int i=0; i<in.length; i++) {
47 int letter = in[i] % 256;
48 max = Math.max(max, ++frequencies[letter]);
49 }
50
51 try{
52 out.write("Letter frequencies:\n".getBytes());
53
54 for (int i = max; i> 0; i--) {
55 for (int j = 0; j < frequencies.length; j++) {
56 if (frequencies[j] == i) {
57 String hexCode = Integer.toString(j, 16);
58 if (hexCode.length() == 1)
59 hexCode = "0" + hexCode;
60 out.write("Character 0x".getBytes());
61 out.write(hexCode.getBytes());
62 out.write(" (ASCII ".getBytes());
63 if ((j >= 0x20) && (j <= 0x7E)) {
64 out.write("\"".getBytes());
65 out.write((char) j);
66 out.write("\") ".getBytes());
67 }else{
68 out.write("unprintable) ".getBytes());
69 }
70 out.write(Integer.toString(frequencies[j]).getBytes());
71 out.write("\n".getBytes());
72 }
73 }
74 }
75 }catch(IOException e){
76 e.printStackTrace();
77 }
78
79 int[] result = new int[out.size()];
80 byte[] data = out.toByteArray();
81 for (int i=0; i<data.length; i++)
82 result[i] = data[i];
83 return result;
84 }
85 }