1

I have made reference from this question and achieved success in setting up encryption. I am trying to however utilize this encryption on a string of array to write into a file. This is how I am setting my method up but I end up writing only one of the string array into the file.

String[] str = new String ["X: Adam", "Y: Barry", "z: Oliver"];  

File file = new File(Path + "/EncryptedFile.txt);

Calling method to write the string array into the file: Crypto.WriteEncrypteFile(str, file);

The method

Public void WriteEncrypteFile(String[] str, File file) {
  try { 
    BufferedWriter w = new BufferedWriter(new FileWriter(file)); 
    byte[] tmptxt = Array.toString(str).getbytes(Charset.forName(" UTF-8 "));  
    byte[] encTxt = cipher.doFinal(tmptxt);
    w.write(string.valueOf(encTxt)); 
    w.flush();
    w.close(); 
  } catch (Exception e){
    e.printStackTrace();
  }    

My questions is how can I write an encrypted string from my array into the file. Any pointers?

Community
  • 1
  • 1
codeFreak
  • 353
  • 1
  • 3
  • 15
  • you are currently only writing an integer to the file.(the valueof the array). You might want to remove the string.valueOf() and just supply the encTxt to the writer – Tschallacka May 20 '15 at 14:12
  • @MichaelDibbets How is he writing an integer? `w.write(string.valueOf(encTxt));` – m0skit0 May 20 '15 at 14:14

2 Answers2

2

You are just writing the String value of the array to the file (since you use Array.toString(str)). This will usually just be some representation of the reference. You have to either concatenate the values of the array or loop through it and encrypt/write every value individually.

Additionally, you shouldn't use a Writer to write content that does not consist of characters. Writers always try to encode the output which could potentially ruin your carefully set up bytes.

Just use a FileOutputStream and write the bytes with that:

try( FileOutputStream fos = new FileOutputStream(file) ) {
    for(String s : str) {
        byte[] tmptxt = s.getbytes(Charset.forName("UTF-8"));  
        byte[] encTxt = cipher.doFinal(tmptxt);
        w.write(encTxt);
    }
} catch(IOException e) {
    // print error or whatever
}

For reading you do the same thing but with a FileInputStream instead.

Robert
  • 39,162
  • 17
  • 99
  • 152
mhlz
  • 3,497
  • 2
  • 23
  • 35
  • 1
    The approach is correct, however encrypting each string individually may lead to security problems. – Robert May 20 '15 at 15:01
  • Is there a better way? Because I can't think of a better way to encrypt a string array. And what security flaw are you referring to?? – codeFreak May 20 '15 at 16:20
  • 1
    The issue is best described in this encrypted picture of tux: http://commons.wikimedia.org/wiki/File:Tux_ecb.jpg. Concatenating the strings and splitting them up again is most likely the best solution. – mhlz May 20 '15 at 19:39
  • Tnx. Have concatenated the values in the string array now into a declared string variable. That way, encryption is done once not individually . +1 – codeFreak May 24 '15 at 08:56
0

You can use Arrays.toString(), but this way you will need to parse it to read it. Alternatively you can also write the byte[] directly in the file using an OutputStream. There's no need to convert to a string unless you want a human (e.g. yourself) to read it.

m0skit0
  • 25,268
  • 11
  • 79
  • 127