summaryrefslogtreecommitdiff
path: root/src/algorithmus/Caesar.java
diff options
context:
space:
mode:
authorLeonard Kugis <leonard@kug.is>2022-04-25 18:43:46 +0200
committerLeonard Kugis <leonard@kug.is>2022-04-25 18:43:46 +0200
commitc9dd9469183d95c6c2f4d01e3d6365ec57386a65 (patch)
tree07ec933179eced02eec8a70f8b64c9281d453457 /src/algorithmus/Caesar.java
Initial commitHEADmaster
Diffstat (limited to 'src/algorithmus/Caesar.java')
-rwxr-xr-xsrc/algorithmus/Caesar.java96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/algorithmus/Caesar.java b/src/algorithmus/Caesar.java
new file mode 100755
index 0000000..1404272
--- /dev/null
+++ b/src/algorithmus/Caesar.java
@@ -0,0 +1,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;
+ }
+
+}