Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试传递值时出现切片指示错误_Python - Fatal编程技术网

Python 尝试传递值时出现切片指示错误

Python 尝试传递值时出现切片指示错误,python,Python,这是一个fiestel密码,我正在尝试解码存储的消息,但是当我运行它时,我得到一个未处理的异常“切片索引必须是整数或无,或具有索引方法”。我把它追溯到(L,R)=G(test),但我不确定传入的内容有什么问题 from base64 import b64decode (a,b,c,d) = (122584878038618531937141401903692682612022052258468698452458861145036781853705766570015376117590674048

这是一个fiestel密码,我正在尝试解码存储的消息,但是当我运行它时,我得到一个未处理的异常“切片索引必须是整数或无,或具有索引方法”。我把它追溯到(L,R)=G(test),但我不确定传入的内容有什么问题

from base64 import b64decode

(a,b,c,d) = (12258487803861853193714140190369268261202205225846869845245886114503678185370576657001537611759067404857011052836681912514135296185688334428452462064527761,\
         13346376869506979374836874046204370339910872071884888902215442517395171258258152757258082136597260296613932276350407476889729430724339742164666125768654387,\
         10707278048073703090234519728300006549070759592219984101161107932419267899872157175740069946671710753166980969556028191258252903890793549897483872362043507,\
         11330448751098045546614990567612096132290751751225546822371413732253437433478002901406535282761940023232557605690376224913895910240841519464567354770276551)

def F(x):
    return (a*x*x + b*x + c)%d

def G(message):
    n = len(message)
    L = message[0:(n/2)]
    R = message[(n/2):n]

    L = int(L.encode("hex"), 16)
    R = int(R.encode("hex"), 16)

    return (L,R)

def fiestel(L, R):
    rounds = 8
    for i in xrange(rounds):
        (L,R) = (R, L^F(R))

    L = hex(L).replace("0x", "").replace("L", "")
    R = hex(R).replace("0x", "").replace("L", "")

    return R+L


test = "1SvXEaXhywrBE6DRX9zomKxKbZGYu46Tj7Z+oNrX0SxGU253OmLKDLHoO+LaJT2W+lPyQkWBToiPbo7wNz2lSIrTRT8yxV6AovUQO3Hvob33/hVfYmpHiytVwQ/dPmx+IQi7w+rTYZGro58FauonXu4hjwCnRaVYhwdjAvbC7cA="
test = b64decode(test) 

(L,R) = G(test)
next_test = fiestel(L,R)
FLAG = next_test.decode("hex")

print(FLAG)


#1SvXEaXhywrBE6DRX9zomKxKbZGYu46Tj7Z+oNrX0SxGU253OmLKDLHoO+LaJT2W+lPyQkWBToiPbo7wNz2lSIrTRT8yxV6AovUQO3Hvob33/hVfYmpHiytVwQ/dPmx+IQi7w+rTYZGro58FauonXu4hjwCnRaVYhwdjAvbC7cA=

n
是奇数时,
n/2
显然是浮点数。在您的例子中,
len(n)=128
,因此这不是问题所在,而是需要注意的问题

您使用的是返回浮点数的简单除法
/
。使用整数除法
/
,它将返回整数

print(128/2)
#64.0
print(128//2)
#64

这至少可以让您进入下一行,在那里您要将
L.encode(“hex”)
更改为
L.hex()
。如果您使用的是python3.x,那么
xrange()
应该是
range()

您可以发布错误的完整回溯吗?这是有效的,但是现在我用这一行L=int(L.encode(“hex”),16处理这个异常,消息说“hex”不是文本编码;使用codecks.encode()处理任意编解码器,但它应该能够接受