Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a file protected by a password in java

I would like to create a file protected by a password in JAVA. What I mean is, once I launch the program, one file created by my program would be directly protected by previously determined password.

Is there an easy way to do it ?

Once again, my aim is not to create a file and then add it a password, but right during the creation protecting the file by a password. Actually, I want the current runner program not having access in reading/editing the created file EXCEPT if he/she has the password previously set.

So anyway, if some of you know an easy way to protect files when writing them thanks to java, I would be most grateful.

Have a nice day!

like image 504
duckduck Avatar asked Aug 11 '12 11:08

duckduck


People also ask

Can I password protect a single file?

Use encryption to password protect a folder or a file Navigate to the folder or file you want to encrypt. Right-click on the item, click Properties, then click Advanced. Check Encrypt contents to secure data. Click OK, then click Apply.

How do you create a password protected zip file and unzip it in Java?

It has support for streams like ZipInputStream and ZipOuputStream. We can use this library to create any password-protected zip file in java. We can zip/unzip any existing file or directory by just specifying the file path. It also provides supports for both AES 128/256 Encryption and standard Zip Encryption.


1 Answers

You want to encrypt your file('s content) with a password. Here is a pretty well known library to do it: http://www.jasypt.org/

From their site:

..encrypting and decrypting a text...

BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword(myEncryptionPassword);
String myEncryptedText = textEncryptor.encrypt(myText);
...
String plainText = textEncryptor.decrypt(myEncryptedText);

You can read/write the encrypted content to your file.

like image 172
Enno Shioji Avatar answered Sep 21 '22 14:09

Enno Shioji