当我尝试传递值时切片索引错误

时间:2019-05-02 19:40:38

标签: python

这是一个固定的密码,我正在尝试对存储的消息进行解码,但是当我运行它时,出现了未处理的异常“切片索引必须为整数或None或具有 index 方法”。我追踪到(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=

1 个答案:

答案 0 :(得分:1)

n是一个奇数时,n/2显然是一个浮点数。对于您来说,len(n) = 128并不是问题,而是需要注意的问题。

您正在使用简单除法/,该除法返回浮点数。使用整数除法//将返回一个整数。

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

这至少会引导您进入要将L.encode("hex")更改为L.hex()的下一行。如果您使用的是python3.x,则xrange()应该是range()