summaryrefslogtreecommitdiff
path: root/src/algorithmus/XOR.java
blob: 0bbfef32fdeea1ddaa574f3fcbc3261b12c0b9b6 (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
97
98
99
100
101
102
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 XOR extends Algorithmus {
	
	private String name, key;
	private String[] folgen;
	private char[] chars;
	
	public XOR() {
		name = "XOR";
		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' };
		folgen = new String[] { "01000001", "01000010", "01000011", "01000100", "01000101", "01000110", "01000111", "01001000", "01001001", "01001010", "01001011", "01001100", "01001101", "01001110", "01001111", "01010000", "01010001", "01010011", "01010100", "01010101", "01010110", "01010111", "01011000", "01011001", "01011010" };
	}

	@Override
	public void options() {
		JFrame frame = new JFrame();
		frame.setLayout(null);
		frame.setTitle("XOR");
		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);
		TextField binary = new TextField();
		String binarystr = "";
		for(int i = 0; i<folgen.length; i++) {
			binarystr += folgen[i] + ";";
		}
		binary.setText(binarystr);
		binary.setBounds(5, 55, 150, 20);
		JButton apply = new JButton("Speichern");
		apply.setBounds(5, 80, 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();
				folgen = binary.getText().split(";");
			}
		});
		frame.add(keyField);
		frame.add(alphabet);
		frame.add(binary);
		frame.add(apply);
		frame.setVisible(true);
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public String encode(String input) {
		String out = "";
		int size = input.length();
		for(int i = 0; i<size; i++)
			out += xor(folgen[Utils.inArr(chars, input.charAt(i))], folgen[Utils.inArr(chars, key.charAt(i%key.length()))]) + ';';
		return out;
	}

	@Override
	public String decode(String input) {
		String out = "";
		String[] in = input.split(";");
		for(int i = 0; i<in.length; i++)
			out += xor(in[i], folgen[Utils.inArr(chars, key.charAt(i%key.length()))]) + ';';
		return out;
	}
	
	private String xor(String one, String two) {
		String out = "";
		for(int x = 0, len = one.length(); x<len; x++) {
			if((one.charAt(x) == '0' && two.charAt(x) == '0') || (one.charAt(x) == '1' && two.charAt(x) == '1'))
				out += '0';
			else if((one.charAt(x) == '0' && two.charAt(x) == '1') || (one.charAt(x) == '1' && two.charAt(x) == '0'))
				out += '1';
		}
		return out;
	}

}