尝试解密文件时,CipherInputStream为空

时间:2014-03-28 06:42:59

标签: java inputstream bufferedreader

我正在尝试编写一些辅助方法来处理读取和写入加密文件。我有两个成功实现此功能的方法,并返回一个可用于读取或写入文件的InputStream或OutputStream(实际上是Cipher版本)。我已经确认,当使用Object流包装并用于读取和写入加密的Object文件时,这些方法很有效。

但是,当我尝试从加密的文本文件中读取时会出现问题。我可以验证我提供的字符串是否正在加密并写入正确的文件,但是当我尝试从该文件读回时,BufferedReader报告一个EOF(null)。 InputStream.available()方法返回0.我可以确保文件在那里,正在找到,并且InputStream本身不为null。任何人都可以告诉我可能导致这种情况的原因吗?

读取/写入加密对象可以很好地工作(CorruptedStreamException在这里很好):

        private static void testWriteObject() {
    String path = "derp.derp";
    Derp start = new Derp("Asymmetril: " + message, 12543, 21.4, false);
    FilesEnDe.writeEncryptedObject(key, "derp.derp", start);
    echo("original");
    echo(">"+start);

    Object o;
    try {
        ObjectInputStream ois = new ObjectInputStream(ResourceManager.getResourceStatic(path));
        o = ois.readObject();
        echo("encrypted");
        echo(">"+o);
        ois.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    o = FilesEnDe.readEncryptedObject(key, path);
    echo("decrypted");
    echo(">"+o);
}

输出:

original
>Asymmetril: WE CAME, WE SAW, WE CONQUERED.; 12543; 21.4; false
[RM] > Trying to load resource: derp.derp
java.io.StreamCorruptedException
[RM] > Trying to load resource: derp.derp
decrypted
>Asymmetril: WE CAME, WE SAW, WE CONQUERED.; 12543; 21.4; false

尝试解密文本文件不会(请注意加密文本是可读的):

private static void testWriteFile() {
    String path = "EncryptedOut.txt";
    BufferedReader bis1, bis2;

    try {
        BufferedOutputStream os = new BufferedOutputStream(FilesEnDe.getEncryptedOutputStream(key, path));
        os.write(message.getBytes());
        os.flush();
                    os.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    echo("original");
    echo(">"+message);

    try {
        bis1 = new BufferedReader (new InputStreamReader(ResourceManager.getResourceStatic(path)));
        echo("encrypted");
        echo(">" + bis1.readLine());
        bis1.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        InputStream is = FilesEnDe.getEncryptedInputStream(key, path);
        InputStreamReader isr = new InputStreamReader(is);
        bis2 = new BufferedReader (isr);
        echo("bits in stream? " + is.available());

        echo("decrypted");
        echo(">"+bis2.readLine());
        bis2.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }

输出:

original
>WE CAME, WE SAW, WE CONQUERED.
encrypted
>¤ƒ]£¬Vß4E?´?ùûe
[RM] > Trying to load resource: EncryptedOut.txt
bytes in stream? 0
decrypted
>null

用于创建CipherInputStream的代码:

    public static InputStream getEncryptedInputStream(String key, String path) {
    try {
        InputStream is = ResourceManager.getResourceStatic(path);
        SecretKeySpec keyspec = new SecretKeySpec(getHash(key),"AES");
        Cipher c = Cipher.getInstance("AES");
        c.init(Cipher.DECRYPT_MODE, keyspec);
        return new CipherInputStream(is,c);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
    }

当我尝试使用CIPHERINPUTSTRAM来解密文件并检索原始字符串时,会出现问题。

2 个答案:

答案 0 :(得分:1)

  

但是,当我尝试从加密的文本文件中读取时会出现问题。

没有“加密文本文件”这样的东西。加密的结果是二进制,而不是文本。

  

我可以验证我提供的字符串是否正在加密并写入正确的文件,但是当我尝试从该文件读回时,BufferedReader会报告EOF(null)。

您不应该使用BufferedReader.它不是文本,而是二进制文件。使用BufferedInputStream.

答案 1 :(得分:-1)

无论我是通过PrintWriter还是BufferedOutputStream写,也不是我是否使用Reader阅读。事实证明,我忘了关闭创建该文件的OutputStream。一旦我添加了一条小线,一切都开始工作了。感谢Antoniossss建议我重做我方法的破碎部分。我想知道为什么Eclipse没有提到它的资源泄漏......

相关问题