import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Vigenère {
public static void main(String[] args) {
JFrame fenster = new JFrame();
fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenster.setVisible(true);
fenster.setSize(700, 500);
fenster.setTitle("Vigenère");
setupContent(fenster);
}
private static void setupContent(JFrame fenster) {
// TODO Auto-generated method stub
JPanel content = new JPanel();
JButton button = new JButton("Ok");
JButton button2 = new JButton("verschlüsseln");
JButton button3 = new JButton("entschlüsseln");
JLabel label = new JLabel("Gebe den Schlüssel ein");
JTextField textField = new JTextField(30);
content.add(label);
content.add(textField);
content.add(button);
fenster.setContentPane(content);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String schlüssel = textField.getText();
char[] key = schlüssel.toCharArray(); // Array Key soll von anderen Methoden aufgerufen werden,
textField.setText("");
label.setText("Gebe den Text ein ");
button.setVisible(false);
content.setLayout(null);
content.add(button2);
content.add(button3);
button2.setBounds(75, 80, 250, 250);
button3.setBounds((625 - 250), 80, 250, 250);
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int richtung = 1;
String Text1 = textField.getText();
char[] Text = Text1.toCharArray();
char[] encrypted = crypt(Text, key, richtung); // Key wird nicht erkannt
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int richtung = 0;
String Text2 = textField.getText();
char[] Text = Text2.toCharArray();
char[] decrypted = crypt(Text, key, richtung);
}
});
}
public static char[] crypt(char[] Text, char[] key, int richtung) {
char[] output = new char[Text.length];
for (int i = 0; i < Text.length; i++) {
if (richtung == 1) {
int result = (Text[i] + key[i % key.length]) % 128;
output[i] = (char) result;
} else if (richtung == 0) {
int result;
if (Text[i] - key[i % key.length] < 0)
result = (Text[i] - key[i % key.length]) + 128;
else
result = (Text[i] - key[i % key.length]) % 128;
output[i] = (char) result;
}
}
return output;
}
}