Python CGI ValueError:时间数据''不符合格式'%Y%m%d-%H%M%S'

时间:2017-08-10 16:12:57

标签: python apache datetime cgi aes

我的代码包括aesencrypt,aesdecrypt,aespad,aesunpad,cookiegenerate,cookieverify,tokengenerated,tokenverified和expiredtime函数,如下所示:

from Crypto import Random
from Crypto.Cipher import AES
import base64
from datetime import datetime as dtime
from http import cookies as Cookie

# time until token expiration in seconds
ExpirationTime = 3600
# encryption parameters
BlockSize = 16

# aes encryption function
def aesencrypt(data, passphrase):
    iv = Random.new().read(BlockSize)
    cipher = AES.new(passphrase, AES.MODE_CFB, iv)
    encrypted = base64.b64encode(iv+cipher.encrypt(aespad(data)))
    return encrypted
# aes decryption function
def aesdecrypt(encrypted, passphrase):
    decoded = base64.b64decode(encrypted)
    iv = decoded[:BlockSize]
    cipher = AES.new(passphrase, AES.MODE_CFB, iv)
    decrypted = aesunpad(cipher.decrypt(decoded[BlockSize:]))
    return decrypted
# aes pad function
def aespad(data):
    length = 16 - (len(data) % 16)
    data += bytes([length])*length
    return data
# aes unpad function
def aesunpad(data):
    data = data[:-data[-1]]
    return data
# expiration check helper function
def expiredtime(ttime, exptime):
    now = dtime.utcnow()
    then = dtime.strptime(ttime, "%Y%m%d-%H%M%S")
    diff = (now - then).total_seconds()
    if diff > exptime:
        return True
    else:
        return False

# aes token generator
def tokengenerated(thekey):
    return aesencrypt(dtime.utcnow().strftime("%Y%m%d-%H%M%S").encode('utf-8'), thekey)

# aes token verifier
def tokenverified(thetoken, thekey):
    decrypted = aesdecrypt(thetoken, thekey)
    if expiredtime(decrypted.decode('utf-8'), ExpirationTime):
        return False
    else:
        return True

# cookie generation function
def cookiegenerate(thekey, subid):
    cookie = Cookie.SimpleCookie()
    cookie['session'] = tokengenerated(thekey)
    cookie["session"]['domain'] = "MYSERVER"
    cookie["session"]['path'] = '/'
    cookie['session']['expires'] = 'expires in %s seconds' % (ExpirationTime)
    cookie['user_id'] = aesencrypt(str(subid).encode('utf-8'), thekey)
    return cookie.output()

# cookie verification function
def cookieverify(acookie, thekey):
    return tokenverified(acookie['session'].value, thekey)

当我使用这些功能的代码点击时,如果cookieverify(cookie,akey):'它返回以下错误:

  

[Thu Aug 10 14:40:35.099480 2017] [cgi:error] [pid 9774] [客户   ME:51769] AH01215:File" /var/www/html/cgi-bin/authdefs.py" ;, line   63,在tokenverified中:/var/www/html/cgi-bin/newsite.py,referer:   MYSERVER [Thu Aug 10 14:40:35.099493 2017] [cgi:error] [pid 9774]   [客户ME:51769] AH01215:如果   expiredtime(decrypted.decode(' utf-8'),ExpirationTime)::   /var/www/html/cgi-bin/newsite.py,referer:MYSERVER [8月10日星期四   14:40:35.099518 2017] [cgi:错误] [pid 9774] [客户ME:51769]   AH01215:文件" /var/www/html/cgi-bin/authdefs.py" ;,第49行,在   expiredtime:/var/www/html/cgi-bin/newsite.py,referer:MYSERVER [Thu   8月10日14:40:35.099528 2017] [cgi:错误] [pid 9774] [客户ME:51769]   AH01215:则= dtime.strptime(ttime,"%Y%m%d-%H%M%S"):   /var/www/html/cgi-bin/newsite.py,referer:MYSERVER [8月10日星期四   14:40:35.099545 2017] [cgi:错误] [pid 9774] [客户ME:51769]   AH01215:文件" /usr/lib/python3.5/_strptime.py" ;,第510行,在   _strptime_datetime:/var/www/html/cgi-bin/newsite.py,referer:MYSERVER [Thu Aug 10 14:40:35.099560 2017] [cgi:error] [pid 9774]   [客户ME:51769] AH01215:tt,fraction = _strptime(data_string,   格式):/ var / www / html / cgi-bin / newsite.py,referer:MYSERVER [8月8日   10 14:40:35.099573 2017] [cgi:错误] [pid 9774] [客户ME:51769]   AH01215:文件" /usr/lib/python3.5/_strptime.py" ;,第343行,在   _strptime:/var/www/html/cgi-bin/newsite.py,referer:MYSERVER [Thu Aug 10 14:40:35.099579 2017] [cgi:error] [pid 9774] [client ME:51769]   AH01215:(data_string,format)):/ var / www / html / cgi-bin / newsite.py,   referer:MYSERVER [Thu Aug 10 14:40:35.099591 2017] [cgi:error] [pid   9774] [客户端ME:51769] AH01215:ValueError:时间数据''才不是   匹配格式'%Y%m%d-%H%M%S':/var/www/html/cgi-bin/newsite.py,   referer:MYSERVER [Thu Aug 10 14:40:35.116910 2017] [cgi:error] [pid   9774] [客户端ME:51769]标题之前的脚本输出结束:   newsite.py,referer:MYSERVER

我可以验证我是否存储了一个cookie,并且可以使用一个简短的测试脚本检索它,以及我的加密和解密函数正在按预期工作,但由于某种原因,我回来的日期时间字符串解密后从客户端的cookie中给出了一个ValueError。

任何人都知道发生了什么?我做错了什么?

编辑:我添加了一些行来将cookie [session] .value保存到文件中并得到:" NameError:name' session'未定义:/var/www/html/cgi-bin/newsite.py,referer:MYSERVER"尽管在上一页上运行cookiegenerate()并通过EditThisCookie查看cookie(值为" b' leP3KVY5tBI9HlwUjWvouNBGasECueng / 1T7fu5TPRw ='")和cookie = Cookie.SimpleCookie(os.environ [ " HTTP_COOKIE"])在给我提问的脚本中。

0 个答案:

没有答案