summaryrefslogtreecommitdiff
path: root/src/algorithmus/Blabla.java
blob: e459645c25b9960e4577457ae83a25e93864b826 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package algorithmus;

import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;

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

public class Blabla extends Algorithmus {
	
	private String name, pw;
	private char[] charset;
	
	public Blabla() {
		name = "Blabla";
	}
	
	public void options() {
		charset = 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',
				'Ä', 'Ö', 'Ü', 'ä', 'ö', 'ü', 'ß', ' '};
		JFrame frame = new JFrame();
		frame.setLayout(null);
		frame.setTitle("Blabla");
		frame.setSize(200, 150);
		TextField alphabet = new TextField();
		alphabet.setText("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ÄÖÜäöüß ");
		alphabet.setBounds(5, 5, 150, 20);
		TextField password = new TextField();
		password.setText("Passwort");
		password.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) {
				pw = password.getText();
				String alpha = alphabet.getText();
				charset = new char[alpha.length()];
				alpha.getChars(0, alpha.length(), charset, 0);
			}
		});
		frame.add(alphabet);
		frame.add(password);
		frame.add(apply);
		frame.setVisible(true);
	}
	
	public String getName() {
		return name;
	}
	
	public String encode(String input) {
		int sum = getSum(pw, "UTF8"), length = input.length();
		String out = "";
		byte[] ba = Utils.fill(new byte[64], length), inba = null;
		System.out.println(ba.length);
		try {
			inba = input.getBytes("UTF8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		for(int i = 0; i<length; i++) {
			ba[(i/(length+1))*64] = inba[i];
		}
		//
		for(int i = 0; i<ba.length; i++) {
			if(ba[i] == 0x00)
				out += "0";
			else
				out += ba[i];
		}
		//
		return out;
	}
	
	public String decode(String input) {
		int sum = getSum(pw, "UTF8"), length = input.length();
		byte[] bin = null;
		String str = "";
		try {
			for(int i = length/64; i>=0; i--) {
				bin = Utils.fill(input.substring((i*64)-64, (i*64)).getBytes("UTF8"), 64);
				sum = bin.length * 64;
				for(int x = bin.length; x>0; x--) {
					str += Constants.LETTERS_NUMBERS[(sum-=(bin[x]+x))%62];
				}
			}
		} catch(UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}
	
	public int getSum(String str, String charset) {
		int sum = 0;
		byte[] ba = new byte[] { (byte)0x00 };
		try {
			ba = str.getBytes(charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		for(int i = 0; i<ba.length; i++)
			sum += ba[i];
		return sum;
	}
	
	private int f(int x) {
		return x^2;
	}

}