str和getpass输出之间的区别

时间:2013-03-10 14:17:03

标签: python string

好吧,自从我上次使用Python以来已经有一段时间了,所以我的知识有点生疏。我有一个关于getpass模块和raw_input()以及python字符串的问题。

我正在使用pycrypto模块。有一个密码字符串,使用SHA256进行哈希处理 然后使用摘要来解密AES密码。

以下代码有效:

password = "applesandpairsstairs"
print len(password)
print type(password)
plaintext = AESDecrypt(ciphertext, password, iv)
print "Plaintext: %s\n" % plaintext

我得到了输出:

20
<type 'str'>
Plaintext: This is some test data for use.

显然在程序中输入密码有点傻,所以首先我把它换成raw_input()。

password = raw_input("Enter Password: ")
print len(password)
print type(password)
plaintext = AESDecrypt(ciphertext, password, iv)
print "Plaintext: %s\n" % plaintext

当打印时,它会:

20
<type 'str'>
Plaintext: (Load of ciphertext characters)

所以它显然不起作用。注意我也尝试了一个str()围绕raw_input

最后我有代码:

password = getpass.getpass("Enter Password: ",sys.stderr)
print len(password)
print type(password)

其打印与raw_input打印相同。

AESDecrypt是一种只调用哈希和解密并返回明文的方法:

hash = SHA256.new()
hash.update(password)
key = hash.digest()
obj = AES.new(key, AES.MODE_CBC, iv)
plaintext = obj.decrypt(ciphertext)
return plaintext

有人有任何建议吗?

1 个答案:

答案 0 :(得分:0)

啊,发现了这个问题......我是个白痴。我输错了密码。

即使重复打印密码,也可以管理错过它。