summaryrefslogtreecommitdiff
path: root/src/algorithmus/XOR.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/algorithmus/XOR.java')
-rwxr-xr-xsrc/algorithmus/XOR.java102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/algorithmus/XOR.java b/src/algorithmus/XOR.java
new file mode 100755
index 0000000..0bbfef3
--- /dev/null
+++ b/src/algorithmus/XOR.java
@@ -0,0 +1,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;
+ }
+
+}