Search code examples
pythonbase64decodeencode

How to encode a string 10 times then decode it ten times python


I'm trying to encode a string 10 times. It starts with a base string let's say "yes" and encode that with base64 then it will encode that encode again on repeat for 10 loops.

Then I'm wanting a function that will decode that which I'm guessing is just decoding 10 times which I'm having a problem with.

def de(string):
    t = string
    for v in range(0, 10):
        f = t.encode("ascii")
        g = base64.b64encode(f)
        t = g.decode('utf-8')
    return t

def decode(string):
    for v in range(0, 10):
        g = base64.b64decode(string)
        string = g.decode('utf-8')
        print(string)
return binascii.a2b_base64(s)

binascii.Error: Incorrect padding

It only works 1 loop until I get the error


Solution

  • What about removing binascii.a2b_base64(s)? This returns 'yes' for me:

    import base64
    def de(string):
        t = string
        for v in range(10):
            f = t.encode("ascii")
            g = base64.b64encode(f)
            t = g.decode('utf-8')
        return t
    
    def decode(string):
        for v in range(10):
            g = base64.b64decode(string)
            string = g.decode('utf-8')
        return string
    
    s = de("yes")
    decode(s)