B
blubbrezn
Gast
Hallo Leute,
ich hab dieses Programm für meine Seminararbeit geschrieben (ist nocht nicht ganz fertig!)
aber ich kann Textdateien einlesen verschlüsseln, wieder entschlüsseln und auch speichern
wenn ich aber den eingelesenen Text verschlüssle und dann speichere und später wieder einlese kommt beim entschlüsseln nur wieder ein Zeichen wirrwar heraus.
könnte vllt jemand in einer freien Minute(vllt auch ein paar mehr) drüberschauen? wäre echt super nett
ich hab dieses Programm für meine Seminararbeit geschrieben (ist nocht nicht ganz fertig!)
aber ich kann Textdateien einlesen verschlüsseln, wieder entschlüsseln und auch speichern
wenn ich aber den eingelesenen Text verschlüssle und dann speichere und später wieder einlese kommt beim entschlüsseln nur wieder ein Zeichen wirrwar heraus.
könnte vllt jemand in einer freien Minute(vllt auch ein paar mehr) drüberschauen? wäre echt super nett
Java:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
public class ApplicationA extends JFrame {
/**
* @param args
*/
private JButton jb1 = new JButton("Öffnen");
private JButton jb2 = new JButton("Speichern");
private JButton jb3 = new JButton("Verschlüsseln");
private JButton jb4 = new JButton("Entschlüsseln");
private JTextArea jta1 = new JTextArea();
private JScrollPane jsp1 = new JScrollPane(jta1);
private JPasswordField jpf1 = new JPasswordField();
public ApplicationA ()throws Exception{
super("App");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setSize(600,430);
setLocation(0,0);
Container cp = getContentPane();
cp.setLayout(null);
setResizable(false);
setVisible(true);
jb1.setBounds(480, 350, 60, 20);
jb1.setMargin(new Insets(2,2,2,2));
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{jb1_Action(evt);}catch(Exception e){}
}});
cp.add(jb1);
jb2.setBounds(380, 350, 80, 20);
jb2.setMargin(new Insets(2,2,2,2));
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{jb2_Action(evt);}catch(Exception e){}
}});
cp.add(jb2);
jb3.setBounds(200, 330, 100, 20);
jb3.setMargin(new Insets(2,2,2,2));
jb3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{jb3_Action(evt);}catch(Exception e){}
}});
cp.add(jb3);
jb4.setBounds(200, 370, 100, 20);
jb4.setMargin(new Insets(2,2,2,2));
jb4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{jb4_Action(evt);}catch(Exception e){}
}});
cp.add(jb4);
jsp1.setBounds(10,10,500,300);
cp.add(jsp1);
jpf1.setBounds(20, 350, 150, 20);
jpf1.setEchoChar('*');
cp.add(jpf1);
}
private File chooseFile () {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = fc.showOpenDialog(jta1);
File f;
if(returnVal == JFileChooser.APPROVE_OPTION){
f = fc.getSelectedFile();
}else{f=null;}
return f;
}
private File chooseSaveFile(){
JFileChooser fc = new JFileChooser();
//fc.setApproveButtonText("Speichern");
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = fc.showSaveDialog(jta1);
File f;
if(returnVal == JFileChooser.APPROVE_OPTION){
f = fc.getSelectedFile();
}else{f=null;}
return f;
}
private String readTXT (File f) throws Exception {
String text="";
BufferedReader br= new BufferedReader(new FileReader(f));
String line=br.readLine();
while(line!=null){
text+=(line+"\r\n");
line=br.readLine();
}
return text;
}
private String readIMG (File f,String datType)throws IOException {
BufferedImage bI = ImageIO.read(f);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bI, datType, byteArrayOutputStream);
return new String(byteArrayOutputStream.toByteArray());
}
private void save(File f)throws Exception{
PrintWriter pw= new PrintWriter(f);
pw.write(jta1.getText());
pw.flush();
}
private String decrypt()throws Exception{
long key=0;
char[] buffer=jpf1.getPassword();
String s=jta1.getText();
for(int i=0;i<buffer.length;i++){
key+=(long)buffer[i];
}
buffer = new char[s.length()];
s.getChars(0,s.length(),buffer,0);
for(int i=0;i<s.length();i++){
buffer[i]+=key;
}
s = new String(buffer);
return s;
}
private String encrypt()throws Exception{
long key=0;
char[] buffer=jpf1.getPassword();
String s=jta1.getText();
for(int i=0;i<buffer.length;i++){
key+=(long)buffer[i];
}
buffer = new char[s.length()];
s.getChars(0,s.length(),buffer,0);
for(int i=0;i<s.length();i++){
buffer[i]-=key;
}
s = new String(buffer);
return s;
}
private void jb1_Action(ActionEvent evt)throws Exception{
File f = chooseFile();
String[] datType={"txt","jpg","jpeg","png","gif"};
int index=0;
for(;!f.getName().toLowerCase().endsWith(datType[index]);index++){}
switch(index){
case 0:
jta1.setText(readTXT(f));
break;
case 1: case 2: case 3: case 4:
jta1.setText(readIMG(f,datType[index]));
break;
default:
jta1.setText("Dieser Dateityp wird nicht unterstützt");
break;
}
}
private void jb2_Action(ActionEvent evt)throws Exception{
File f=chooseSaveFile();
f.createNewFile();
save(f);
}
private void jb3_Action(ActionEvent evt)throws Exception{
jta1.setText(decrypt());
}
private void jb4_Action(ActionEvent evt)throws Exception{
jta1.setText(encrypt());
}
public static void main(String[] args) throws Exception {
new ApplicationA();
}
}
Zuletzt bearbeitet von einem Moderator: