Java:如何编写二进制文件? [英] Java: How to write binary files?
问题描述
现在我有这种情况:
我试图在一个文件中存储一些散列的单词。我想我应该使用二进制文件(请纠正我,如果我错了)。但是我不知道该怎么写这个文件。我尝试了很多方法,但是当我读回文件并尝试解密这些单词时,我得到了
BadPaddingException
。 有没有人有任何想法如何将文字写入文件?
PS:我使用此代码来加密/解密单词(我从另一个StackOverflow线程,进行一些修改):
$ pre $ public $ static $ byte $ { b SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PBEWithMD5AndDES);
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(password));
密码pbeCipher = Cipher.getInstance(PBEWithMD5AndDES);
pbeCipher.init(Cipher.ENCRYPT_MODE,key,new PBEParameterSpec(salt,20));
return pbeCipher.doFinal(property.getBytes(UTF-8));
$ b $ public static String decrypt(byte [] property)throws GeneralSecurityException,IOException {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PBEWithMD5AndDES);
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(password));
密码pbeCipher = Cipher.getInstance(PBEWithMD5AndDES);
pbeCipher.init(Cipher.DECRYPT_MODE,key,new PBEParameterSpec(salt,20));
返回新的字符串(pbeCipher.doFinal(property));
c $ c> FileInputStream 和 FileOutputStream
=)
示例写作
//数组
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个字节的长度。
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
.
Does anyone have any idea how to write the words to a file?
P.S: I use this code for encrypting/decrypting the words (I got it from another StackOverflow thread, with a few modifications):
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
=)
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 bytes per item means encrypted item can not be longer than 2^16 bytes. Of course, you can use 4 bytes for length.
这篇关于Java:如何编写二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!