英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论