File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes How to create password protected file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to create password protected file" Watch "How to create password protected file" New topic
Author

How to create password protected file

deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

please I want to create "fileName.anyExtention" with Password protected. that file must not be able to read/write/delete without the correct password. can anybody tell me the methods or tricks that I can use for this.it is better if you can give code example also

thank you...


Do or die...?? No, Do before you die... || (SCJP .6)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I've edited your post and removed the formatting. Please don't use colours and different sizes other than for highlighting.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

Wouter Oet wrote:I've edited your post and removed the formatting. Please don't use colours and different sizes other than for highlighting.


it's OK dear.. thank for that.but I wanted to highlight some of part of it... so what about that....I'm really confused..
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You made your entire post blue and with a bigger size. That isn't really highlighting. Also making about half your post bold isn't neither. Feel free to edit your post to add some highlighting but be selective about it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Check out CipherOutputStream wrapped around FileOutputStream for writing, CipherInputStream wrapped around FileInputStream for reading. You'll need a proper encryption algorithm like AES to create the Cipher objects needed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Rob Spoor wrote:Check out CipherOutputStream wrapped around FileOutputStream for writing, CipherInputStream wrapped around FileInputStream for reading. You'll need a proper encryption algorithm like AES to create the Cipher objects needed.


Based on his original posting I don't think the OP wants to encrypt the file. On can certainly write to and delete a a password encrypted file and one can read it though not make sense of what one is reading without knowing the password. It sounds to me like the OP requires a password protected file system.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Well, without the proper key (password) the data is gibberish, so it comes close. I don't think Java allows files or file systems to become password protected without JNI.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2864
    
  11

I would have answered the question the same way Rob did, but on closer reading, I can see James's point. Encrypting a file certainly wouldn't prevent someone from deleting or writing to the file. That could be accomplished by setting the file's owner and read, write, execute bits on Mac, Unix, or Linux. You'd then have to "su" to the correct user and enter a password to be able to read, write, or delete that file. Windows has a different set of capabilities, but you can protect files more or less the same way. All of this is OS-related though. It would be tricky to get a Java interface to it, but maybe not impossible.
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

deca leni wrote:please I want to create "fileName.anyExtention" with Password protected. that file must not be able to read/write/delete without the correct password. can anybody tell me the methods or tricks that I can use for this.it is better if you can give code example also

thank you...


can anybody tell me methods or technical information about file protection that we can use to protect a file?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Well, what do you want to protected the file against? From deletion, overwriting, or only reading?
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

mmmm ok imagine. that I want to save some valuable data (like sensitive data) in file.. if some one get that file he/she can read it without any problem or it can be deleted.it is better if I make that file read/write/delete protected.isn't it? so?
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

can I do this thing in java?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

That depends on how sensitive the data is. If it is really sensitive then making the file read/write/delete protected isn't enough, then you'll need to encrypt it. See previous comments about how to accomplish that. If it is not so sensitive then you could make the file private to your windows/linux/unix/whatever user. Then you need your password to be able to open it.
Arun Chidam
Ranch Hand

Joined: May 08, 2006
Posts: 79
Dunno wether it hepls in your case....what about PGP encryption???

PGP Encryption


SCJP 1.4, SCWCD 1.4
Jack Bearden
Greenhorn

Joined: May 17, 2011
Posts: 2
Have the application you are writing store the information on a remote, password protected database. That should be fairly easy to set up.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

In my opinion is setting up a database, making it accessible to the world, making sure it rule 24/7 (otherwise you won't be able to access your files) and securing communication with the database a whole lot more complicated than invoking "a couple" java methods.
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

I do not want to save the file in database.

I found these things in this thread,

1. encrypting -> if I use this,which type of encrypting is good ?
2.JNI(java native interface) -> in which way can I use this?
3.CipherOutputStream wrapped around FileOutputStream for writing, CipherInputStream wrapped around FileInputStream for reading -> how helpful is this?

my main idea is to make the that file must not be able to read/write/delete without the correct password.that file is used store sensitive Data(like passwards)

so please can anybody enplane me how useful and ways of using them...

thank you all
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 245

In the book "Core Java - Volume II - Advanced Features - Java SE 6 - Eighth Edition" (Sun Press) There is a complete source code listing of an encryption/decryption program. This book is the latest in the "Core Java" series. This one covers SE 6.


Sent from my IBM 360 mainframe
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Carey Brown wrote:In the book "Core Java - Volume II - Advanced Features - Java SE 6 - Eighth Edition" (Sun Press) There is a complete source code listing of an encryption/decryption program. This book is the latest in the "Core Java" series. This one covers SE 6.


Encryption has already been suggested but the OP has stated more than once that he want to read,write and delete protect the file and encryption does not achieve this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create password protected file