I know how to encrypt:
encrypted = hashlib.sha256('1234').hexdigest()
But I am not sure, how to decrypt this?
decrypted = decrypt(encrypted)
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).
Note http://en.wikipedia.org/wiki/Cryptographic_hash_function:
The ideal cryptographic hash function has four main properties:
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
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:
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With