简体   繁体   English

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

[英]Java: How to write binary files?

I have been doing web programming for several years now and since then I have not done any programming for desktop applications, and I have forgotten so many things. 我已经做了几年的网络编程,从那时起我就没有为桌面应用程序做过任何编程,而且我已经忘记了很多东西。 Please be patient if this is too simple. 如果这太简单了,请耐心等待。

Now I have this situation: 现在我有这种情况:
I am trying to store some hashed words in a file. 我试图在文件中存储一些哈希的单词。 I think I should use binary files for this (please correct me if I am wrong). 我想我应该使用二进制文件(如果我错了请纠正我)。 But I have no idea how should I write the words to the file. 但我不知道如何将文字写入文件。 I tried many ways, but when I read back the file, and try to decrypt the words, I get BadPaddingException . 我尝试了很多方法,但是当我读回文件并尝试解密这些文字时,我得到BadPaddingException

Does anyone have any idea how to write the words to a file? 有没有人知道如何将文字写入文件?

PS: I use this code for encrypting/decrypting the words (I got it from another StackOverflow thread, with a few modifications): 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));
    }

Well, just use FileInputStream and FileOutputStream =) 好吧,只需使用FileInputStreamFileOutputStream =)

Sample writing: 样本写作:

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

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

Sample reading: 样本阅读:

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

Above code assumes that one file holds single encrypted item. 上面的代码假设一个文件包含单个加密项。 If you need to hold more than one item in the single file, you'll need to devise some format scheme for that. 如果您需要在单个文件中保存多个项目,则需要为此设计一些格式方案。 For example, you can store number of bytes in encrypted data as 2 bytes, before data itself. 例如,您可以在数据本身之前将加密数据中的字节数存储为2个字节。 2 bytes per item means encrypted item can not be longer than 2^16 bytes. 每个项目2个字节表示加密项目不能超过2 ^ 16个字节。 Of course, you can use 4 bytes for length. 当然,您可以使用4个字节的长度。

Saving as a text document would seem to make more sense to me, the data is already a so there's no need to convert it to a byte[] and if you need to read from the file would be pretty convenient. 保存为文本文档对我来说似乎更有意义,数据已经是一个所以没有必要将它转换为byte[] ,如果你需要从文件中读取将是非常方便的。 Unless you're saving it from the web and its already coming through a socket as a byte[] . 除非你从网上保存它并且它已经通过套接字作为byte[] I know it says don't provide your opinion but its strictly a matter of opinion, that was the only part of your question left unanswered by the previous two answered 我知道它说不提供你的意见,但严格意义上的问题,这是你问题的唯一部分,前两个答案没有得到回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM