diff options
author | Leonard Kugis <leonard@kug.is> | 2022-04-25 18:43:46 +0200 |
---|---|---|
committer | Leonard Kugis <leonard@kug.is> | 2022-04-25 18:43:46 +0200 |
commit | c9dd9469183d95c6c2f4d01e3d6365ec57386a65 (patch) | |
tree | 07ec933179eced02eec8a70f8b64c9281d453457 /src/algorithmus/Vigenere.java |
Diffstat (limited to 'src/algorithmus/Vigenere.java')
-rwxr-xr-x | src/algorithmus/Vigenere.java | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/algorithmus/Vigenere.java b/src/algorithmus/Vigenere.java new file mode 100755 index 0000000..9f429b7 --- /dev/null +++ b/src/algorithmus/Vigenere.java @@ -0,0 +1,99 @@ +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 Vigenere extends Algorithmus {
+
+ private String name, key;
+ private char[] chars;
+
+ public Vigenere() {
+ name = "Vigenere";
+ 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("Vigenere");
+ 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 = 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];
+ }
+ 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;
+ }
+
+}
|