19

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));
    }
3
  • Have you tried doing System.out.println(decrypt(encrypt("Hello world"))); ? Commented Jan 17, 2012 at 13:10
  • 1
    Then please paste your I/O code too. Commented Jan 17, 2012 at 13:13
  • 3
    The error is not in the crypto code. It's in the IO code. Show us the IO code. Commented Jan 17, 2012 at 13:14

2 Answers 2

23

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.

Sign up to request clarification or add additional context in comments.

3 Comments

And for reading back? Does it automatically know the length of the bytes to read?
For reading back use FileInputStream and it's method avialable(). Read javadoc at: docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html
I did this: I write the size of the byte array before the actual content of the array. It works like a charm. Thanks.
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

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.