Python - 错误CryptProtectData密钥无法在指定状态下使用

时间:2018-02-08 00:02:47

标签: python python-2.7 google-chrome cryptprotectdata

我有脚本用Python脚本导出chrome密码使用win32 api函数CryptProtectData但解密密码不起作用下面是代码和错误 ERROR

Traceback (most recent call last):
  File "E:\www\scripts_py\chromepass\aa.py", line 23, in <module>
    pwd = win32crypt.CryptUnprotectData(pwd, None, None, None, 0) #This returns a tuple description and the password
pywintypes.error: (-2146893813, 'CryptProtectData', 'Key not valid for use in specified state.')

CODE

import os
import sqlite3
import win32crypt

#path to user's login data
data_path = os.path.expanduser('~')+"\AppData\Local\Google\Chrome\User Data\Default"

login_db = os.path.join(data_path, 'Login Data')

#db connect and query
c = sqlite3.connect(login_db)
cursor = c.cursor()
select_statement = "SELECT origin_url, username_value, password_value FROM logins"
cursor.execute(select_statement)

login_data = cursor.fetchall()

#URL: credentials dictionary
credential = {}

#decrytping the password
for url, user_name, pwd, in login_data:
    pwd = win32crypt.CryptUnprotectData(pwd, None, None, None, 0) #This returns a tuple description and the password
    credential[url] = (user_name, pwd[1])

#writing to a text file (CAUTION: Don't leave this text file around!)
prompt = raw_input("[.] Are you sure you want to write all this sensitive data to a text file? \n[.]  or \n[>] ")
if prompt == 'y':
    with open('pwd.txt', 'w') as f:
        for url, credentials in credential.iteritems():
            if credentials[1]:
                f.write("\n"+url+"\n"+credentials[0].encode('utf-8')+ " | "+credentials[1]+"\n")
            else:
                f.write("\n"+url+"\n"+"USERNAME NOT FOUND | PASSWORD NOT FOUND \n")
    print "[.] Successfully written to pwd.txt!"
else:
    quit()

1 个答案:

答案 0 :(得分:1)

每台计算机都有自己的本地加密密钥,因此,只有对数据进行加密的计算机才能对其进行解密。 我假设在您的情况下,您尝试从另一台计算机读取登录数据,而您的Windows计算机没有解密密码所需的密钥,因此您得到了以上错误。 我建议在源计算机上解密数据,然后通过打开的套接字/电子邮件将结果传递到目的地。