encrypt html files using java swing - java

I am working on a Swing application in which i have to give the HTML files to client but I dont want the client to get access to them.
Is there any way that I can put my files either in password protected folder or I could encrypt the file and my program should access them back in the Swing application.

You could encrypt them with a shared hidden key.
One for your application, hardcoded in the software, one for yourself, to encrypt the html files with.
That's about all you can do about it. Once decrypted it is fairly easy to get the contents from it because they can always write their own class which implements your class that simply shows the content(it's about 15 lines of code really, java decompilers work pretty good these days)

Hehe, well that's of course technically possible, but at some point the cleartext will be available to your client - if not by other means, then at least by network sniffing...
Use your resources to write good code and don't bother encrypting your HTML.
Cheers,

Related

How to encrypt configuration files for a desktop java application

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.

How to mark file as already encrypted

I am trying to implement a simple encryption utility for educational pourpuses, it works, at least with simple files, but when I succesfully encrypt one file, i'd like not to encrypt it again, because that could lead to loose my data if i encrypt/decrypt it in a wrong way... Is there a way to prevent me from do it?
I am using java, and the default encryption library.
Thanks in advance
The answer to what you want to know here depends very much on how your encrypting the files in the first place.
I'll list a couple of different approaches that might help you however
Approach 1 - Scripting
If your using a 3rd party tool such as an encryption util written by another java programmer, and if your running this tool in some kind of a shell session, your best bet might be to wrap the invocation of said tool in a shell script.
If your running on windows this could be a batch file, on Linux a bash script.
Essentially you use this approach by working out ahead of time what command you need to use, then putting that command into said shell script while substituting any parameters you need to change.
Following on from the wrapped command you could then provide further commands to rename the file, or embed some kind of information in it's file properties or file name, a possible example might be something like:
IF NOT EXIST %%0.encrypted(
encrypt %%0 -a -b -c -d
rename %%0 %%0.encrypted) ELSE (
)
ELSE
()
NOTE: These are just theoretical examples as I don't know what your OS is
If this was saved in a file called 'myencrypt.bat', then you could just type
myencrypt.bat afile.ext
Approach 2 - Custom Bytes
If you have direct control of the source code and consequently the application that performs this encryption, then why not make a pseudo file format.
Add some kind of a marker into the file that your program then checks for.
By way of an example, you could perhaps
add the following string to the front of the file
ENCFOriginalFile.Ext
Turn that into a set of bytes, then load the file in, encrypt it, add the bytes from the text on and save it back out, maybe with a custom file extension.
When you come to encrypt a file again, all you then need to do is read the first 4 bytes and if they are equal to ENCF you know that the file is already encrypted.
Those are just 2 ideas I can think of off the top of my head, but it's late here and I'm tired. If I was more awake I could probably come up with a whole page full.
Since it is encrypted it cannot be opened in the default program for that file type, so you can savely rename the file. This can be done for example by adding .enc as the extension. Doing so will make it easy to spot the encrypted file for you and your java application.
Depending on your use case you can also let your java application manage a database of encrypted files.

Can runnable jar files be interpreted back into java source code [duplicate]

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.

Making a folder lock application using java

I've got a project from my college to make a folder lock application using Java.
I have no idea how to go about it. Please give some clues on how to make a folder\file password protected ?.
It is totally native dependent.
You need to protect/encrypt/hide the folder using native features.
and if your app validates the user reverse the process
A daemon thread or service will be running,for lock folder management that is for locking,unlocking, displaying alerts etc..
Going with fast and efficient encryption and decryption algorithm is also another challenge as the number of files in the folders grow ,the efficiency should not decline.
At the same time,users and password list should be secured which should not be os dependents. File tampering etc should also be dealt with !!
If you dont want it dependent on a specific OS you can go this way:
zip it using:
http://download.oracle.com/javase/1.3/docs/api/java/util/zip/package-summary.html
encrypt it using:
http://download.oracle.com/javase/1.4.2/docs/guide/security/CryptoSpec.html
delete safely the original data by first replacing all the characters in the files with random data and then deleting those files.

Android: .java files readable from .apk file?

I'm currently developing an application for a company which includes livescoring. The XML-files I access (from the net like: "http://company.com/files/xml/livescoring.xml") are not intended to be public and should only known to me.
I was wondering if it is possible for anyone to decode the .apk file and read my original .java files (which include the link to the XML files).
So, I renamed the .apk file to .zip and could access the "classes.dex", which seemed to include the .java files (or classes). Googling led me to a tool named "AvaBoxV2" which decoded this "classes.dex" file. Now I have a folder including an "out" folder where files named .smali exist. I opend one of these with an editor and finally there is the link to the xml file. Not good. :(
Is there a way to encrypt my app or the classes.dex file? I don't want to tell that company, that anyone can access the original xml-files. Maybe signing the app probably helps?
Also, do you know a really noob-friendly tutorial to prepare apps (signing, versioning,...) for Google Market?
Thanks in advance!
The .java source code is not included in the APK.
It is possible to disassemble the Dalvik bytecode into bytecode mnemonics using a tool like baksmali, but there's no way a user can recover the original .java source.
Furthermore, you can use a tool like proguard (included in the Android SDK) to obfuscate your byte code, making it hard to interpret the behavior of the disassembled bytecode.
You can make small tricks too, like storing the link string in some sort of obfuscated form, and then de-obfuscating it at run-time in your app (a simple example would be to use base 64 encoding, but someone could probably reverse that quickly if they wanted to).
That said, it's pretty trivial for someone to run tcpdump and sniff the network traffic between your device and the server, and get the URL that way, so there's no way to completely prevent anyone from getting this value.
Yeah, its impossible to fully prevent something like this. Its the same on a desktop application, or any other application.
As mentioned, obfuscation will help, but people who are persistent can still get past it. Especially for something like a url like that.
One solution of making it much more tricky for hackers is to use PHP on your webserver and some sort of token system to determine if the request is coming from your app or not... That would get a bit tricky though, so I don't really suggest it.

Categories