summaryrefslogtreecommitdiff
path: root/src/algorithmus/Caesar.java
blob: 1404272ff04b405a80659db12fc610838fe70ad4 (plain)
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
package algorithmus;

import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;

public class Caesar extends Algorithmus {
	
	private String name;
	private char[] chars;
	private int v;
	
	public Caesar() {
		name = "Caesar";
		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',
				'Ä', 'Ö', 'Ü', 'ä', 'ö', 'ü', 'ß', ' '};
		v = 13;
	}
	
	public void options() {
		JFrame frame = new JFrame();
		frame.setLayout(null);
		frame.setTitle("Caesar");
		frame.setSize(200, 150);
		TextField alphabet = new TextField();
		String alphaText = "";
		for(int i = 0; i<chars.length; i++) {
			alphaText += chars[i];
		}
		alphabet.setText(alphaText);
		alphabet.setBounds(5, 5, 150, 20);
		JLabel label = new JLabel("Verschiebung:");
		label.setBounds(5, 30, 100, 20);
		JSpinner spinner = new JSpinner();
		spinner.setBounds(100, 30, 50, 20);
		spinner.setValue(v);
		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);
				}
				v = (int) spinner.getValue();
			}
		});
		frame.add(alphabet);
		frame.add(label);
		frame.add(spinner);
		frame.add(apply);
		frame.setVisible(true);
	}
	
	public String getName() {
		return name;
	}
	
	public String encode(String input) {
		String out = "";
		for(int i = 0, size = input.length(); i<size; i++) {
			for(int x = 0; x<chars.length; x++) {
				if(chars[x] == input.charAt(i)) {
					out += chars[(x + v) % chars.length];
				}
			}
		}
		return out;
	}
	
	public String decode(String input) {
		String out = "";
		int pos = 0;
		for(int i = 0, size = input.length(); i<size; i++) {
			for(int x = 0; x<chars.length; x++) {
				if(chars[x] == input.charAt(i)) {
					pos = (x - v);
					if(pos<=0)
						pos += chars.length;
					out += chars[pos % chars.length];
				}
			}
		}
		return out;
	}

}