2014-11-01 140 views
0

我想密碼文件,並將密鑰保存在文件中,如加密的文本密鑰。密碼和解密文件

然後我想從文件中取出密鑰,並使用該密鑰解密文件。

我有下面的代碼,

public class JavaApplication8 { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, FileNotFoundException, ClassNotFoundException { 
    // TODO code application logic here 

    cifrarFicheiro(); 
    decifrarFicheiro(); 

} 
public static void cifrarFicheiro() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{ 
    File t1 = new File("t1.txt"); 
    FileInputStream Fin= new FileInputStream(t1); 
    byte[] texto= new byte[(int)t1.length()]; 
    Fin.read(texto); 
    Fin.close(); 

    SecretKey key = KeyGenerator.getInstance("DES").generateKey(); 
    Cipher cifra = Cipher.getInstance("DES/ECB/PKCS5Padding"); 
    cifra.init(Cipher.ENCRYPT_MODE, key); 
    byte[] chave = key.getEncoded(); 
    byte[] texto_cifrado = cifra.doFinal(texto); 

    FileOutputStream fout = new FileOutputStream("t1_t.txt"); 
    ObjectOutputStream obj = new ObjectOutputStream(fout); 
    fout.write(texto_cifrado); 
    obj.writeObject(chave); 
    obj.close(); 
    fout.close(); 

} 
public static void decifrarFicheiro() throws FileNotFoundException, IOException, ClassNotFoundException{ 
    FileInputStream fin = new FileInputStream("t1_t.txt"); 
    ObjectInputStream obj = new ObjectInputStream(fin); 

    SecretKey chave = (javax.crypto.SecretKey)obj.readObject(); 
    byte []keyCifrada = chave.getEncoded(); 
    obj.close(); 

    FileOutputStream fout = new FileOutputStream("t1_chave.txt"); 
    ObjectOutputStream obj1 = new ObjectOutputStream(fout); 
    obj1.writeObject(keyCifrada); 
    byte [] text = new byte[fin.available()]; 
    fin.read(text); 

} 

} 

但我得到下面的異常:

Exception in thread "main" java.io.StreamCorruptedException: invalid type code: F9 
     at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1377) 
     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370) 
     at javaapplication8.JavaApplication8.decifrarFicheiro(JavaApplication8.java:59) 
     at javaapplication8.JavaApplication8.main(JavaApplication8.java:31) 
    Java Result: 1 

誰能幫助我? :)

回答

0

首先,密碼和解密方法應該是對稱的。您正在調用兩次寫入操作,只能讀取一次。如果你有一個ObjectOutputStream包裝文件輸出流,那麼這兩個調用都應該在ObjectOutputStream上進行。 StreamCorruptedException在流中的元數據損壞時啓動。調用flush方法來防止(我認爲)。無論哪種方式,你應該:writeObject(texto_cifrado),writeObject(key),flush。然後read_Object(texto_cifrado),read_object(key)。 ACHO闕assim funciona笑

-1

公共類JavaApplication8 {

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, FileNotFoundException, ClassNotFoundException { 
    // TODO code application logic here 

    cifrarFicheiro(); 
    decifrarFicheiro(); 

} 
public static void cifrarFicheiro() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{ 
    File t1 = new File("t1.txt"); 
    FileInputStream Fin= new FileInputStream(t1); 
    byte[] texto= new byte[(int)t1.length()]; 
    Fin.read(texto); 
    Fin.close(); 

    SecretKey key = KeyGenerator.getInstance("DES").generateKey(); 
    Cipher cifra = Cipher.getInstance("DES/ECB/PKCS5Padding"); 
    cifra.init(Cipher.ENCRYPT_MODE, key); 
    byte[] chave = key.getEncoded(); 
    byte[] texto_cifrado = cifra.doFinal(texto); 

    FileOutputStream fout = new FileOutputStream("t1_t.txt"); 
    ObjectOutputStream obj = new ObjectOutputStream(fout); 
    obj.writeObject(texto_cifrado); 
    obj.writeObject(chave); 
    obj.close(); 
    fout.close(); 

} 
public static void decifrarFicheiro() throws FileNotFoundException, IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{ 
    FileInputStream fin = new FileInputStream("t1_t.txt"); 
    ObjectInputStream obj = new ObjectInputStream(fin); 

    byte[] texto = (byte[]) obj.readObject(); 
    byte[] chave = (byte[]) obj.readObject(); 
    obj.close(); 

    FileOutputStream fout = new FileOutputStream("t1_chave.txt"); 
    ObjectOutputStream obj1 = new ObjectOutputStream(fout); 
    obj1.writeObject(chave); 

    FileOutputStream fout1 = new FileOutputStream("t1_texto.txt"); 
    ObjectOutputStream obj2 = new ObjectOutputStream(fout1); 
    obj2.writeObject(texto); 

    SecretKey sks = new SecretKeySpec(chave, "DES"); 
    Cipher c= Cipher.getInstance("DES/ECB/PKCS5Padding"); 
    c.init(Cipher.DECRYPT_MODE,sks); 
    byte[] aux=c.doFinal(texto); 

    FileOutputStream fout2 = new FileOutputStream("t1_final.txt"); 
    fout2.write(aux); 
} 

}

我能解決這個問題。如果使用對象,則可以將它寫入文件中並按照相同的順序讀取它。在cifrarFicheiro()函數中,我使用ObjectInputStream並寫入文本,然後按鍵。然後在decifrarFicheiro()函數中使用了ObjectOutputStream,現在我知道第一個對象是文本,第二個對象是關鍵字。

+0

你能否在你的回答中解釋你是如何解決錯誤的。 – WannaBeCoder 2014-11-01 20:42:40