I wrote a Java program which needs to read a local source data file (the data is to large to be hard-coded into the program). Is it possible that I create a JAR package for the program without containing the local source data file (in order to keep the file private)? Or I have to encrypt the local file?
There is no real way to keep the file or data private. Not even encryption. If your program can read the data, then any one who has the same permissions as your program on that machine can read that data. At best you can make it a little inconvenient for someone to read the data - but you cannot prevent it.
If you encrypt the file - where will you keep the encryption key. If you program can read the encryption key, then a person also can. If you hardcode the encryption key in the program, then again the key can be got from the program binary.
Related
I have a .txt file in which I make the configuration for a certain system I'm building.
Basically, after reading the file, I keep the variable values and set those values into what I need.
It would be nice if I could encrypt that information so that it wouldn't be readable if someone got access to that said file. The problem is that I really would not know how to decode it in my program.
What is the most simple way to encode a text file? How do I decode in my program?
A very important detail is that im building this app in JavaME which is limiting my set of tools to make this.
To summarize, I need to encrypt a file outsite the program and read it inside the program?
What is the easiest way to do so?
Thanks
Why do you need to "encrypt a file outside the program"?
Read your plaintext file into your program, use an encryption algorithm (use a library, custom, etc.) to encrypt your text and them write that into a new file.
To decrypt, read encrypted file and run an inverse of your encryption function and do whatever you want with it in your program.
I'm developing a desktop JavaFX application and plan on putting the database and server connection details into configuration files in a hidden folder on the windows machine.
The connection details will contain passwords. I know these should not be in plain text so how should I encrypt them so that only the application can read them?
I will try use a native installer to protect the source code.
Any advice on securing a java desktop app would be great, I really don't know much about security
The base of securely encrypting is that one secret is not stored on disk but only know by a user. If you cannot use that, you can only try obfuscation, that is all the information is there but much harder to read.
Here you could encrypt the data with a password known by the application, store pieces of the password in different variables, and then at run time:
assemble the password
read and decode the configuration
erase the variable containing the password if that last operation is possible (unsure using plain Java)
But this will not protect the data from a user that is ready to analyse the Java bytecode, it will just require a very heavy work - that's why it is not security but just objuscation.
Normal encryption would need a key (file) somewhere, and that is not too difficult.
However if you want to do it totally dynamically, without trace such a jar (zip format) which inside has some key file. Then use
Random random = new Random(42_424_242);
The Random constructor with a number selects a fixed random sequence, hence now you have got a sequence of numbers to encrypt/decrypte a byte sequence, say user xor-ing,
bytes[i] = (byte) (bytes[i] ^ random.nextInt(256));
Not unbeatable, but needs hacking.
I'm currently building an Eclipse plugin which creates and stores sensitive data, let's say "highscores".
As users can simply unpack the jar file, they are able to see the source code of the project. Thus they are able to cheat and create a faked highscore file by either changing the data in the file itself or writing to the highscore file (as they know how the data is encrypted from the source code).
Can I encrypt my jar file so that users cannot read the source code, but the plugin is still working properly?
As greg-449 already commented, that when the code is with user, it can be hacked in one way or the another. If Encryption of secure data is not going to help, you can move the secure data on your server.
Impact would be, it is available only when the user is online.
Another solution, a bit complex one:
Don't store High Score at all. Store/ log the user's activities to calculate high score. make the logs encrypted to add more security. Use salt, timestamp and secure key while encryption and a checksum to prevent manual changes to the file.
Within a Java program i've got a bunch of text files which the program reads and writes to (i know this is a really bad way to implement an app) but I need some way to ensure the integrity of the text files every time the program loads.
If the text file is deleted the program will be able to re-create it as it was last. Is there any way of doing something like this where I can store data between program executions? - But the important thing is that i'm able to change the data stored.
(Usually would use a database but it's not an option atm).
edit: (Clarify what I'm looking for)
There exists a text file full of data.
User deletes the text file.
Program detects wrong or missing file and re-creates it from a backup which the user can't get his hands on.
This is the kind of process i'm trying to implement.
You can't save data locally in a safe way. Everything that is stored on the users machine is under the users control. You can make them jump through hoops, like with using encryption or storing files in obscure formats in strange places, but you will just make it less convenient to change the files, not impossible for a determined user.
The only way to get around this is to store the data online.
This question already has answers here:
How do I "decompile" Java class files? [closed]
(19 answers)
Closed 8 years ago.
When i create a runnable jar file from a java source code file from eclipse, i believe it creates a class file which then can be run by the JVM.
The reason i am asking this is because i am going to be making an java application that keeps all my passwords. The app is going consist of an interface that asks for a password and then if the password is correct show the passwords. Here are my questions on this subject:
What exactly does a runnable *jar file* consist of?
If the runnable jar file consists of a class file, can that class file be interpreted in anyway to be able to see the source code which would revile the passwords?
when you run the runnable jar file from cmd and type "java -jar xxx". and "xxx" meaning the file name, does "-jar" mean you are going to run a jar file and "java" means run this following file in the JVM?
Is this like .exe files, which can't be un done to readable source code when turned into the .exe file.
For the unasked question: If your password is in the source code it will be in the class file in a pretty easy to find way.
A runnable jar file is a jar file (i.e. zip file with jar suffix) containing class files and special file containing information about which class to start.
You can decompile byte code: http://www.program-transformation.org/Transform/JavaDecompilers to get source code. But you can actually see the password in the byte code without decompiling it
yes. in java -jar xxx java means run the jvm using a jar file with name xxx
If you know the language and tools created you should be able to decompile exe file just as class files. And even if not passwords in the source code will be easy to find in the .exe file. So yes jar files are kind of like exe files, but they are different then what you describe.
If you want to make an application to maintain your passwords, make it so that it encrypts the stored passwords using a master password that you provide on startup as a user of the application. Never ever store passwords in source code.
Java class files can be disassembled back to readable Java code. Even easier, though, is that you can extract all passwords from the compiled class file because they are stored as plain text (try for example the strings command on a GNU machine).
So, no, the passwords are not inherently safe inside a class file (and therefore not in a jar archive, because jars can easily be unpacked).
What you need to do is implement some sort of encryption and let your program save the passwords in an encrypted file. Your program will then need you to input the decryption key to even be able to unpack the passwords. The key should not be included in the Java program, it should be provided by you each time you run the program.
Basically, however hard you try, it can be easily decompiled and read by anyone.
If you can, don't store the password, compare it online on your server (using asynchronous encryption) or store it using a master password given to the application every time.
If you can't, don't store the password as-is. The best thing I can think of is storing (and checking against) the hashcode of the password. User enters the password, it is then SHA-256'd and the resulting hashcode is compared to your stored hashcode of the password. Add salt to protect against rainbow tables.
The idea of this is that the hash function is only one-way, a password can't be recovered from its hash. Therefore, nobody should be able to get your password in a reasonable time if it's strong enough.
A very good read on this are the wiki links - but most notably this article on jasypt.org.
That said - if you tried to encrypt the file containing the hash to add another layer of security, the decrypted password could still be found using a decompiler and a debugger, so don't really count on it being supereffective against someone who would really want to get through. Therefore, it's more a security through obscurity than a real encryption.