密码和解密文件

时间:2014-11-01 19:17:32

标签: java

我想要加密文件,并将密钥保存在文件中,就像加密的文本密钥一样。

然后我想从文件中取出密钥,并使用该密钥解密文件。

我有以下代码,

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

任何人都可以帮助我吗? :)

2 个答案:

答案 0 :(得分:0)

首先,密码和解密方法应该是对称的。你正在调用两个写操作,只读一次。如果你有一个ObjectOutputStream包装fileoutputstream,那么调用都应该在ObjectOutputStream上进行。当流上的元数据损坏时,将启动StreamCorruptedException。调用flush方法来防止这种情况(我认为)。无论哪种方式你应该:writeObject(texto_cifrado),writeObject(key),flush。然后是read_Object(texto_cifrado),read_object(key)。 Acho que assim funciona lol

答案 1 :(得分:-1)

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);
    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()函数中我使用de ObjectOutputStream,我现在第一个对象是文本,第二个是键。