如何将字符串编码十次,然后解码十次 python

huangapple go评论66阅读模式
英文:

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.

然后,我尝试对一个字符串进行10次编码。它从一个基本字符串开始,比如说"yes",然后使用Base64进行编码,然后再对这个编码进行重复10次。

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.

然后,我想要一个可以对其进行解码的函数,我猜想只是进行10次解码,但我遇到了问题。

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)
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)

It only works 1 loop until I get the error

这段代码只能正常运行1次循环,然后出现错误。

英文:

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

答案1

得分: -2

关于删除 binascii.a2b_base64(s) 的问题,这个代码段将返回 'yes':

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)
英文:

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)

huangapple
  • 本文由 发表于 2023年2月10日 10:31:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定