-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptography.java
More file actions
90 lines (63 loc) · 2.01 KB
/
Copy pathcryptography.java
File metadata and controls
90 lines (63 loc) · 2.01 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
public class cryptography {
public static String caesarShift(String MSG, int S) {
String CODED = "";
char[] LETTERS = MSG.toCharArray();
for (int i = 0; i < LETTERS.length; i++) {
if (LETTERS[i] != ' ') {
int ASNUM = (int) LETTERS[i];
ASNUM += S;
CODED += (char) ASNUM;
} else {
CODED += LETTERS[i];
}
}
return CODED;
}
public static String caesarShiftClassic(String MSG, int S) {
String CODED = "";
char[] LETTERS = MSG.toLowerCase().toCharArray();
for (int i = 0; i < LETTERS.length; i++) {
if (LETTERS[i] != ' ') {
int ASNUM = (int) LETTERS[i];
ASNUM += S;
//Loop through the lowercase letters only.
while (ASNUM > 122) {
ASNUM -= 26;
}
while (ASNUM < 97) {
ASNUM += 26;
}
//-----------------------------------------
CODED += (char) ASNUM;
} else {
CODED += LETTERS[i];
}
}
return CODED;
}
public static String caesarDecoder(String CODE, int S) {
return caesarShift(CODE, -S);
}
public static String classicDecoder(String CODE, int S) {
return caesarShiftClassic(CODE, -S);
}
public static int freqAnalysis(String msg){
int[] counts = new int[100000];
char[] array = msg.toCharArray();
for (int i = 0; i < array.length; i++) {
int ASNUM = (int) array[i];
if(ASNUM > 33) {
counts[ASNUM] += 1;
}
}
int highIndex = -1;
int highValue = 0;
for (int i = 0; i < counts.length; i++) {
if(counts[i] > highValue){
highIndex = i;
highValue = counts[i];
}
}
return highIndex-101;
}
}