0

This is a fiestel cipher and I am trying to decode the stored message, however when I run it i get an exception unhandled "slice indices must be integers or None or have an index method". I traced it to (L,R) = G(test), but I am not sure what is wrong with what is being passed in

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=
Cohan
  • 4,384
  • 2
  • 22
  • 40
zavier
  • 25
  • 3

1 Answers1

1

When n is an odd number, n/2 is obviously a float. In your case, len(n) = 128, so that's not the issue, but something to be aware of.

You are using simple division / which returns a float. Use integer division // which will return an int.

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

This will at least get you to through to the next line where you want to change L.encode("hex") into L.hex(). Then xrange() should be range() if you're using python3.x.

Cohan
  • 4,384
  • 2
  • 22
  • 40
  • That worked however now I get this exception unhandled with this line L = int(L.encode("hex"), 16), the message says 'hex' is not a text encoding; use codecs.encode() to handle arbitrary codecs, but it should be able to take it in – zavier May 02 '19 at 19:57
  • 1
    https://stackoverflow.com/questions/6624453/whats-the-correct-way-to-convert-bytes-to-a-hex-string-in-python-3 – Cohan May 02 '19 at 19:58