我如何在python中使用hashlib解密?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15956952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:27:31  来源:igfitidea点击:

How do i decrypt using hashlib in python?

pythonhashlib

提问by Kamilski81

I know how to encrypt:

我知道如何加密:

encrypted = hashlib.sha256('1234').hexdigest()

but I'm not sure how to decrypt this??

但我不知道如何解密这个??

decrypted = decrypt(encrypted)

采纳答案by Anil Vaitla

The point of a hash like sha256 is that it is supposed to be a one way function (although the existence of true one way functions is still an open question, see http://en.wikipedia.org/wiki/One-way_function).

像 sha256 这样的散列的要点是它应该是单向函数(尽管真正的单向函数的存在仍然是一个悬而未决的问题,请参阅http://en.wikipedia.org/wiki/One-way_function) .

Note http://en.wikipedia.org/wiki/Cryptographic_hash_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
    1. it is infeasible to generate a message that has a given hash
    1. it is infeasible to modify a message without changing the hash
    1. it is infeasible to find two different messages with the same hash.
    1. 很容易计算任何给定消息的哈希值
    1. 生成具有给定散列的消息是不可行的
    1. 在不改变散列的情况下修改消息是不可行的
    1. 找到两个具有相同散列的不同消息是不可行的。

If you could reverse it then you'd be breaking rule 2. These rules allow one to tell another party that they have some information (such as a password), without revealing the information. For example, see wikipedia: http://en.wikipedia.org/wiki/Cryptographic_hash_function#Illustration

如果你能逆转它,那么你就违反了规则 2。这些规则允许一个人告诉另一方他们有一些信息(例如密码),而不会泄露这些信息。例如,参见维基百科:http: //en.wikipedia.org/wiki/Cryptographic_hash_function#Illustration

If you need invertibility see Simple way to encode a string according to a password?, you can use something weak like Vignere, but there is also an example using PyCrypto:

如果您需要可逆性,请参阅根据密码对字符串进行编码的简单方法?,您可以使用像 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))

If you want a reversible hash function, see Reversible hash function?

如果您想要可逆散列函数,请参阅可逆散列函数?

回答by chrisw

The short answer is you cannot 'decrypt' a hash; it is a one way function. There is a major difference between encrypting and hashing.

简短的回答是你不能“解密”一个散列;它是一种单向函数。加密和散列之间有一个主要区别。

Hashing

散列

See http://en.wikipedia.org/wiki/Cryptographic_hash_function

请参阅http://en.wikipedia.org/wiki/Cryptographic_hash_function

Note: It is possible to 'BREAK' certain hashing algorithms, but this is not decrypting. You'll find more information in the links as well as other algorithms that are also supported by python

注意:可以“打破”某些散列算法,但这不是解密。您将在链接以及 python 支持的其他算法中找到更多信息

Encryption

加密

and http://en.wikipedia.org/wiki/Encryption

http://en.wikipedia.org/wiki/Encryption

Example

例子

A useful example of hashing is storing passwords in a database whereas a useful example of encryption is sending your bank details to an online store to purchase something.

散列的一个有用示例是将密码存储在数据库中,而加密的一个有用示例是将您的银行详细信息发送到在线商店以购买东西。

回答by scottydelta

The hashes are calculated using one way functions, i.e. it will give same output for a particular input but as it is only a one-way function, no matter what you do, you cannot decrypt it. One can try decrypting it by brute force, i.e calculating hashes of words from dictionary and comparing it with the hash you want to decrypt. To save the time of calculating the hashes of dictionary words, there are rainbow tables available online which contains hashes with the words.

散列是使用单向函数计算的,即它会为特定输入提供相同的输出,但由于它只是单向函数,无论您做什么,都无法对其进行解密。可以尝试用蛮力解密它,即从字典中计算单词的哈希值并将其与要解密的哈希值进行比较。为了节省计算字典单词散列的时间,网上有彩虹表,其中包含单词的散列。

read: http://en.wikipedia.org/wiki/Rainbow_table

阅读:http: //en.wikipedia.org/wiki/Rainbow_table

You can also use online services for brute force decryption of a hash. there are plenty available and works well if the word you want to decrypt belongs to a dictionary.

您还可以使用在线服务对哈希进行强力解密。如果您要解密的单词属于字典,则有很多可用并且效果很好。

回答by Thomas Robert Horn

This is a valid question, maybe not posed correctly though.

这是一个有效的问题,但可能没有正确提出。

OP, I think what you're trying to do is check a hashed value against an unhashed one?

OP,我认为您要做的是根据未散列的值检查散列值?

hashed = hashlib.sha256('1234').hexdigest()
hashedstring = '1234' + ',' + hashed

now to check that hashed == original value. So parse out the piece before and after the comma. Hash 1234 and compare it to the value hashed.

现在检查散列 == 原始值。所以解析逗号前后的片段。散列 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

where input h is a string of format "val,(HASHEDSTRING)"

and hash_str is a function that hashes.

其中输入 h 是格式为 "val,(HASHEDSTRING)" 的字符串

hash_str 是一个散列函数。

回答by MikeW

Not-very-exact Analogy: Encryption is like someone wearing a disguise ... taking a Hash is like taking their fingerprints !

不太准确的类比:加密就像有人伪装……获取哈希就像获取他们的指纹!

You can get the "original" person back by removing/reversing the disguise, but you cannot do that from a set of fingerprints !

您可以通过移除/反转伪装来找回“原始”人,但是您不能通过一组指纹来做到这一点!