<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>

[{'answer_id': 55875, 'author': 'Niyaz', 'author_id': 184, 'author_profile': 'https://Stackoverflow.com/users/184', 'pm_score': 1, 'selected': False, 'text': '<p>If you are using a strong hash algorithm (SHA-2) and a strong Encryption algorithm (AES), you will do fine with this approach.</p>\n'}, {'answer_id': 55881, 'author': 'moobaa', 'author_id': 3569, 'author_profile': 'https://Stackoverflow.com/users/3569', 'pm_score': 1, 'selected': False, 'text': "<p>Why not use a compression library that supports password-protected files? I've used a password-protected zip file containing XML content in the past  :}</p>\n"}, {'answer_id': 55882, 'author': 'David Schmitt', 'author_id': 4918, 'author_profile': 'https://Stackoverflow.com/users/4918', 'pm_score': 2, 'selected': False, 'text': '<p>As Niyaz said, the approach sounds reasonable if you use a quality implementation of strong algorithms, like <a href="http://en.wikipedia.org/wiki/SHA" rel="nofollow noreferrer">SHA-265</a> and <a href="http://en.wikipedia.org/wiki/Advanced_Encryption_Standard" rel="nofollow noreferrer">AES</a> for hashing and encryption. Additionally I would recommend using a <a href="http://en.wikipedia.org/wiki/Salt_(cryptography)" rel="nofollow noreferrer">Salt</a> to reduce the possibility to create a dictionary of all password hashes.</p>\n\n<p>Of course, reading <a href="http://www.schneier.com/" rel="nofollow noreferrer">Bruce Schneier</a>\'s <a href="http://www.schneier.com/book-applied.html" rel="nofollow noreferrer">Applied Cryptography</a> is never wrong either.</p>\n'}, {'answer_id': 55904, 'author': 'erickson', 'author_id': 3474, 'author_profile': 'https://Stackoverflow.com/users/3474', 'pm_score': 6, 'selected': True, 'text': '<h3>Key Generation</h3>\n\n<p>I would recommend using a recognized algorithm such as PBKDF2 defined in <a href="ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-5v2/pkcs5v2-0.pdf" rel="nofollow noreferrer">PKCS #5 version 2.0</a> to generate a key from your password. It\'s similar to the algorithm you outline, but is capable of generating longer symmetric keys for use with AES. You should be able to find an open-source library that implements PBE key generators for different algorithms.</p>\n\n<h3>File Format</h3>\n\n<p>You might also consider using the <a href="http://www.ietf.org/rfc/rfc3852.txt" rel="nofollow noreferrer">Cryptographic Message Syntax</a> as a format for your file. This will require some study on your part, but again there are existing libraries to use, and it opens up the possibility of inter-operating more smoothly with other software, like S/MIME-enabled mail clients.</p>\n\n<h3>Password Validation</h3>\n\n<p>Regarding your desire to store a hash of the password, if you use PBKDF2 to generate the key, you could use a standard password hashing algorithm (big salt, a thousand rounds of hashing) for that, and get different values. </p>\n\n<p>Alternatively, you could compute a MAC on the content. A hash collision on a password is more likely to be useful to an attacker; a hash collision on the content is likely to be worthless. But it would serve to let a legitimate recipient know that the wrong password was used for decryption.</p>\n\n<h3>Cryptographic Salt</h3>\n\n<p><a href="http://en.wikipedia.org/wiki/Cryptographic_salt" rel="nofollow noreferrer">Salt</a> helps to thwart pre-computed dictionary attacks. </p>\n\n<p>Suppose an attacker has a list of likely passwords. He can hash each and compare it to the hash of his victim\'s password, and see if it matches. If the list is large, this could take a long time. He doesn\'t want spend that much time on his next target, so he records the result in a "dictionary" where a hash points to its corresponding input. If the list of passwords is very, very long, he can use techniques like a <a href="http://lasecwww.epfl.ch/~oechslin/projects/ophcrack/" rel="nofollow noreferrer">Rainbow Table</a> to save some space.</p>\n\n<p>However, suppose his next target salted their password. <em>Even if the attacker knows what the salt is, his precomputed table is worthless</em>&mdash;the salt changes the hash resulting from each password. He has to re-hash all of the passwords in his list, affixing the target\'s salt to the input. Every different salt requires a different dictionary, and if enough salts are used, the attacker won\'t have room to store dictionaries for them all. Trading space to save time is no longer an option; the attacker must fall back to hashing each password in his list for each target he wants to attack.</p>\n\n<p>So, it\'s not necessary to keep the salt secret. Ensuring that the attacker doesn\'t have a pre-computed dictionary corresponding to that particular salt is sufficient.</p>\n'}, {'answer_id': 55927, 'author': 'Vertigo', 'author_id': 5468, 'author_profile': 'https://Stackoverflow.com/users/5468', 'pm_score': 0, 'selected': False, 'text': "<p>Is there really need to save the hashed password into the file. Can't you just use the password (or hashed password) with some salt and then encrypt the file with it. When decrypting just try to decrypt the file with the password + salt. If user gives wrong password the decrypted file isn't correct.</p>\n\n<p>Only drawbacks I can think is if the user accidentally enters wrong password and the decryption is slow, he has to wait to try again. And of course if password is forgotten there's no way to decrypt the file.</p>\n"}]
['https://Stackoverflow.com/questions/55862', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4154/']