Hi Leute
Ich habe eine Verschlüsselung gebaut und die geht auch soweit.. nur wenn ich die Verschlüsselung rückwärts machen möchte geht es irgendwie nicht. Findet jemand einen Ansatz?
Ich habe eine Verschlüsselung gebaut und die geht auch soweit.. nur wenn ich die Verschlüsselung rückwärts machen möchte geht es irgendwie nicht. Findet jemand einen Ansatz?
Code:
package miguel.gutierrez.thusdasy.secretCode;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JSpinner;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SpinnerNumberModel;
public class GUI extends JFrame {
private JPanel contentPane;
private JTextField textFieldInput;
private JTextField textOutput;
Encoder encoder = new Encoder();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI frame = new GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600,400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Caesarverschl\u00FCsselung");
lblNewLabel.setFont(new Font("Stencil", Font.PLAIN, 25));
lblNewLabel.setBounds(32, 32, 423, 43);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Eingabetext");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 18));
lblNewLabel_1.setBounds(32, 86, 182, 22);
contentPane.add(lblNewLabel_1);
textFieldInput = new JTextField();
textFieldInput.setBounds(32, 117, 496, 49);
contentPane.add(textFieldInput);
textFieldInput.setColumns(10);
JSpinner spinnerSwitchIt = new JSpinner();
spinnerSwitchIt.setModel(new SpinnerNumberModel(new Integer(0), null, null, new Integer(1)));
spinnerSwitchIt.setBounds(150, 274, 44, 37);
contentPane.add(spinnerSwitchIt);
JLabel lblNewLabel_2 = new JLabel("Verschl\u00FCsselungsgrad");
lblNewLabel_2.setBounds(32, 279, 119, 26);
contentPane.add(lblNewLabel_2);
JLabel lblNewLabel_1_1 = new JLabel("Ausgabetext");
lblNewLabel_1_1.setFont(new Font("Tahoma", Font.PLAIN, 18));
lblNewLabel_1_1.setBounds(32, 172, 182, 22);
contentPane.add(lblNewLabel_1_1);
textOutput = new JTextField();
textOutput.setColumns(10);
textOutput.setBounds(32, 198, 496, 49);
contentPane.add(textOutput);
JButton btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String spinner= spinnerSwitchIt.getValue().toString();
int value = Integer.parseInt(spinner);
encoder.setSwitchIt(value);
String text = textFieldInput.getText();
String textdecrypt = textOutput.getText();
if(textFieldInput.getText() != null) {
textOutput.setText(encoder.encrypt(text));
}
if(textOutput.getText()!= null) {
textFieldInput.setText(encoder.decryption(textdecrypt));
}
else {
textOutput.setText("hoppla es läuft was schief du Noob");
}
//verschlüsseln
}
});
btnStart.setBounds(439, 281, 89, 22);
contentPane.add(btnStart);
}
}
Code:
package miguel.gutierrez.thusdasy.secretCode;
import javax.swing.JSpinner;
public class Encoder {
private int switchIt = -1; // verschiebung der Buchstaben -1
private char[] chArray = null;
public Encoder() {
this(3);
}
public Encoder(int switchIt) {
this.chArray = createArray();
setSwitchIt(switchIt);
}
public int getSwitchIt() {
return this.switchIt;
}
public void setSwitchIt(int i) {
this.switchIt = switchIt;
}
public String decrypt(String text) {
setSwitchIt(getSwitchIt() * -1);
text = encrypt(text);
setSwitchIt(getSwitchIt() * -1);
return text;
}
public String decryption(String text) {
StringBuffer decryption = new StringBuffer();
for(int i=0;i<text.length();i++) {
char cur = text.charAt(i);
for(int j=0;j<this.chArray.length; j++) {
if (cur == this.chArray[j])
{ int pos = j + getSwitchIt();
if (pos >= this.chArray.length) {
pos -= this.chArray.length;
}
else if (pos < 0) {
pos += this.chArray.length;
}
cur = this.chArray[pos];
break;
}
}
decryption.append(cur);
}
return decryption.toString();
}
public String encrypt(String text) {
StringBuffer encrypted = new StringBuffer();
for (int i = 0; i < text.length(); i++) {
char cur = text.charAt(i);
for (int j = 0; j < this.chArray.length; j++) {
if (cur == this.chArray[j])
{ int pos = j + getSwitchIt();
if (pos >= this.chArray.length) {
pos -= this.chArray.length;
}
else if (pos < 0) {
pos += this.chArray.length;
}
cur = this.chArray[pos];
break;
}
}
encrypted.append(cur);
}
return encrypted.toString();
}
public char[] createArray() {
char[] ch = new char[26 + 26 + 10 + 33 + 6];
int pos = 0;
for (int i = 32; i < 127; i++) {
ch[pos++] = (char)i;
}
ch[pos++] = 'ä';
ch[pos++] = 'ö';
ch[pos++] = 'ü';
ch[pos++] = 'Ä';
ch[pos++] = 'Ö';
ch[pos++] = 'Ü';
return ch;
}
public char[] getChArray() {
return this.chArray;
}
public void setChArray(char[] chArray) {
this.chArray = chArray;
}
}