有没有办法在Windows和Python一起使用DPAPI (数据保护应用程序编程接口)?
我宁愿使用一个现有的模块,如果有一个可以做到这一点。不幸的是,我还没有找到一种使用Google或Stack溢出的方法。
编辑:--我已经以"dF“所指的示例代码为例,将其调整为一个独立的库,该库可以简单地在高级使用,在用户模式下使用DPAPI进行加密和解密。只需调用dpapi.cryptData(text_to_encrypt)返回加密字符串,或调用反向decryptData(encrypted_data_string)返回纯文本。这是图书馆:
# 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)发布于 2009-01-21 01:37:54
我一直通过ctype使用CryptProtectData和CryptUnprotectData,代码来自
http://article.gmane.org/gmane.comp.python.ctypes/420
而且一直运作良好。
发布于 2009-10-13 19:20:07
另外,pywin32在win32crypt模块中实现了CryptProtectData和CryptUnprotectData。
发布于 2009-01-21 01:34:50
最简单的方法是使用铁Python。
https://stackoverflow.com/questions/463832
复制相似问题