71

What is the best way to save user settings in java desktop app securely? For example If I want to save an Ftp account settings what is the best way to do that?

Thanks

3 Answers 3

58

The Preferences API is a nice way to store user and system preferences. If you want to store passwords, you'll have to encrypt them. Here is a nice article that can get you started.

Encrypted Preferences in Java

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

5 Comments

Where shall we store the required key?
I want to note that on Windows you might have to change the registry if you are using Preferences. For a normal user of your application, this might be a blocker (no-go!).
@ElMac: I guess you refer to the infamous WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5 warning? Which has led users to believe that they need to use regedit to create a new Win Registry key (because then this warning goes away). This is actually a bug in Java itself. You are not meant to have to do this. It is a result of the bug.
IMO that Preferences API is an over-engineered mess, full of bugs which I'm not even sure are all resolved. The comments above are Exhibit A. Just use a regular friggin file. Better yet, use a PKI private key file and 2FA.
28

I usually store in user data directory, with sub directories of application name followed by application version.

public static String getUserDataDirectory() { return System.getProperty("user.home") + File.separator + ".jstock" + File.separator + getApplicationVersionString() + File.separator; } 

I had been using the following code for 3 years. This method works quite well either in Windows, Linux or Mac.

Please note that, in Windows, never store it in Program Files, as UAC in Windows Vista or newer may give you a lot of trouble.

Remember put a dot in-front of your application name, so that it will become a hidden folder in Linux.

Good thing by using this methology is that, you are not limited your self in storing primitive value only. Instead, you may save the entire object state to the disk by using xstream

For example :

public static boolean toXML(Object object, File file) { XStream xStream = new XStream(); OutputStream outputStream = null; Writer writer = null; try { outputStream = new FileOutputStream(file); writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8")); xStream.toXML(object, writer); } catch (Exception exp) { log.error(null, exp); return false; } finally { close(writer); close(outputStream); } return true; } 

7 Comments

But is this secure? Are you storing passwords in plain-text?
There's a case for storing passwords in plain text, as opposed to simply obfuscating them - obfuscation gives a false sense of safety. See for example developer.pidgin.im/wiki/PlainTextPasswords
Before I store a password, I will use jasypt.org to perform encryption. Currently, I hard code my encryption key in my application. I once thinking to let user specific the key. However, that may stop average user from using my app as it is kind to hard to explain what key is. Hence, I give up the idea. So far, I receive no complain on security issue by doing so.
So anyone with a copy of your program can use anybody's key file? Lack of compalints doesn't make it secure. A person whose password is compromised may not even be aware of the fact. Sure this works and with properly protected machines it's quite low risk, but in fact the encryption buys very little. In effect your applciation is the key, so that's what needs to be protected.
I think this is a security threat.As a cracker can get the hard coded password by decompiling your Java code.
|
13

Storing a single password securely is quite difficult. Suppose you encrypt the password using some secret key. Then when your applciation starts again it needs that secret key, where does it get that from?

If it asks the user then he might as well just enter the ftp password which you stored in the first place. If it reads the secret key from somewhere then you need to secure the secret key, and we're back where we started.

If you are keeping several passwords then asking the user for a single password to some "vault" may be much friendlier, but you then get into all the hassle of dealing with expired passwords.

There are products available to deal with this wort of stuff, if you have a serious need then you probably need to investigate them.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.