-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesar1.java
More file actions
49 lines (33 loc) · 1.7 KB
/
Caesar1.java
File metadata and controls
49 lines (33 loc) · 1.7 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
public class Caesar1 {
public static void main(String[] args) {
String klartext = IO.getString("Bitte geben Sie den Klartext ohne Sonderlaute in GROßBUCHSTABEN ein.");
int k = IO.getInt("Bitte geben Sie die Schlüssellänge an.");
String geheimtext = Caesar1.encode(klartext, k);
IO.show(geheimtext);
IO.show("Der Klartext lautet: " + Caesar1.decode(geheimtext, k));
}
public static String encode(String text, int key) {
String klartextalphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String geheimtextalphabet = klartextalphabet.substring(key) + klartextalphabet.substring(0, key);
String geheimtext = "";
for (int i=0; i < text.length(); i++) {
char buchstabe = text.charAt(i);
int stelle = klartextalphabet.indexOf(buchstabe);
geheimtext = geheimtext + geheimtextalphabet.charAt(stelle);
}
return geheimtext;
}
public static String decode(String geheimText, int usedKey) {
String klartextalphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int index = klartextalphabet.length()-(usedKey);
String geheimtextalphabet = klartextalphabet.substring(index) + klartextalphabet.substring(0, index);
System.out.println(geheimtextalphabet);
String geheimtext = "";
for (int i=0; i < geheimText.length(); i++) {
char buchstabe = geheimText.charAt(i);
int stelle = klartextalphabet.indexOf(buchstabe);
geheimtext = geheimtext + geheimtextalphabet.charAt(stelle);
}
return geheimtext;
}
}