<p>I'm using an identical call to "CryptUnprotectData" (exposed from Crypt32.dll) between XP and Vista. Works fine in XP. I get the following exception when I run in Vista:</p>

<pre><code>"Decryption failed. Key not valid for use in specified state."
</code></pre>

<p>As expected, the versions of crypt32.dll are different between XP and Vista (w/XP actually having the more recent, possibly as a result of SP3 or some other update).</p>

<p>More specifically, I'm encrypting data, putting it in the registry, then reading and decrypting using "CryptUnprotectData". UAC is turned off.</p>

<p>Anyone seen this one before?</p>

[{'answer_id': 64301, 'author': 'Roger Lipscombe', 'author_id': 8446, 'author_profile': 'https://Stackoverflow.com/users/8446', 'pm_score': 3, 'selected': True, 'text': '<p>The <a href="http://msdn.microsoft.com/en-us/library/aa380882.aspx" rel="nofollow noreferrer">CryptUnprotectData function</a> documentation states that it usually only works when the user has the same logon credentials as the encrypter.</p>\n\n<p>This suggests to me that maybe the key is tied to the user\'s current token. Since you mention Vista, this makes me think UAC and restricted tokens.</p>\n\n<p>Can you show us some code? Can you give us more information about what you\'re doing with the data -- i.e. are you moving it between processes, or users, or computers?</p>\n'}, {'answer_id': 64646, 'author': 'Scott Marlowe', 'author_id': 1683, 'author_profile': 'https://Stackoverflow.com/users/1683', 'pm_score': 1, 'selected': False, 'text': "<p>Nice. Hopefully this is my bone-head move of the week! ;-)</p>\n\n<blockquote>\n  <p>This suggests to me that maybe the key\n  is tied to the user's current token.</p>\n</blockquote>\n\n<p>That was it. Turns out I was using encrypted data from another machine (the XP one) and trying to decrypt on the Vista machine.</p>\n\n<p>As the MSDN documentation states:</p>\n\n<blockquote>\n  <p>Usually, only a user with the same\n  logon credentials as the encrypter can\n  decrypt the data. In addition, <strong>the\n  encryption and decryption must be done\n  on the same computer.</strong></p>\n</blockquote>\n\n<p>Once I re-encrypted the data on the Vista machine, decryption works as expected.</p>\n\n<p>Thanks.</p>\n"}]
['https://Stackoverflow.com/questions/64258', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1683/']