我知道如何加密:
encrypted = hashlib.sha256('1234').hexdigest()
Run Code Online (Sandbox Code Playgroud)
但我不确定如何解密?
decrypted = decrypt(encrypted)
Run Code Online (Sandbox Code Playgroud)
Ani*_*tla 21
像sha256这样的散列点是它应该是一个单向函数(虽然真正的单向函数的存在仍然是一个悬而未决的问题,参见http://en.wikipedia.org/wiki/One-way_function) .
注意http://en.wikipedia.org/wiki/Cryptographic_hash_function:
The ideal cryptographic hash function has four main properties:
1. it is easy to compute the hash value for any given message
2. it is infeasible to generate a message that has a given hash
3. it is infeasible to modify a message without changing the hash
4. it is infeasible to find two different messages with the same hash.
Run Code Online (Sandbox Code Playgroud)
如果您可以撤销它,那么您将违反规则2.这些规则允许人们告诉另一方他们有一些信息(例如密码),而不会泄露信息.例如,请参阅维基百科:http://en.wikipedia.org/wiki/Cryptographic_hash_function#Illustration
如果您需要可逆性,请参阅根据密码对字符串进行编码的简单方法?,你可以使用像Vignere这样的弱点,但也有一个使用PyCrypto的例子:
from Crypto.Cipher import AES
import base64
cipher = AES.new(secret_key,AES.MODE_ECB) # never use ECB in strong systems obviously
encoded = base64.b64encode(cipher.encrypt(msg_text))
# ...
decoded = cipher.decrypt(baes64.b64decode(msg_text))
Run Code Online (Sandbox Code Playgroud)
如果需要可逆散列函数,请参阅可逆散列函数?
小智 6
这是一个有效的问题,但可能没有正确提出。
OP,我认为您想要做的是将散列值与未散列值进行检查?
hashed = hashlib.sha256('1234').hexdigest()
hashedstring = '1234' + ',' + hashed
Run Code Online (Sandbox Code Playgroud)
现在检查哈希值 == 原始值。所以解析出逗号之前和之后的部分。哈希 1234 并将其与哈希值进行比较。
def check_secure_val(h):
commapos = h.find(",")
val = h[0:commapos]
hashval = h[commapos+1:-1]
rehashval = hash_str(val)
if rehashval == hashval:
return val
Run Code Online (Sandbox Code Playgroud)
其中输入 h 是格式为“val,(HASHEDSTRING)”的字符串
hash_str 是一个散列函数。
简短的回答是你不能'解密'哈希; 它是单向函数.加密和散列之间存在重大差异.
哈希
见http://en.wikipedia.org/wiki/Cryptographic_hash_function
注意:可以"BREAK"某些哈希算法,但这不是解密.您可以在链接中找到更多信息以及python也支持的其他算法
加密
和http://en.wikipedia.org/wiki/Encryption
例
散列的一个有用示例是将密码存储在数据库中,而加密的一个有用示例是将您的银行详细信息发送到在线商店以购买某些东西.
| 归档时间: |
|
| 查看次数: |
59938 次 |
| 最近记录: |