duddits |
|
 |
 |
Anmeldedatum: 03.01.2006 |
Beiträge: 569 |
Wohnort: /proc |
|
|
 |
 |
 |
|
Hi,
diese kleine Programm ist zwar recht einfach, aber ich bin gerade dabei diees auch für weiter Algorithmen umzusetzen, Auf Basis dieses Codes. Deshalb poste ich das ganze hier mal:
Code: | /**
*
* @author duddits
*/
import java.awt.*;
import java.awt.event.*;
public class Crypto extends Frame implements ActionListener{
Label en,de,k;
TextField ecrypt,decrypt,key;
static Crypto g = new Crypto();
Button action,clear;
/** Creates a new instance of Crypto*/
public Crypto() {
//main settings
super("Crypto");
setSize(500,350);
setLayout(null);
setBackground(Color.white);
// field to enter the text for the encryption
de = new Label("Enter the text you want to encrypt:");
de.setBounds(20,20,300,40);
add(de);
decrypt = new TextField();
decrypt.setBounds(20,70,300,40);
add(decrypt);
// button to start the encryptions
action = new Button("encrypt");
action.setBounds(350,70,100,40);
action.addActionListener(this);
add(action);
// a button to clear the fields
clear = new Button("clear");
clear.setBounds(350,140,100,40);
clear.addActionListener(this);
add(clear);
// button to start the decryptions
action = new Button("decrypt");
action.setBounds(350,210,100,40);
action.addActionListener(this);
add(action);
// field to enter the key for encrypting the text
k = new Label("Key for encrypting the text:");
k.setBounds(20,120,300,40);
add(k);
key = new TextField();
key.setBackground(Color.black);
key.setForeground(Color.white);
key.setEchoChar('*');
key.setBounds(20,160,200,40);
add(key);
// the result of the encryption
en = new Label("Encryptet text:");
en.setBounds(20,220,300,40);
add(en);
ecrypt = new TextField();
ecrypt.setBounds(20,260,300,40);
add(ecrypt);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public String encrypt (String sold) throws Exception
{
String s = sold.toLowerCase();
char character[] = {'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'};
int ik = key.getText().length();
char c;
int ci = 0;
StringBuffer encryptstr = new StringBuffer (s.length());
int i = 0;
int j = 0;
while(j < s.length()){
c = s.charAt(j);
i = 0;
if(c >= 'a' && c <= 'z'){
for(; i < character.length ; i++){
if(character[i] == c){
ci = i;
}
}
encryptstr.insert (j, character[(ci+ik)%26]);
}else{
encryptstr.insert (j, c);
}
j++;
}
return encryptstr.toString();
}
public String decrypt (String sold) throws Exception
{
String s = sold.toLowerCase();
char character[] = {'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'};
int ik = key.getText().length();
char c;
int ci = 0;
StringBuffer decryptstr = new StringBuffer (s.length());
int i = 0;
int j = 0;
while(j < s.length()){
c = s.charAt(j);
i = 0;
if(c >= 'a' && c <= 'z'){
for(; i < character.length ; i++){
if(character[i] == c){
ci = i;
}
}
decryptstr.insert (j, character[(ci-ik+26)%26]);
}else{
decryptstr.insert (j, c);
}
j++;}
return decryptstr.toString();
}
public void actionPerformed(ActionEvent ae){
String asave = ae.getActionCommand();
if(asave.equals("encrypt")){
try{
String sold = decrypt.getText();
String snew = g.encrypt(sold);
ecrypt.setText(snew);
}catch(Exception e){
System.err.println(e.getMessage());
}
}else if(asave.equals("clear")){
ecrypt.setText("");
decrypt.setText("");
key.setText("");
}else if(asave.equals("decrypt")){
try
{
String sold = ecrypt.getText().toLowerCase();
String snew = g.decrypt(sold);
decrypt.setText(snew);
}catch(Exception e){
System.err.println(e.getMessage());
}
}
}
public static void main(String args[]){
g.setVisible(true);
} |
Achso der Schlüssel hier ist natürlich beidiesen Algorithmus nur dafür da um die Position der Verschiebung zu definieren. Dabei wrid einfach die länge des Strings untersucht und zum verschieben genutzt.
mfg duddits |
|