Java:如何编写二进制文件?

新手上路,请多包涵

我从事 Web 编程已经好几年了,从那以后我就没有为桌面应用程序做过任何编程,而且我忘记了很多东西。如果这太简单了,请耐心等待。

现在我有这种情况:

我正在尝试将一些哈希词存储在文件中。我想我应该为此使用二进制文件(如果我错了请纠正我)。但是我不知道应该如何将这些词写入文件。我尝试了很多方法,但是当我回读文件并尝试解密这些单词时,我得到了 BadPaddingException

有谁知道如何将单词写入文件?

PS:我使用这段代码来加密/解密单词(我从另一个 StackOverflow 线程获得它,并进行了一些修改):

 public static byte[] encrypt(String property) throws GeneralSecurityException, UnsupportedEncodingException {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(password));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, 20));
        return pbeCipher.doFinal(property.getBytes("UTF-8"));
    }

    public static String decrypt(byte[] property) throws GeneralSecurityException, IOException {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(password));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(salt, 20));
        return new String(pbeCipher.doFinal(property));
    }

原文由 Omid Kamangar 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 384
2 个回答

好吧,只需使用 FileInputStreamFileOutputStream =)

写作范例:

 // encrypted data in array
byte[] data = ...

FileOutputStream fos = ...
fos.write(data, 0, data.length);
fos.flush();
fos.close();

样本阅读:

 File inputFile = new File(filePath);
byte[] data = new byte[inputFile.length()];
FileInputStream fis = new FileInputStream(inputFile);
fis.read(data, 0, data.length);
fis.close();

上面的代码假定一个文件包含单个加密项目。如果您需要在单个文件中包含多个项目,则需要为此设计一些格式方案。例如,您可以在数据本身之前将加密数据中的字节数存储为 2 个字节。每个项目 2 个字节意味着加密项目不能超过 2^16 个字节。当然,你可以使用 4 个字节作为长度。

原文由 Victor Sorokin 发布,翻译遵循 CC BY-SA 3.0 许可协议

尝试了上面最高投票的解决方案,我发现了一些问题:

  1. 有一个错误说“不兼容的类型:从 long 到 int 可能有损失转换)”。不完全是警报的内容,因为我的提示是用另一种语言。在下面的代码中解决。

  2. 如果要输出读取的内容,请看下面的代码。

    public void readWriteBinaryFile(String fileName, String content) throws FileNotFoundException, IOException {
    // write
    byte[] dataToWrite = content.getBytes(StandardCharsets.UTF_8);
    File f = new File(fileName);
    FileOutputStream fos = new FileOutputStream(f);
    fos.write(dataToWrite, 0, dataToWrite.length);
    fos.flush();
    fos.close();

    // read
    byte[] dataToRead = new byte[(int)f.length()];   // for issue 1, avoid casting error
    FileInputStream fis = new FileInputStream(f);
    fis.read(dataToRead, 0, dataToRead.length);
    String str = new String(dataToRead, StandardCharsets.UTF_8); // for issue 2,  show content at output
    System.out.println(str);
    fis.close();

}

通过以下代码测试:

 String str = """
                 This is a test for read and write to binary file using Java.
                 This is line 2.
                 The end line.
                 """;
readWriteBinaryFile("./test/test.txt", str);

原文由 n a 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题