gpt4 book ai didi

python - 在 Python 中使用 DPAPI?

转载 作者:太空狗 更新时间:2023-10-29 17:06:56 26 4
gpt4 key购买 nike

有没有办法通过 Python 在 Windows XP 上使用 DPAPI(数据保护应用程序编程接口(interface))?

如果有可以做到的模块,我宁愿使用现有的模块。不幸的是,我无法通过 Google 或 Stack Overflow 找到解决方法。

编辑: 我采用了“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)

最佳答案

我一直在通过 ctypes 使用 CryptProtectDataCryptUnprotectData,代码来自

http://article.gmane.org/gmane.comp.python.ctypes/420

它一直运行良好。

关于python - 在 Python 中使用 DPAPI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/463832/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com