-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryption.java
More file actions
33 lines (27 loc) · 991 Bytes
/
Encryption.java
File metadata and controls
33 lines (27 loc) · 991 Bytes
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
import java.io.IOException;
import java.nio.CharBuffer;
public class Encryption {
static String pathSource = "TextBeforeEncryption.txt";
static String pathDestination = "TextAfterEncryption.txt";
public static void encryption(int key) {
CharBuffer ch = WorkWithFile.read(pathSource);
StringBuilder output = new StringBuilder();
for (int i = 0; i < ch.length(); i++) {
char c = ch.charAt(i);
if (Alphabet.SIMBOLS.indexOf(c) != -1) {
int newIndex = getNewIndex(key, c);
output.append(Alphabet.SIMBOLS.charAt(newIndex));
} else {
output.append(c);
}
}
try {
WorkWithFile.write(pathDestination, output);
} catch (IOException e) {
e.printStackTrace();
}
}
private static int getNewIndex(int key, char c) {
return (Alphabet.SIMBOLS.indexOf(c) + key) % Alphabet.SIMBOLS.length();
}
}