-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttackCaesar.java
More file actions
123 lines (110 loc) · 3.76 KB
/
Copy pathAttackCaesar.java
File metadata and controls
123 lines (110 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.sql.*;
/**
* Created by Ruby on 2/22/2016.
*/
public class AttackCaesar {
public static void main(String[] args) {
Attack atk1=new Attack();
atk1.calculateSi("KHOOR ZRUOG");
System.out.println("Processing probable words...Please wait...");
atk1.decrpt(atk1.findGreatestSiIndex());
}
}
class Attack{
private String plainTxt=null;
private String decryptedTxt=null;
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String URL = "jdbc:mysql://localhost/dictionary";
// Database credentials
static final String USER = "root";
static final String PASS = "";
static Connection conn = null;
static PreparedStatement stSQL=null;
String characters="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private double [] pVal={0.080, 0.015, 0.030, 0.040, 0.130, 0.020, 0.015, 0.060, 0.065, 0.005, 0.005, 0.035, 0.030, 0.070, 0.080, 0.020, 0.002, 0.065, 0.060, 0.090, 0.030, 0.010, 0.015, 0.005, 0.020, 0.002};
private double [] siarr=new double[26];
private double reqFreq;
void calculateSi(String plainTxt){
this.plainTxt=plainTxt;
int intermediateVal=0;
int lenWithoutSpace=plainTxt.replaceAll(" ", "").length();
reqFreq=1/(double)lenWithoutSpace;
System.out.println(reqFreq);
for (int i=0; i<26; i++){
for (int j=0; j<plainTxt.length(); j++) {
if (plainTxt.charAt(j)!=' '){
intermediateVal=characters.indexOf(plainTxt.charAt(j))-i;
siarr[i]+=reqFreq*pVal[(intermediateVal+26)%26];
}
}
}
}
int findGreatestSiIndex(){
int grtIndex=0;
double greatest=siarr[0];
for (int i=0; i<siarr.length; i++){
if (siarr[i]>greatest){
greatest=siarr[i];
grtIndex=i;
}
}
return grtIndex;
}
void decrpt(int index){
StringBuilder decrypted=new StringBuilder(plainTxt);
char ch;
for (int i=0; i<plainTxt.length(); i++){
ch= plainTxt.charAt(i);
if (ch!=' '){
decrypted.setCharAt(i, characters.charAt(((characters.indexOf(ch) - index) + 26) % 26));
}
}
this.decryptedTxt=decrypted.toString();
System.out.println(this.decryptedTxt);
searchAvailability(this.decryptedTxt);
}
void searchAvailability(String decrypted){
String [] words;
words=decrypted.split(" ");
boolean available=true;
for (int i=0; i<words.length; i++){
available=checkAvailability(words[i]);
if (!available){
break;
}
}
if (!available){
siarr[findGreatestSiIndex()]=0;
decrpt(findGreatestSiIndex());
}else{
System.out.println("\nThe decrypted text is:" + this.decryptedTxt);
}
}
boolean checkAvailability(String word){
boolean avail=true;
//Load Driver Class
try {
Class.forName(JDBC_DRIVER);
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
ex.printStackTrace();
System.exit(1);
}
try{
conn=DriverManager.getConnection(URL, USER, PASS);
String str="select * from words where strings LIKE'"+ word.toLowerCase() + "%'";
stSQL=conn.prepareStatement(str);
ResultSet rec=stSQL.executeQuery();
if (!rec.next()){
avail=false;
}else{
avail=true;
}
}catch(SQLException e){
e.printStackTrace();
}
return avail;
}
}