18

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));
    }
Omid Kamangar
  • 5,768
  • 9
  • 40
  • 69

2 Answers2

22

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.

qed
  • 22,298
  • 21
  • 125
  • 196
Victor Sorokin
  • 11,878
  • 2
  • 35
  • 51
  • And for reading back? Does it automatically know the length of the bytes to read? – Omid Kamangar Jan 17 '12 at 13:17
  • For reading back use FileInputStream and it's method `avialable()`. Read javadoc at: http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html – bezmax Jan 17 '12 at 13:23
  • I did this: I write the size of the byte array before the actual content of the array. It works like a charm. Thanks. – Omid Kamangar Jan 17 '12 at 16:47
0

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. Unless you're saving it from the web and its already coming through a socket as a 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

Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
Kevin Bigler
  • 256
  • 1
  • 2
  • 9
  • Not that I necessarily disagree, but this doesn't answer the question. This would be better as a comment on the original question than an answer. – David Nov 03 '12 at 14:19
  • Oh sorry I'm new to this site. At any rate, "I think I should use binary files for this (please correct me if I am wrong)." Is what I was attempting to respond to. Sorry about that. – Kevin Bigler Nov 03 '12 at 14:32