Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

append decrypted text to JTextArea  RSS feed

 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Im just trying to append text to the JTextArea in my app and just wondering where I'm going wrong with this? its just appending encrypted chars or nothing at all I know its something simple, Can someone please help and tell me why this is not working and why your code works, thank you

 
Dave Tolls
Ranch Hand
Posts: 2835
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure there's enough there to figure out what's going on.

What is userField? Is that the JTextArea?

What happens in the decrypt() method?

At the moment it looks like you are reading the original file into the JTextArea...unless the decrypt is decrypting into the original File I can't see how your decryption is supposed to work.
 
Stephan van Hulst
Saloon Keeper
Posts: 7719
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis, I fixed your indentation to make it more clear what's going on. Next time, please use the preview button before you post code to see how it appears on the forum. Please don't post the same topic twice.

You have a lot of noise in your code. Lines 9, 10, 11, and 14 don't do anything. You're creating a new file in line 18 which is useless if you're decrypting into the same file where the encrypted data comes from, but in this case it discards everything except for the name of the file.

Use Path instead of File. Use try-with-resources to read from streams. Provide decrypt() with an extra path where the decrypted bytes will be written, don't use the same path for both the source and the target.
 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave,


yes userField if my JTextArea. Here is more context about the Decrypt method, this is decrypting the text AES then

3 methods responsible for the decrypt method ' decrypt(inputDec, new File(inputDec.getName()),key);' below






And yes its just appending the encrypted characters to the userField(JTextArea)



Stephan,

Didn't mean to make it look messy , apologies.
 
Dave Tolls
Ranch Hand
Posts: 2835
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From our original post your decrypt passes in, as the outputFile, a File object that points to a filename at the root of your class path (or somewhere like that).

However, you then don't ever use that file.
You use the original file.
 
Stephan van Hulst
Saloon Keeper
Posts: 7719
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing, I think I may have mentioned this in another topic, but you have a couple of glaring security holes in your encrypt/decrypt steps:

  • You're not using a proper key derivation function.
  • You're not generating a random IV per encryption operation.

  • You should get rid of the getKeyBytes() method and use the provided key directly. To provide a key of the correct length, use an algorithm like PBKDF2 and pass it the appropriate parameters.

    The generated IV should be stored in the encrypted file, as well as a HMAC of the ciphertext and the IV.

    On decryption, you should first authenticate the stored ciphertext and the IV by verifying the HMAC. Then, you decrypt the ciphertext using the stored IV.
     
    travis Haycock
    Ranch Hand
    Posts: 67
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yeah thank you Stephan, Im just trying to get everything functioning at the moment then tweak I understand and appreciate you pointing that out, still have a lot of work to do on this app but its on my big board infront of me as reminders
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7719
    142
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Good luck!
     
    travis Haycock
    Ranch Hand
    Posts: 67
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Dave ,

    But if I'm using the JFileChooser ,selecting the file declaring this as "inputDec" passing it into the decrypt method(s) and its coming out as inputFile.getName

    The File 'fd' object should it not work because its placed in the params and passed through FileReader, BufferedReader etc. should the new decrypted text not appear on the jTextArea since my new object 'fd' is the decrypted File?


    I guess I'm just not sure whats going on in this situation? it looks from my point of view the JTextArea should have been written decrypted

     
    Paul Clapham
    Sheriff
    Posts: 22374
    42
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You said you wanted to append decrypted data to a JTextArea; that shouldn't be hard, except that you've severely handicapped yourself by making your decryption process work with File objects. So now you have to turn your code inside out and spend a lot of unnecessary time dealing with that design problem.

    Is it not possible to have the "decrypt" method work with an InputStream and an OutputStream, and let the calling code decide where the OutputStream points to?
     
    travis Haycock
    Ranch Hand
    Posts: 67
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    hmm but since its a File object itself could we not just decrypt the file then grab the data thats been decrypted and then append it to the JTextArea? it would be a lot of research to go back and learn how to design a new method to fit the proposition


    Like for an example; I have a standard 'save' & 'open' method included which works alright at the moment ( NO Decrypt Obviously) just an example of how I'm appending the normal file







    This goes through the file of course and appends it to the JTextArea, but what I don't understand because I'm working with files is why this can't go through per normal and then goes through the 'decrypt' method and because its set-up for files like you pointed out should it not give me the decrypted file back and I can go ahead and append this I just don't understand why id have to design the whole decrypt method again which took a bit of time when I'm working with a file as well? since the user is retrieving a file that they hose with JFileChooser
     
    Dave Tolls
    Ranch Hand
    Posts: 2835
    30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    travis Haycock wrote:@Dave ,

    But if I'm using the JFileChooser ,selecting the file declaring this as "inputDec" passing it into the decrypt method(s) and its coming out as inputFile.getName

    The File 'fd' object should it not work because its placed in the params and passed through FileReader, BufferedReader etc. should the new decrypted text not appear on the jTextArea since my new object 'fd' is the decrypted File?


    I guess I'm just not sure whats going on in this situation? it looks from my point of view the JTextArea should have been written decrypted



    The String that comes from getName is only the name of the file, and not its path.
    When that is passed into the File constructor it uses the current execution location as its path and simply appends the file name to it.

    Try this:


    The output from a run is:
    Name: numbers.xlsx
    Absolute: C:\Users\xxxxx\Documents\stuff\numbers.xlsx
    Input absolute: C:\Users\xxxxx\Documents\stuff\numbers.xlsx
    Output absolute: D:\projects\Scratch\numbers.xlsx
    File I am reading from: C:\Users\xxxxx\Documents\stuff\numbers.xlsx

    As you can see, the file being read from is the original file, not the one that (in your case) would have the decrypted data.

    But, in any case, as Paul suggests, why not decrypt to the text area? Or into a String?
     
    travis Haycock
    Ranch Hand
    Posts: 67
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alright thank you for both your inputs.. I guess ill go back and try to design the methods so it can accomplish the task effectively


    So essentially just re-design that decrypt and optimize it for strings so it can be appended to JTextArea
     
    travis Haycock
    Ranch Hand
    Posts: 67
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just dropping in to show what my solution was. I tried really hard not to gut my methods so I came up with something for now that offers an OK solution.

    the encrypt & decrypt area methods where the en/de cryption is going on is the exact same.


    For the encrypt method I am just creating the file and then immediately deleting the file which is unnoticeable (atleast to me) since it happens so quickly and then all that is left is the encrypted version in which the user when he/she wants to decrypt the text it will just append in the JTextArea(userField) I feel dull for even posting this topic as its not too hard to figure out (more sleep ) I thank you everyone for offering their input, appreciate the help and good environment here. Am now onto addressing security concerns like Stephan spoke of.







     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!