Python win32crypt.CryptProtectData 2.5和3.1之间的区别?
我正在尝试对密码进行哈希处理以转储到 .rdp 文件中。我发现一个网站(或多或少)展示了如何做到这一点 此处,但在 3.1 中不起作用。
在 2.5.4 中,我得到了这个:
>>> import win32crypt
>>> import binascii
>>> pwdHash = win32crypt.CryptProtectData(u"password",u'psw',None,None,None,0)
>>> print str(binascii.hexlify(pwdHash)).upper()
01000000D08C9DDF0115D1118C7A00C04FC297EB010000007E9E... blah, blah blah
在 3.1 中,我得到了这个(3.1 中的所有内容都是 unicode,所以只需抛弃 u",对吧?):
>>> pwdHash = win32crypt.CryptProtectData("password",'psw',None,None,None,0)
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
pwdHash = win32crypt.CryptProtectData("password",'psw',None,None,None,0)
TypeError: expected an object with a buffer interface
好的,我以前见过该错误,通常这意味着我需要先转换为字节,所以:
>>> pwdHash = win32crypt.CryptProtectData("password".encode(),'psw',None,None,None,0)
>>> print(str(binascii.hexlify(pwdHash)).upper())
B'01000000D08C9DDF0115D1118C7A00C04FC297EB010000007E9E... blah, blah, blah
>>>
这看起来一切都很好,但是当您将该十六进制值转储到 .rdp 文件中时,这不起作用,我只能假设那是因为它不是 unicode 字符串“密码”的十六进制加密,而是十六进制-crypt 字节“密码”。我尝试了 .decode() 但这正是您所期望的并将十六进制加密字节转换为字符串,它不会为您提供十六进制加密字符串。原始的 unicode 字符串。
我疯狂地搜索了有关 win32crypt.CryptProtectData 的任何信息,但找不到任何有用的信息来说明为什么它现在需要字节或缓冲区对象而不是字符串,
有人可以帮忙吗? (或者有谁知道一种更简单的方法来将密码输入到我通过 Python 以编程方式打开的远程桌面会话中?呵呵)
I'm trying to hash a password to dump into a .rdp file. I found a site that (more or less) shows how to do that here but it doesn't work in 3.1.
In 2.5.4 I get this:
>>> import win32crypt
>>> import binascii
>>> pwdHash = win32crypt.CryptProtectData(u"password",u'psw',None,None,None,0)
>>> print str(binascii.hexlify(pwdHash)).upper()
01000000D08C9DDF0115D1118C7A00C04FC297EB010000007E9E... blah, blah blah
In 3.1 I get this (everything's unicode in 3.1 so just ditch the u" right?):
>>> pwdHash = win32crypt.CryptProtectData("password",'psw',None,None,None,0)
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
pwdHash = win32crypt.CryptProtectData("password",'psw',None,None,None,0)
TypeError: expected an object with a buffer interface
OK, I've seen that error before and usually that just means I need to convert to bytes first, so:
>>> pwdHash = win32crypt.CryptProtectData("password".encode(),'psw',None,None,None,0)
>>> print(str(binascii.hexlify(pwdHash)).upper())
B'01000000D08C9DDF0115D1118C7A00C04FC297EB010000007E9E... blah, blah, blah
>>>
Which seems all well and good but that doesn't work when you dump that hexed value into an .rdp file, I can only assume that's because it isn't a hex-crypt of the unicode string 'password' but a hex-crypt of the bytes 'password'. I've attempted a .decode() but that does just what you'd expect and makes the hex-crypt bytes into a string, it doesn't give you the hex-crypt string for the original unicode string.
I've googled like crazy for any info on the win32crypt.CryptProtectData and I can't find any useful information on why it now requires a bytes or buffer object instead of a string.
Can anybody help?
(or does anyone know of an easier way to feed a password into a Remote Desktop session I'm opening programatically through Python? hehe)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 "password".encode('utf-16-le') 代替。
Use "password".encode('utf-16-le') instead.