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

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

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次解码,但我遇到了问题。

  1. def de(string):
  2. t = string
  3. for v in range(0, 10):
  4. f = t.encode("ascii")
  5. g = base64.b64encode(f)
  6. t = g.decode('utf-8')
  7. return t
  8. def decode(string):
  9. for v in range(0, 10):
  10. g = base64.b64decode(string)
  11. string = g.decode('utf-8')
  12. print(string)
  1. def de(string):
  2. t = string
  3. for v in range(0, 10):
  4. f = t.encode('ascii')
  5. g = base64.b64encode(f)
  6. t = g.decode('utf-8')
  7. return t
  8. def decode(string):
  9. for v in range(0, 10):
  10. g = base64.b64decode(string)
  11. string = g.decode('utf-8')
  12. 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.

  1. def de(string):
  2. t = string
  3. for v in range(0, 10):
  4. f = t.encode("ascii")
  5. g = base64.b64encode(f)
  6. t = g.decode('utf-8')
  7. return t
  8. def decode(string):
  9. for v in range(0, 10):
  10. g = base64.b64decode(string)
  11. string = g.decode('utf-8')
  12. print(string)
  1. 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':

  1. import base64
  2. def de(string):
  3. t = string
  4. for v in range(10):
  5. f = t.encode("ascii")
  6. g = base64.b64encode(f)
  7. t = g.decode('utf-8')
  8. return t
  9. def decode(string):
  10. for v in range(10):
  11. g = base64.b64decode(string)
  12. string = g.decode('utf-8')
  13. return string
  14. s = de("yes")
  15. decode(s)
英文:

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

  1. import base64
  2. def de(string):
  3. t = string
  4. for v in range(10):
  5. f = t.encode("ascii")
  6. g = base64.b64encode(f)
  7. t = g.decode('utf-8')
  8. return t
  9. def decode(string):
  10. for v in range(10):
  11. g = base64.b64decode(string)
  12. string = g.decode('utf-8')
  13. return string
  14. s = de("yes")
  15. 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:

确定