【问题标题】:How to display the decrypted file in Java without File Output?如何在没有文件输出的情况下在 Java 中显示解密的文件?
【发布时间】:2015-01-08 09:30:12
【问题描述】:

我正在尝试开发一个系统来解密文件,然后允许授权用户查看它而不保存解密的文件。这是为了确保其他用户无法打开解密后的文件。

以下代码产生文件输出。

    public NewJFrame() {try{
                String key = "squirrel123";
                FileInputStream fis2 = newFileInputStream("encrypted.mui");
                FileOutputStream fos2 = new FileOutputStream("decrypt.rar");

                decrypt(key, fis2, fos2);
                Desktop dk=Desktop.getDesktop();
                File f = new File("decrypt.rar");
                dk.open(f);
            }
                catch (Throwable e) {
         JOptionPane.showMessageDialog(null, e);
    }}  
    public static void decrypt(String key, InputStream is, OutputStream os) throws Throwable {
    encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
}

public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {

    DESKeySpec dks = new DESKeySpec(key.getBytes());
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey desKey = skf.generateSecret(dks);
    Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE

    if (mode == Cipher.ENCRYPT_MODE) {
        cipher.init(Cipher.ENCRYPT_MODE, desKey);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        doCopy(cis, os);
    } else if (mode == Cipher.DECRYPT_MODE) {
        cipher.init(Cipher.DECRYPT_MODE, desKey);
        CipherOutputStream cos = new CipherOutputStream(os, cipher);
        doCopy(is, cos);
    }
}

public static void doCopy(InputStream is, OutputStream os) throws IOException {
    byte[] bytes = new byte[64];
    int numBytes;
    while ((numBytes = is.read(bytes)) != -1) {
        os.write(bytes, 0, numBytes);
    }
    os.flush();
    os.close();
    is.close();
}

如何在不使用 FileOutputStream 的情况下解密文件,然后允许授权用户在解密后查看它?

【问题讨论】:

  • 您可以使用内存中的输出流之一,例如 ByteArrayOutputStream 作为输出流吗?抱歉,我无法记住 Java 从那里到字符串的奇怪流路径。这将允许您解密和显示而不必触摸磁盘。

标签: java encryption netbeans


【解决方案1】:

如果您不想将其写入物理文件,而只是显示数据,则可以使用与 FileOutputStream 不同的写入器。

比如可以创建一对PipedStream,解密后再读取结果。

        String key = "squirrel123";
        FileInputStream fis2 = newFileInputStream("encrypted.mui");

        PipedInputStream pis = new PipedInputStream();
        PipedOutputStream pos = new PipedOutputStream(pis);

        decrypt(key, fis2, pis);

        BufferedReader reader = new BufferedReader(new InputStreamReader(pis));
        String line;
        while( (line=reader.readLine()) != null ){
            System.out.println(line);
        }

【讨论】:

  • 请注意,您需要将此课程设为最终课程。那么就不可能通过继承、覆盖和类折叠(Groovy)“窃取”您的秘密或“视图”等方法。
  • 你也可以使用ByteArrayOutputStream。
  • @Netto 我也遇到了同样的问题,但我不知道我可以把 BufferedReader 代码放在哪里。你能帮我吗?
  • @felipecrp 我也有同样的问题,你能帮帮我吗
  • @KVK “在哪里放置 BufferedReader”是什么意思?像管道一样的流,您可以在其中放置或读取信息。 Reader 是处理流的低级功能的实体,因此您无需担心读取字节和长度数组。本主题的重点是使用不同的输出流然后是“FileOutputStream”,因此您无需编写另一个文件,而是直接解析数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多