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
|
package algorithmus;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
public class Autokey extends Algorithmus {
private String name, key;
private char[] chars;
public Autokey() {
name = "Autokey";
key = "";
chars = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'Ä', 'Ö', 'Ü', 'ä', 'ö', 'ü', 'ß', ' '};
}
@Override
public void options() {
JFrame frame = new JFrame();
frame.setLayout(null);
frame.setTitle("Autokey");
frame.setSize(200, 150);
TextField keyField = new TextField();
keyField.setText(key);
keyField.setBounds(5, 5, 150, 20);
TextField alphabet = new TextField();
String alphaText = "";
for(int i = 0; i<chars.length; i++) {
alphaText += chars[i];
}
alphabet.setText(alphaText);
alphabet.setBounds(5, 30, 150, 20);
JButton apply = new JButton("Speichern");
apply.setBounds(5, 55, 100, 20);
apply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String alpha = alphabet.getText();
chars = new char[alpha.length()];
for(int i = 0; i<chars.length; i++) {
chars[i] = alpha.charAt(i);
}
key = keyField.getText();
}
});
frame.add(keyField);
frame.add(alphabet);
frame.add(apply);
frame.setVisible(true);
}
@Override
public String getName() {
return name;
}
@Override
public String encode(String input) {
int[][] arr = new int[2][input.length()];
String out = "";
for(int i = 0; i<arr[0].length; i++) {
char inc = input.charAt(i), pc;
if(i<key.length())
pc = key.charAt(i);
else
pc = input.charAt(i-key.length());
for(int x = 0; x<chars.length; x++) {
if(inc == chars[x])
arr[0][i] = x;
if(pc == chars[x])
arr[1][i] = x;
}
out += chars[(arr[0][i]+arr[1][i])%chars.length];
}
return out;
}
@Override
public String decode(String input) {
int[][] arr = new int[2][input.length()];
String out = "";
for(int i = 0; i<arr[0].length; i++) {
char inc = input.charAt(i), pc = key.charAt(i%key.length());
for(int x = 0; x<chars.length; x++) {
if(inc == chars[x])
arr[0][i] = x;
if(pc == chars[x])
arr[1][i] = x;
}
out += chars[((arr[0][i]-arr[1][i])+chars.length)%chars.length];
}
return out;
}
}
|