Einzelnen Beitrag anzeigen
Alt 03.12.04, 19:42   #5 (permalink)
Boar
 
Registriert seit: 03.12.04
Boar Leistung: Facit NTK
Likes: 0
Standard

Ich hab die Caesar-Chiffre für Windows in Java programmiert. Ver- und Entschlüsselung eines kurzen Textes ist möglich.

Hier ist mein Code:

Code:
/*
 * @author: Boar
 *
 * Ver- und Entschlüsselung eines Strings mit der Caesar-Chiffre
 *
 * (c) 17.11.2004
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Caesar extends JFrame implements ActionListener
{
  String text;
  String convText;
  int[] alphabet = new int[26];
  boolean added = false;
  int temp=0;

  JPanel up          = new JPanel();
  JLabel in          = new JLabel("Text-Eingabe:                 ",JLabel.LEFT);
  JTextField eingabe = new JTextField(20);
  
  JPanel middle1  = new JPanel();
  JLabel  info    = new JLabel ("Schlüssel: ");
  JTextField schl = new JTextField(2);
  
  JPanel middle2  = new JPanel();
  JButton verschl = new JButton("Verschlüsseln");
  JButton entschl = new JButton("Entschlüsseln");
  
  JPanel down        = new JPanel();
  JLabel out         = new JLabel("Konvertierte Ausgabe: ");
  JTextField ausgabe = new JTextField(20);
  
  public Caesar()
  {
    for (int i=0; i<26; i++)
      alphabet[i]='A'+i;
      
    this.setTitle("Caesar-Chiffre");
    this.setSize(370,180);
    this.setResizable(false);

    ausgabe.setEditable(false);
    
    up.setLayout(new FlowLayout(FlowLayout.LEFT));
    up.add(in);
    up.add(eingabe);
    
    middle1.setLayout(new FlowLayout(FlowLayout.CENTER));
    middle1.add(info);
    middle1.add(schl);
    middle1.add(verschl);
    middle1.add(entschl);
    
    down.setLayout(new FlowLayout(FlowLayout.LEFT));
    down.add(out);
    down.add(ausgabe);
    
    getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
    getContentPane().add(up);
    getContentPane().add(middle1);
    getContentPane().add(down);
    
    verschl.addActionListener(this);
    entschl.addActionListener(this);
    verschl.setActionCommand("ver");
    entschl.setActionCommand("ent");
  }
  
  public void actionPerformed(ActionEvent e)
  {
    if(e.getActionCommand().equals("ver"))
    {
      ausgabe.setText("");
      convText="   ";
      text = eingabe.getText().toUpperCase().trim();
      for(int i=0; i<text.length(); i++)
      {
        added=false;
        for(int j=0; j<26; j++)
        {
          if(text.charAt(i)==(char)alphabet[j]&& !schl.getText().equals(""))
          {
            temp=text.charAt(i);
            if(temp+Integer.parseInt(schl.getText())>90)
              temp+=(Integer.parseInt(schl.getText())-26);
            else
              temp+=Integer.parseInt(schl.getText());
            convText+=(char)temp;
            added=true;
          }
          else if(added==false&&j==25&&!schl.getText().equals(""))
            convText="   ERROR: Keine Sonderzeichen!";
          else if(added==false&&j==25&&schl.getText().equals(""))
            convText="   ERROR: Kein Schlüssel angegeben!";
          ausgabe.setText(""+convText);
        }
      }
    }
    else if(e.getActionCommand().equals("ent"))
    {
      ausgabe.setText("");
      convText="   ";
      text = eingabe.getText().toUpperCase().trim();
      for(int i=0; i<text.length(); i++)
      {
        added=false;
        for(int j=0; j<26; j++)
        {
          if(text.charAt(i)==(char)alphabet[j]&& !schl.getText().equals(""))
          {
            temp=text.charAt(i);
            if(temp-Integer.parseInt(schl.getText())<65)
              temp=temp+26-Integer.parseInt(schl.getText());
            else
              temp-=Integer.parseInt(schl.getText());
            convText+=(char)temp;
            added=true;
          }
          else if(added==false&&j==25&&!schl.getText().equals(""))
            convText="   ERROR: Keine Sonderzeichen!";
          else if(added==false&&j==25&&schl.getText().equals(""))
            convText="   ERROR: Kein Schlüssel angegeben!";
          ausgabe.setText(""+convText);
        }
      }
    }
  }
  
  public static void main(String[] args)
  {
    Caesar chiffre = new Caesar();
    
    WindowQuitter wquit = new WindowQuitter();
    chiffre.addWindowListener( wquit );
    
    chiffre.setVisible(true);
  }
}

class  WindowQuitter  extends WindowAdapter
{
  public void windowClosing( WindowEvent e )
  {
    System.exit( 0 );
  }
}
PS: Bin noch ziemlicher Anfänger, bin um jeden Verbesserungsvorschlag für meinen Code froh (z.B. Programmierstil usw.)
Boar ist offline   Mit Zitat antworten
 

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