title: How to implement password protection for individual files?
body: <p>I'm writing a little desktop app that should be able to encrypt a data file and protect it with a password (i.e. one must enter the correct password to decrypt).  I want the encrypted data file to be self-contained and portable, so the authentication has to be embedded in the file (or so I assume).</p>
  
  <p>I have a strategy that appears workable and seems logical based on what I know (which is probably just enough to be dangerous), but I have no idea if it's actually a good design or not.  So tell me: is this crazy?  Is there a better/best way to do it?</p>
  
  <ul>
  <li>Step 1: User enters plain-text password, e.g. "MyDifficultPassword"  </li>
  <li>Step 2: App hashes the user-password and uses that value as the symmetric key to encrypt/decrypt the data file.  e.g. "MyDifficultPassword" --> "HashedUserPwdAndKey".  </li>
  <li>Step 3: App hashes the hashed value from step 2 and saves the new value in the data file header (i.e. the unencrypted part of the data file) and uses that value to validate the user's password.  e.g. "HashedUserPwdAndKey" --> "HashedValueForAuthentication"</li>
  </ul>
  
  <p>Basically I'm extrapolating from the common way to implement web-site passwords (when you're not using OpenID, that is), which is to store the (salted) hash of the user's password in your DB and never save the actual password.  But since I use the hashed user password for the symmetric encryption key, I can't use the same value for authentication.  So I hash it again, basically treating it just like another password, and save the doubly-hashed value in the data file.  That way, I can take the file to another PC and decrypt it by simply entering my password.</p>
  
  <p>So is this design reasonably secure, or hopelessly naive, or somewhere in between?  Thanks!</p>
  
  <p>EDIT: clarification and follow-up question re: Salt.<br>
  I thought the salt had to be kept secret to be useful, but your answers and links imply this is not the case.  For example, <a href="ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-5v2/pkcs5v2-0.pdf" rel="noreferrer">this spec</a> linked by erickson (below) says: </p>
  
  <blockquote>
    <p>Thus, password-based key derivation as defined here is a function of a password, a salt, and an iteration count, where the latter two quantities need not be kept secret.</p>
  </blockquote>
  
  <p>Does this mean that I could store the salt value in the same place/file as the hashed key and still be more secure than if I used no salt at all when hashing?  How does that work?</p>
  
  <p>A little more context: the encrypted file isn't meant to be shared with or decrypted by others, it's really single-user data.  But I'd like to deploy it in a shared environment on computers I don't fully control (e.g. at work) and be able to migrate/move the data by simply copying the file (so I can use it at home, on different workstations, etc.).</p>