使用python ||解码字符串SHA256

问题描述 投票:0回答:1

我目前在python中使用hashlib库使用SHA256加密URL。以下是代码。

import hashlib
url='https://booking.com'
hs = hashlib.sha256(url.encode('utf-8')).hexdigest()
print(hs) # 037c89f2570ac1cff92d67643f570bec93ebea7f0222e105616590a9673be21f

现在,我想解密并获取URL。有人可以告诉我该怎么做吗?

python-3.x md5 sha256 hashlib
1个回答
0
投票

您不能使用哈希来完成此操作

您应该使用密码,例如AES Cipher


示例:

from Crypto.Cipher import AES


def resize_length(string):
    #resizes the String to a size divisible by 16 (needed for this Cipher)
    return string.rjust((len(string) // 16 + 1) * 16)

def encrypt(url, cipher):
    # Converts the string to bytes and encodes them with your Cipher
    return cipher.encrypt(resize_length(url).encode())

def decrypt(text, cipher):
    # Converts the string to bytes and decodes them with your Cipher
    return cipher.decrypt(text).decode().lstrip()


# It is important to use 2 ciphers with the same information, else the system breaks (at least for me)
# Define the Cipher with your data (Encryption Key and IV)
cipher1 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
cipher2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
decrypt(encrypt("https://booking.com", cipher1), cipher2)

此充满回报的https://booking.com

© www.soinside.com 2019 - 2024. All rights reserved.