Dies ist der Quelltext für das Vigenere-Chiffre-Programm:
package Testat2;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/**
* Title: Testat2
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
class Vigenere{
public Vigenere(String str){
schlüssel=Vigenere.format(str);
if(schlüssel=="")
schlüssel="a";
}
static String format(String str){
str=str.toLowerCase();
String neuklar="";
for(int i=0;i<str.length();i++){
if (Character.isLetter(str.charAt(i)))
neuklar+=String.valueOf(str.charAt(i));
}
return neuklar;
}
static byte[] BnachZahl(byte[] Feld){
for(int i=0;i<Feld.length;i++)
Feld[i]-=97;
return Feld;
}
static String ZahlnachB(byte[] Feld){
for(int i=0;i<Feld.length;i++)
Feld[i]+=97;
return (new String(Feld));
}
static String chiff(String str){
int j=0;
klar=Vigenere.format(str);
byte[] klarFeld=new byte[klar.length()];
klarFeld=klar.getBytes();
byte[] schlüsselFeld=new byte[schlüssel.length()];
schlüsselFeld=schlüssel.getBytes();
byte[] codeFeld=new byte[klar.length()];
klarFeld=Vigenere.BnachZahl(klarFeld);
schlüsselFeld=Vigenere.BnachZahl(schlüsselFeld);
for(int i=0;i<klarFeld.length;i++){
if(j>=schlüsselFeld.length)
j=0;
codeFeld[i]=(byte)((schlüsselFeld[j]+klarFeld[i])%26);
j++;
}
code=Vigenere.ZahlnachB(codeFeld);
return code;
}
static String dechiff(String str){
int j=0;
code=Vigenere.format(str);
byte[] codeFeld=new byte[code.length()];
codeFeld=code.getBytes();
byte[] schlüsselFeld=new byte[schlüssel.length()];
schlüsselFeld=schlüssel.getBytes();
byte[] klarFeld=new byte[code.length()];
codeFeld=Vigenere.BnachZahl(codeFeld);
schlüsselFeld=Vigenere.BnachZahl(schlüsselFeld);
for(int i=0;i<codeFeld.length;i++){
if(j>=schlüsselFeld.length)
j=0;
byte ergebnis=(byte)(codeFeld[i]-schlüsselFeld[j]);
if(ergebnis<0)
ergebnis=(byte)(26+ergebnis);
klarFeld[i]=ergebnis;
j++;
}
klar=Vigenere.ZahlnachB(klarFeld);
return klar;
}
private static String klar;
private static String schlüssel;
private static String code;
}
public class Testat2 extends Applet{
public void init(){
fenster f=new fenster();
add(f);
}
public class fenster extends Panel {
fenster(){
GridBagLayout gridbag=new GridBagLayout();
setLayout(gridbag);
Panel panel=new Panel();
panel.add(chiff);
panel.add(dechiff);
add(panel);
GridBagConstraints constraints=new GridBagConstraints();
constraints.weightx=100;
constraints.weighty=100;
constraints.gridx=1;
constraints.gridy=4;
constraints.gridwidth=4;
constraints.gridheight=1;
gridbag.setConstraints(panel,constraints);
Panel panel2=new Panel();
Label klar=new Label("Klartext:");
panel2.add(klar);
panel2.add(eingabeKlar);
add(panel2);
constraints.gridx=1;
constraints.gridy=1;
constraints.gridwidth=4;
constraints.gridheight=1;
gridbag.setConstraints(panel2,constraints);
Panel panel3=new Panel();
Label schlüssel=new Label("Schlüsseltext");
panel3.add(schlüssel);
panel3.add(eingabeSchlüssel);
add(panel3);
constraints.gridx=1;
constraints.gridy=2;
constraints.gridwidth=4;
constraints.gridheight=1;
gridbag.setConstraints(panel3,constraints);
Panel panel4=new Panel();
Label code=new Label("Code");
panel4.add(code);
panel4.add(eingabeCode);
add(panel4);
constraints.gridx=1;
constraints.gridy=3;
constraints.gridwidth=4;
constraints.gridheight=1;
gridbag.setConstraints(panel4,constraints);
chiff.addActionListener(new chiffLauscher());
dechiff.addActionListener(new dechiffLauscher());
}
}
class chiffLauscher implements ActionListener{
public void actionPerformed(ActionEvent e){
Vigenere Vig=new Vigenere(eingabeSchlüssel.getText());
String klar2=eingabeKlar.getText();
String code2=Vigenere.chiff(klar2);
eingabeCode.setText(code2);
}
}
class dechiffLauscher implements ActionListener{
public void actionPerformed(ActionEvent e){
Vigenere Vig=new Vigenere(eingabeSchlüssel.getText());
String code2=eingabeCode.getText();
String klar2=Vigenere.dechiff(code2);
eingabeKlar.setText(klar2);
}
}
private Button chiff=new Button("Chiffrieren");
private Button dechiff=new Button("Dechiffrieren");
private TextField eingabeKlar=new TextField(30);
private TextField eingabeCode=new TextField(30);
private TextField eingabeSchlüssel=new TextField(30);
}