Hallo erstmal,
ich habe angefangen einen simplen text encrypter zu schrieben. Ich habe zurzeit genau 2 probleme:
(1): Die file
wird immer unter "tmp"+random zeichen gespeichert und nicht unter "tmp".
(2):
Die Zeile so funktioniert perfekt und schreibt alles von rechts nach links. Wenn ich aber
benutze wird nichts zur File geschrieben. Habt ihr eine ahnung wie man das beheben kann?
Es müsste keinen unterschied machen, aber ich arbeite mit Eclipse auf ubuntu.
Ich hoffe ihr könnt mir weiterhelfen.
KaLi
ich habe angefangen einen simplen text encrypter zu schrieben. Ich habe zurzeit genau 2 probleme:
(1): Die file
Code:
File tmp = File.createTempFile("tmp",null,file.getParentFile());
(2):
Code:
newWrite = newWrite + newLine.charAt(newLine.length()-i-1);
Code:
char x = changeChar(newLine.charAt(newLine.length()-i-1));
newWrite = newWrite + x;
Code:
package docEncrypt;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
public class Encryption extends Thread{
static char[] original = {'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','z',' '};
static char[] scramble = new char[original.length];
static int[] used = new int[original.length];
String[] text = new String[1];
String newline = "\n";
static boolean found;
public Encryption(){
}
public static void randomize(){
//Erstellt eine ungeordnete kopie vom original array
int[] used = new int[original.length];
int x;
int counter = 0;
for(int i = 0; i < original.length; i++){
if(counter > 25) break;
found = false;
x = (int)(Math.random()*27);
for(int y = 0; y < original.length; y++){
if(used[y] == x ){
found = true;
break;
}
}
if(!found){
scramble[i] = original[x];
used[i] = x;
counter++;
//System.out.print(scramble[i]);
}
else i--;
}
}
public static char changeChar(char x){
//tauscht einen char von original mit einem von scramble an gleicher position
//wenn es den char nicht gibt, wird der eingabeChar zurückgegeben
for(int i = 0; i < original.length; i++){
if(original[i] == x) return scramble[i];
}
return x;
}
public void run() {
randomize();
try{
int returnVal = docEncrypt.Interface.fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = docEncrypt.Interface.fc.getSelectedFile();
File tmp = File.createTempFile("tmp",null,file.getParentFile());
docEncrypt.Interface.txtArea.append("Opening: " + file.getAbsolutePath() + "." + newline);
BufferedReader in = new BufferedReader(new FileReader(file));
FileWriter fstream = new FileWriter(tmp,false);
BufferedWriter out = new BufferedWriter(fstream);
String newLine = in.readLine();
String newWrite = "";
docEncrypt.Interface.txtArea.append("Encryption starts its execution."+ newline);
docEncrypt.Interface.txtArea.append("Working..." + newline);
while(newLine != null){
//probleme hier!!
for(int i = 0; i < newLine.length(); i++){
char x = changeChar(newLine.charAt(newLine.length()-i-1));
newWrite = newWrite + x;
Thread.sleep(1);
}
out.write(newWrite + "\n");
newWrite ="";
newLine = in.readLine();
Thread.sleep(1);
}
in.close();
out.close();
fstream.close();
docEncrypt.Interface.txtArea.append("Finished. File saved under"+tmp.getAbsolutePath() + newline);
}
else {
docEncrypt.Interface.txtArea.append("Open command cancelled by user." + newline);
}
docEncrypt.Interface.txtArea.setCaretPosition(docEncrypt.Interface.txtArea.getDocument().getLength());
} catch (FileNotFoundException d) {
d.printStackTrace();
}catch(IOException x){
x.printStackTrace();
}catch (InterruptedException ie) {
}
}
public static void main(String [] args){
}
}
Code:
package docEncrypt;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.lang.Thread;
import java.awt.event.KeyEvent.*;
import java.util.regex.*;
import java.lang.StringBuffer;
public class Interface{
JFrame jframe = new JFrame("KaEncrypt - Documentation Encryption");
static private final String newline = "\n";
private static JPanel main = new JPanel(new GridBagLayout());
private static GridBagConstraints gridcon;
public static JFileChooser fc = new JFileChooser();
private static JButton encrypt = new JButton("encrypt");
private static JButton decrypt = new JButton("decrypt");
private static JLabel kali = new JLabel("© Johannes Kahlenborn");
public static JTextArea txtArea = new JTextArea(6,25);
public static JScrollPane scroll = new JScrollPane(txtArea);
public Interface(){
jframe.setSize(new Dimension( 300, 200));
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setContentPane(kEncrypt());
jframe.setVisible(true);
}
public JPanel kEncrypt(){
txtArea.setEditable(false);
encrypt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Encryption m1 = new Encryption();
m1.start();
}
});
JPanel flow = new JPanel(new FlowLayout());
flow.add(encrypt);
flow.add(decrypt);
addItem(main,flow,0,0,1,1,GridBagConstraints.CENTER);
addItem(main,kali,0,1,1,1,GridBagConstraints.BOTH);
addItem(main,scroll,0,2,1,1,GridBagConstraints.BOTH);
return main;
}
public void addItem (JPanel p, JComponent comp, int x, int y, int width, int height, int align){
gridcon = new GridBagConstraints();
gridcon.gridx = x;
gridcon.gridy = y;
gridcon.gridwidth = width;
gridcon.gridheight= height;
gridcon.weightx= 100.0;
gridcon.weighty= 100.0;
gridcon.insets= new Insets(5,5,5,5);
gridcon.anchor= align;
gridcon.fill= GridBagConstraints.NONE;
p.add(comp, gridcon);
}
public static void main(String[] args){
new Interface();
}
}
Ich hoffe ihr könnt mir weiterhelfen.
KaLi