cipher funtkioniert nicht so wie ich das will

jer1cho

Mitglied
Code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;
import java.util.Properties;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;


public class ciphition {
    
    /** Creates a new instance of ciphition */
    public ciphition() {
    }
    public static void main(String[] args){
        CipherOutputStream cos;
        FileOutputStream fos;
        InputStream is;
     Properties p=new Properties();
        try {
            is=saves.class.getResourceAsStream("properties");
            p.load(is);
                Enumeration<Object>     e=p.keys();
        while(e.hasMoreElements()){
            String e1=(String) e.nextElement();
            System.out.println(""+e1+"-"+p.getProperty(e1));
        }
            is.close();
            Cipher c = null;
            try {
                c = Cipher.getInstance("DES");
            } catch (NoSuchPaddingException ex) {
                ex.printStackTrace();
            } catch (NoSuchAlgorithmException ex) {
                ex.printStackTrace();
            } 
                String key="12345678";
    Key k = new SecretKeySpec( key.getBytes(), "DES" );
            try {
                c.init( Cipher.ENCRYPT_MODE, k ); 
            } catch (InvalidKeyException ex) {
                ex.printStackTrace();
            } 
    fos=new FileOutputStream("properties.txt");
    cos=new CipherOutputStream(fos,c);
            p.store(cos,null);
            fos.close();
            cos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    readin();
    }
    public static void readin(){
        CipherInputStream cos;
        FileInputStream fos = null;
         Cipher c = null;
            try {
                c = Cipher.getInstance("DES");
            } catch (NoSuchPaddingException ex) {
                ex.printStackTrace();
            } catch (NoSuchAlgorithmException ex) {
                ex.printStackTrace();
            } 
                String key="12345678";
    Key k = new SecretKeySpec( key.getBytes(), "DES" );
            try {
                c.init( Cipher.DECRYPT_MODE, k ); 
            } catch (InvalidKeyException ex) {
                ex.printStackTrace();
            }
        try {
            fos=new FileInputStream("properties.txt");
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        try {
            cos=new CipherInputStream(fos,c);
        } finally {
        }
            Properties p=new Properties();
        try {
            p.load(cos);
            
            fos.close();
            cos.close();
                      Enumeration<Object>     e=p.keys();
        while(e.hasMoreElements()){
            String e1=(String) e.nextElement();
            System.out.println(""+e1+"-"+p.getProperty(e1));
        }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
       
    }
}

Also als ausgabe bekomme ich:
Code:
maxchar-300
testchar-234adsf
test-400
maxchar-300
t-
testchar-234adsf


irgendwie wurde das "test" verstümmelt y?
 
ahso ich muss wohl das cos.close();
vor dem fos.close();

schließen warum?

Java ist doch sonst auch nicht so pingelig
 
Zuletzt bearbeitet:
Weil das close durch ein flush dafür sorgt dass die Daten die noch im Stream sind wirklich geschrieben werden. Das cos.close sorgt also dafür dass die Daten auf den fos übertragen werden. Wenn du den zuerst schliesst hat er keine Daten um sie in eine Datei zu schreiben.

Du kannst auch cos.flush, fos.close, cos.close machen.
 

Neue Beiträge

Zurück