My Java program builds some files (.html and .txt-files) in one directory.
To finish a project I’d like to “pack” these files for example to send it to another person.
But the other person should not be able to simple “unpack” the file and manipulate the html/txt-Files. Only open the packed file with my Java program.
My idea was, to zip the directory with a master password (set in the Java program) protection. After that I can send the zip-file via eMail and the other person will only be able to open it with my program.
Unfortunately there is no easy and free way to zip/unzip a folder with password in Java.
So maybe you have another idea?
Ok I think I have to explain the problem a little bit more:
I can create a small training with my program. The training contains “normal” pages with information (html-files) and pages with multiple-choice questions (the question and the answers are saved in txt-Files). In the end, the training-user can print a certification (“User XXX did the training”). To play the training the html/txt-files are read into my program.
Ok so my problem is that the user can see (and manipulate) the files. However he can see the right answer of the multiple choice test in the txt-file.
There is no 100% way to stop people reading files, but you can make it a lot more difficult. What you are need to do is encrypt the file to prevent casual reading, or to sign the file so that you can detect authenticity and tampering.
Here is an overview of how you might encrypt a file with public / private key encryption. The tools you will need are:
You will be encrypting files with the command line with GPG and reading them from Java with BouncyCastle (BC).
The steps involved in doing this in a secure way are.
Create a unique public / private key pair for signing / encryption with GPG. e.g.
gpg --gen-keyand follow the onscreen instructions. You can usually pick the default settings, and call your key something like “app-security-key@mydomain.com”Encrypt the file your Java will process. e.g.
gpg -e myfiles.zipto encrypt. You could do this from a script fairly easily if its data that changes a lot. Encryption works by encrypting the file with the public key. Someone who wishes to decrypt the file needs the corresponding private key.Export the private key from the keypair. e.g. file
gpg --export-secret-key -a > decryption.key.Create a new keyring just containing the key you exported. e.g.
mkdir tmpkeys
gpg -homedir=tmpkeys –import decryption.key
cp tmpkeys/secring.gpg keyring
In your Java program, embed the keyring as a resource or by base64 encoding it and injecting it into the code as a string. Keep your public key and ensure you do not inadvertently ship it with your app.
Use BouncyCastle PGP to open the keyring.
This is pseudo code, so read the BC APIs for details.
So basically the Java app gets an encrypted file, gives it to BC along with the private key and gets back an InputStream that it can read the plaintext from. If it has a problem it will throw an exception that you can treat as a fatal error.
Note a few things: