title: Decrypting Windows Wireless Passwords using Python
body: <p>I'm trying to decrypt the Windows wireless password stored in the profile xml file using Python.  I came across a <a href="http://jbarillari.blogspot.com/2009/06/security-lols.html" rel="nofollow">blog post</a> giving an example of how to do it calling Windows <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa380882%28v=vs.85%29.aspx" rel="nofollow">CryptUnprotectData</a> using Python's win32crypt module.  My problem is I get the <code>Key not valid for use in specified state</code> error and need to run it using LocalSystem. </p>
  
  <blockquote>
    <p>You will get that error even if you run cmd.exe as an administrator. Here's where you need to know a bit about Windows that, as a Windows n00b, I didn't know: the LocalSystem account is different from the administrator privilege. In order to run cmd.exe with the LocalSystem account, you need to install a Microsoft package called PsTools. Inside PsTools a program called PsExec, which is a little bit like sudo on Un*x. Just download the zip linked at the bottom of the Microsoft TechNet page above and unzip it somewhere where you can find it.</p>
    
    <p>To use PsExec, open cmd.exe as an administrator (open the start menu in the bottom-left of your screen, type cmd.exe into the search box, and press Ctrl+Shift+Enter to run it as an admin). Hit "continue" on the User Account Control dialog box that opens. In the command shell that opens, navigate to the directory where you unzipped PsTools. Now run "psexec.exe /s /i cmd.exe". After you agree to PsTools's EULA, PsTools should open a new cmd.exe shell window
    running as LocalSystem.</p>
  </blockquote>
  
  <p>Is there a way around this error without using <code>psexec.exe</code> as the blog post states?  Perhaps using CryptoPy or PyCrypto?</p>
  
  <p>For reference, the encrypted password I retrieve is the keyMaterial key from the Windows Vista profile xml file.</p>
  
  <p>The code I'm using:</p>
  
  <pre class="lang-py prettyprint-override"><code>import win32crypt
  mykey='01000000D08C9DDF.....' # 308 characters long
  binout = []
  for i in range(len(mykey)):
      if i % 2 == 0:
          binout.append(chr(int(mykey[i:i+2],16)))
  pwdHash=''.join(binout)
  
  output =  win32crypt.CryptUnprotectData(pwdHash,None,None,None,0)
  
  print "hex:", "".join(["%02X" % ord(char) for char in output[1]])
  
  print "ascii:", output[1]
  </code></pre>
  
  <p>Thanks in advance.</p>