-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrypher.java
More file actions
142 lines (129 loc) · 4.21 KB
/
Crypher.java
File metadata and controls
142 lines (129 loc) · 4.21 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @version 2.0
* @author github.com/Attupatil
* @since 2020-10-19T07:09:54Z
* @updated_at: "2020-11-12T13:19:28Z",
* @pushed_at: "2020-11-12T13:19:26Z",
* @git_url: "git://github.com/Attupatil/DataSecurityEncryptDecrypt.git",
* @ssh_url: "git@github.com:Attupatil/DataSecurityEncryptDecrypt.git",
* @clone_url: "https://github.com/Attupatil/DataSecurityEncryptDecrypt.git",
* @svn_url: "https://github.com/Attupatil/DataSecurityEncryptDecrypt",
*/
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Crypher {
interface Algoritm {
char decrypt(char ch, int key);
char encrypt(char ch, int key);
}
static class Unicode implements Algoritm {
@Override
public char decrypt(char ch, int key) {
int dec = ch - key;
return (char) (dec < 0 ? (128 - dec) : dec);
}
@Override
public char encrypt(char ch, int key) {
return (char) ((ch + key) % 128);
}
}
static class Shift implements Algoritm {
@Override
public char decrypt(char ch, int key) {
if (ch >= 'a' && ch <= 'z') {
int dec = ch - 'a' - key % 26;
return (char) (dec < 0 ? ('z' + dec + 1) : 'a' + dec);
}
if (ch >= 'A' && ch <= 'Z') {
int dec = ch - 'A' - key % 26;
return (char) (dec < 0 ? ('Z' + dec + 1) : 'A' + dec);
}
return ch;
}
@Override
public char encrypt(char ch, int key) {
if (ch >= 'a' && ch <= 'z') {
return (char) (((ch - 'a' + key) % 26) + 'a');
}
if (ch >= 'A' && ch <= 'Z') {
return (char) (((ch - 'A' + key) % 26) + 'A');
}
return ch;
}
}
public static void main(String[] args) {
int key = 0;
String text = "";
String mode = "enc";
String alg = "shift";
String out = null;
for (int i = 0; i < args.length; i += 2) {
switch (args[i]) {
case "-mode": {
mode = args[i + 1];
break;
}
case "-key": {
key = Integer.parseInt(args[i + 1]);
break;
}
case "-data": {
text = args[i + 1];
break;
}
case "-in": {
if (text.isEmpty()) {
try {
text = new String(Files.readAllBytes(Paths.get(args[i + 1])));
} catch (IOException e) {
System.out.println("Error while looking for file.. " + e.getMessage());
}
}
break;
}
case "-out": {
out = args[i + 1];
break;
}
case "-alg": {
alg = args[i + 1];
break;
}
default: {
System.out.println("Unknown parameter type");
return;
}
}
}
Algoritm algoritm = null;
switch (alg) {
case "shift":
algoritm = new Shift();
break;
case "unicode":
algoritm = new Unicode();
break;
}
StringBuilder data = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (mode.equals("enc")) {
data.append(algoritm.encrypt(ch, key));
} else if (mode.equals("dec")) {
data.append(algoritm.decrypt(ch, key));
} else {
data.append(ch);
}
}
if (out == null) {
System.out.println(data);
} else {
try (FileWriter writer = new FileWriter(out)) {
writer.write(data.toString());
} catch (IOException e) {
System.out.print("An exception occurs " + e.getMessage());
}
}
}
}