forked from MasterFlomaster1/JFXCrypto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCipher.java
More file actions
113 lines (94 loc) · 3.16 KB
/
Copy pathSimpleCipher.java
File metadata and controls
113 lines (94 loc) · 3.16 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
package Cipher;
public class SimpleCipher {
private static String allChars = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ,.!?:;()*&^%$#@-=+_");
private static String combination = null;
private static String key = null;
public SimpleCipher() {
updateCombination();
}
public static String encryption(String text) {
StringBuilder encrypted = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
int index = 0;
char a = text.charAt(i);
for (int j = 0; j < allChars.length(); j++) {
if (a == allChars.charAt(j)) {
index = j;
break;
} else {
index++;
}
}
encrypted.append(combination.charAt(index));
}
return encrypted.toString();
}
public static String decryption(String text) {
StringBuilder decrypted = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
int index = 0;
char a = text.charAt(i);
for (int j = 0; j < combination.length(); j++) {
if (a == combination.charAt(j)) {
index = j;
break;
} else
index++;
}
decrypted.append(allChars.charAt(index));
}
return decrypted.toString();
}
public static void updateCombination() {
int[] arr = new int[allChars.length()];
randomSort(arr);
String temp = "";
for (int value : arr) {
temp = temp.concat(String.valueOf(combination.charAt(value - 1)));
}
combination = temp;
}
public static void setKey(String key) {
}
public static String getKey() {
return key;
}
private static int[] getCombFromKey() {
StringBuilder num = new StringBuilder();
int[] mixPos = new int[allChars.length()];
int index = 0;
for (int i = 0; i < combination.length(); i++) {
if (Character.isDigit(combination.charAt(i))) {
num.append(combination.charAt(i));
} else {
mixPos[index] = Integer.parseInt(num.toString());
index++;
num = new StringBuilder();
}
}
return mixPos;
}
private static void randomSort(int[] inputArray){
int count = 0;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < inputArray.length; i++){
int randomNumber = (int)(1+(Math.random()*inputArray.length));
inputArray[i] = randomNumber;
count++;
for (int q = 0; q < count; q++){
if (q==i) {
builder.append(randomNumber).append(allChars.charAt((int)(Math.random()*56+1)));
break;
}
if (randomNumber == inputArray[q]){
i--;
count--;
break;
}
}
}
key = builder.toString();
}
private static void randomSort() {
}
}