5

I have a project where we write a small amount of data to a file every 5 minutes. The idea is to look at how this data changes over a period of hours, days, and weeks.

One of the requirements is to store this data in a secure format. We already have an encryption scheme for sending this data across a network as a byte[] array via DataI/O streams.

The question I have is this, is there a way to write encrypted byte[] arrays to a text file in such a way that I can read them back out? My biggest problem at the moment is that I'm reading Strings from the files, which messes up the byte[] arrays.

Any thoughts or pointers on where to go?

Kyte
  • 834
  • 2
  • 12
  • 27
  • yes. the issue you'll be facing is that you'll need to have the decryption key in your app, therefore possibly obtainable by someone – njzk2 Apr 17 '14 at 16:41
  • possible duplicate of [How can I write a byte array to a file in Java?](http://stackoverflow.com/questions/1769776/how-can-i-write-a-byte-array-to-a-file-in-java) – 2rs2ts Apr 17 '14 at 16:41
  • See [this answer](http://stackoverflow.com/a/17684370/691859) in particular. – 2rs2ts Apr 17 '14 at 16:42
  • @2rs2ts, not a duplicate because we simply want to append an encrypted byte[] array to the file. We're not writing bulk data to a file. We're appending incrementally new encrypted byte[] arrays. Let me know if I need more info in the question! – Kyte Apr 17 '14 at 16:44
  • Use [`Files.write`](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#write%28java.nio.file.Path,%20byte[],%20java.nio.file.OpenOption...%29) with [`StandardOpenOption.APPEND`](http://docs.oracle.com/javase/7/docs/api/java/nio/file/StandardOpenOption.html#APPEND) so you will append to the file. To read, you can use [`Files.readAllBytes`](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllBytes%28java.nio.file.Path%29) and then parse, or you can of course use a [`FileInputStream`](http://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html). – 2rs2ts Apr 17 '14 at 16:54
  • It seems to me that you are asking how to write and read binary files. "is there a way to write encrypted byte[] arrays to a text file in such a way that I can read them back out?" Stop reading your files as if they are text files and you will succeed. – 2rs2ts Apr 17 '14 at 16:59

2 Answers2

5

What you need to do is take your data and put it into a byte array. Then once it is in a byte array, you can encrypt it using an encryption algorithm. Then you write it to the file.

When you want to get the original data back, you have to read the byte array from the file, then decrypt the byte array and then you will have your original data. You cannot just read this data as a string because your encryption algorithm will create bytes that cannot be represented as regular chars so your data will get messed up.

Just make sure you read the encrypted data as a byte array and not a string, that is where you are having a problem.


If you want to write multiple byte arrays to a single file, then you should probably do something like this since you are using Java:

writer.print(arr.length);
writer.print(arr);
writer.flush();

Do this for each byte array. Then when you read the byte arrays back:

int length = reader.readInt();
byte[] bytes = new byte[length];
// fill array

This way the file can be structured like this:

[length of following array][array][length of second array][second array]

You will be able to put all of the byte arrays back to back, and since each array starts with the length of the array, you will know how much data needs to be put into each array.

John
  • 3,769
  • 6
  • 30
  • 49
  • When the data is encrypted a byte[] array returned. So we already have that. The question is how to write multiple byte[] arrays to a file and how to read them back out. Reading individual byte arrays from the file is part of the question – Kyte Apr 17 '14 at 16:47
0

See How to append to AES encrypted file for an example of an AES+CBC Java example which allows opening an already encrypted file and appending more encrypted data to in, while not requiring any special handling when decrypting it since it looks just like it would if the entire file had been encrypted just once.

simpleuser
  • 1,617
  • 25
  • 36