问题描述
有没有办法在Windows XP上使用DPAPI(数据保护应用程序编程接口)与Python?我希望使用现有的模块,如果有一个可以做到的不幸的是,我无法找到与Google或Stack Overflow的方法。
编辑:我已经将示例代码指向通过dF,并将其调整为一个独立的库,可以在高级别上使用DPAPI在用户模式下进行隐藏和解密。只需调用返回一个加密字符串的dpapi.cryptData(text_to_encrypt),或者返回decryptData(encrypted_data_string),返回纯文本。这是图书馆:
#DPAPI访问库
#此文件使用最初由Crusher Joe创建的代码:
#http://article.gmane.org/gmane.comp.python.ctypes/420
#
from ctypes import *
from ctypes.wintypes import DWORD
LocalFree = windll.kernel32.LocalFree
memcpy = cdll.msvcrt.memcpy
CryptProtectData = windll.crypt32.CryptProtectData
CryptUnprotectData = windll.crypt32.CryptUnprotectData
CRYPTPROTECT_UI_FORBIDDEN = 0x01
extraEntropy =cl; ad13 \0al; 323kjd#(adl; k $#ajsd
class DATA_BLOB(Structure):
_fields_ = [ cbData,DWORD),(pbData,POINTER(c_char))]
def getData(blobOut):
cbData = int(blobOut.cbData)
pbData = blobOut .pbData
buffer = c_buffer(cbData)
memcpy(buffer,pbData,cbData)
LocalFree(pbData);
return buffer.raw
def Win32CryptProtectData(plainText,熵):
bufferIn = c_buffer(p lainText,len(plainText))
blobIn = DATA_BLOB(len(plainText),bufferIn)
bufferEntropy = c_buffer(熵,len(熵))
blobEntropy = DATA_BLOB(len(熵)
blobOut = DATA_BLOB()
如果CryptProtectData(byref(blobIn),upython_data,byref(blobEntropy),
无,无,CRYPTPROTECT_UI_FORBIDDEN,byref(blobOut )
return getData(blobOut)
else:
return
def Win32CryptUnprotectData(cipherText,entropy):
bufferIn = c_buffer(cipherText ,len(cipherText))
blobIn = DATA_BLOB(len(cipherText),bufferIn)
bufferEntropy = c_buffer(熵,len(熵))
blobEntropy = DATA_BLOB(len(熵),bufferEntropy )
blobOut = DATA_BLOB()
如果CryptUnprotectData(byref(blobIn),None,byref(blobEntropy)),None,None,
CRYPTPROTECT_UI_FORBIDDEN,byref(blobOut)):
return getData(blobOut)
else:
return
def cryptData(text):
return Win32CryptProtectData(text,extraEntropy)
def decryptData(cipher_text) :
return Win32CryptUnprotectData(cipher_text,extraEntropy)
我通过ctypes使用 CryptProtectData
和 CryptUnprotectData
,代码来自
,并且运行良好。
Is there a way to use the DPAPI (Data Protection Application Programming Interface) on Windows XP with Python?
I would prefer to use an existing module if there is one that can do it. Unfortunately I haven't been able to find a way with Google or Stack Overflow.
EDIT: I've taken the example code pointed to by "dF" and tweaked it into a standalone library which can be simply used at a high level to crypt and decrypt using DPAPI in user mode. Simply call dpapi.cryptData(text_to_encrypt) which returns an encrypted string, or the reverse decryptData(encrypted_data_string), which returns the plain text. Here's the library:
# DPAPI access library
# This file uses code originally created by Crusher Joe:
# http://article.gmane.org/gmane.comp.python.ctypes/420
#
from ctypes import *
from ctypes.wintypes import DWORD
LocalFree = windll.kernel32.LocalFree
memcpy = cdll.msvcrt.memcpy
CryptProtectData = windll.crypt32.CryptProtectData
CryptUnprotectData = windll.crypt32.CryptUnprotectData
CRYPTPROTECT_UI_FORBIDDEN = 0x01
extraEntropy = "cl;ad13 \0al;323kjd #(adl;k$#ajsd"
class DATA_BLOB(Structure):
_fields_ = [("cbData", DWORD), ("pbData", POINTER(c_char))]
def getData(blobOut):
cbData = int(blobOut.cbData)
pbData = blobOut.pbData
buffer = c_buffer(cbData)
memcpy(buffer, pbData, cbData)
LocalFree(pbData);
return buffer.raw
def Win32CryptProtectData(plainText, entropy):
bufferIn = c_buffer(plainText, len(plainText))
blobIn = DATA_BLOB(len(plainText), bufferIn)
bufferEntropy = c_buffer(entropy, len(entropy))
blobEntropy = DATA_BLOB(len(entropy), bufferEntropy)
blobOut = DATA_BLOB()
if CryptProtectData(byref(blobIn), u"python_data", byref(blobEntropy),
None, None, CRYPTPROTECT_UI_FORBIDDEN, byref(blobOut)):
return getData(blobOut)
else:
return ""
def Win32CryptUnprotectData(cipherText, entropy):
bufferIn = c_buffer(cipherText, len(cipherText))
blobIn = DATA_BLOB(len(cipherText), bufferIn)
bufferEntropy = c_buffer(entropy, len(entropy))
blobEntropy = DATA_BLOB(len(entropy), bufferEntropy)
blobOut = DATA_BLOB()
if CryptUnprotectData(byref(blobIn), None, byref(blobEntropy), None, None,
CRYPTPROTECT_UI_FORBIDDEN, byref(blobOut)):
return getData(blobOut)
else:
return ""
def cryptData(text):
return Win32CryptProtectData(text, extraEntropy)
def decryptData(cipher_text):
return Win32CryptUnprotectData(cipher_text, extraEntropy)
I have been using CryptProtectData
and CryptUnprotectData
through ctypes, with the code from
http://article.gmane.org/gmane.comp.python.ctypes/420
and it has been working well.
这篇关于使用DPAPI与Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!