在Python中生成临时url

2024-04-27 21:24:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试在python中生成一个临时url,url将包含一些我需要确保不被更改的数据,因此我将在最后添加一个散列,但是不管我做了什么,我总是以bString结尾,有人能指出我做错了什么吗? 这是我的代码示例 哦,我知道也许改变算法/编码也许能解决问题,但我找不到合适的,任何一个投反对票的人能解释他为什么投反对票吗

import hashlib
import datetime
from Crypto.Cipher import AES

def checkTemp(tempLink):
    encrypter = AES.new('1234567890123456', AES.MODE_CBC, 'this is an iv456')
    decryption = encrypter.decrypt(tempLink)
    length = len(decryption)

    hash_code = decryption[length-32:length]
    data= decryption[:length-32].strip()
    hasher = hashlib.sha256()
    hasher.update(data)
    hashCode = hasher.digest()

    if(hash_code==hashCode):
        array = data.decode().split(",",5)
        print("expiry date is :"+ str(array[5]))
        return array[0],array[1],array[2],array[3],array[4]
    else:
        return "","","","",""

def createTemp(inviter,email,role,pj_name,cmp_name):
    delim = ','
    data = inviter+delim+email+delim+role+delim+pj_name+delim+cmp_name+delim+str(datetime.datetime.now().time())
    data = data.encode(encoding='utf_8', errors='strict')

    hasher = hashlib.sha256()
    hasher.update(data)

    hashCode = hasher.digest()
    encrypter = AES.new('1234567890123456', AES.MODE_CBC, 'this is an iv456')
    # to make the link a multiple of 16 by adding for AES with the addition of spaces
    newData = data+b' '*(len(data)%16)
    result = encrypter.encrypt(newData+hashCode)

    return result
#print(str(link).split(",",5))
link = createTemp("name","email@homail.com","Designer","Project Name","My Company")
print(link)
inviter,email,role,project,company = checkTemp(link)

Tags: nameimportdatadatetimeemaillinkarraylength
1条回答
网友
1楼 · 发布于 2024-04-27 21:24:42

问题是无法输出正常的字符串,因为加密将导致几乎不可能编码的字符,因此解决方案是使用binascii为我们编码bstring并对其进行解码

import binascii

然后我们对链接的可用字符串进行编码

^{pr2}$

在方法中使用它之前,我们先解除它

inviter,email,role,project,company = checkTemp(binascii.unhexlify(hexedLink))

相关问题 更多 >