Java解密文件毁了

时间:2012-10-18 12:44:59

标签: java cryptography jce

我有一个程序必须加密音频文件,然后在需要时解密。我在一些其他类型的文件上测试了我的程序,比如.bin或.txt。我得到的问题是解密文件在实际内容之前有一些奇怪的字符,如源文件包含“010101”,加密解密后它有“¬íw0w010101”。

我的加密方法代码在这里:

public void cipherTheAudioFile(String fileDir, String fileToCipher) throws            FileNotFoundException, IOException, NoSuchAlgorithmException, InvalidKeySpecException,  InvalidKeyException, NoSuchPaddingException {
    File audioSourceFile = new File(fileDir + "\\" + fileToCipher);
    ObjectOutputStream oos = new ObjectOutputStream(
        new CipherOutputStream(new FileOutputStream(
            new java.io.File("").getAbsolutePath().toString() + "/encrypted/" + fileToCipher + ".sky"), cipher));

    byte[] audioFileInBytes = FileUtils.readFileToByteArray(audioSourceFile);
    oos.write(audioFileInBytes);

    fos = new FileOutputStream(KEY_FILE);
    SecretKeyFactory skf = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM);
    DESKeySpec keyspec = (DESKeySpec) skf.getKeySpec(key, DESKeySpec.class);
    fos.write(keyspec.getKey());

    fos.close();
    oos.close();
}

我的解密方法代码在这里:

public void decryptTheAudioFile(String fileDir, String fileToDecipher) throws NoSuchAlgorithmException, NoSuchPaddingException, FileNotFoundException, IOException, ClassNotFoundException, InvalidKeySpecException, InvalidKeyException {
    fis = new FileInputStream(keyFile);
    byte[] keyspecbytes = new byte[fis.available()];

    File fileToWriteIn = createFileToWriteIn(fileDir, fileToDecipher);

    fis.read(keyspecbytes);
    SecretKeyFactory skf = SecretKeyFactory.getInstance(encryptionAlgorithm);
    DESKeySpec keyspec = new DESKeySpec(keyspecbytes);
    SecretKey key = skf.generateSecret(keyspec);
    Cipher cipher = Cipher.getInstance(encryptionAlgorithm);
    cipher.init(Cipher.DECRYPT_MODE, key);

    ObjectInputStream ois = new ObjectInputStream(
        new CipherInputStream(
            new FileInputStream(new java.io.File("").getAbsolutePath().toString() + "/encrypted/" + fileToDecipher + ".sky"), cipher));
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileToWriteIn));

    byte[] audioFileInBytes = new byte[1024];

    int numRead = 0;
    while ((numRead = ois.read(audioFileInBytes)) >= 0) {
        oos.write(audioFileInBytes, 0, numRead);
    }

    oos.close();
    fis.close();
    ois.close();
}

P.S。它可能是编码的东西,但我不太确定。

EDITED

好的,我已经改为FileWriters,但仍然没有变化。这是代码:

 OutputStream os = new FileOutputStream(new java.io.File("").getAbsolutePath().toString() + "/encrypted/" + fileToCipher + ".sky");
    CipherInputStream cis = new CipherInputStream(new FileInputStream(audioSourceFile), cipher);
    byte[] audioFileInBytes = new byte[1024];
    int numRead = 0;
    while ((numRead = cis.read(audioFileInBytes)) >= 0) {
            os.write(audioFileInBytes, 0, numRead);
    }

同样是解密者。

2 个答案:

答案 0 :(得分:2)

问题在于decryptTheAudioFile方法写入文件的方式。具体来说,问题是它使用的是ObjectOutputStream。那就是添加一个对象序列化头。但它根本不属于那里。

解决方案是从decryptTheAudioFile

中删除它
ObjectOutputStream oos = new ObjectOutputStream(
         new FileOutputStream(fileToWriteIn));

并将其替换为:

OutputStream os = new FileOutputStream(fileToWriteIn);

并更改其余代码以写入os。您的代码需要反映您在cipherTheAudioFile中阅读文件的方式。


最好摆脱其他ObjectStream个实例,只需读取和写入普通Streams。其他ObjectStream是无害的(大部分),但它们实际上并没有实现任何目标。

答案 1 :(得分:0)

删除所有ObjectOutputStreams和ObjectInputStreams。您正在编写一个字节数组,因此它们是不必要的。你看到的额外字节可能告诉你byte []的类型和大小。

相关问题