<tfoot id='MTveI'></tfoot>

  • <legend id='MTveI'><style id='MTveI'><dir id='MTveI'><q id='MTveI'></q></dir></style></legend>
  • <small id='MTveI'></small><noframes id='MTveI'>

      <bdo id='MTveI'></bdo><ul id='MTveI'></ul>

      1. <i id='MTveI'><tr id='MTveI'><dt id='MTveI'><q id='MTveI'><span id='MTveI'><b id='MTveI'><form id='MTveI'><ins id='MTveI'></ins><ul id='MTveI'></ul><sub id='MTveI'></sub></form><legend id='MTveI'></legend><bdo id='MTveI'><pre id='MTveI'><center id='MTveI'></center></pre></bdo></b><th id='MTveI'></th></span></q></dt></tr></i><div id='MTveI'><tfoot id='MTveI'></tfoot><dl id='MTveI'><fieldset id='MTveI'></fieldset></dl></div>

        Python:base64解码时忽略“不正确的填充"错误

        Python: Ignore #39;Incorrect padding#39; error when base64 decoding(Python:base64解码时忽略“不正确的填充错误)
        • <small id='peEnj'></small><noframes id='peEnj'>

            <bdo id='peEnj'></bdo><ul id='peEnj'></ul>

            <legend id='peEnj'><style id='peEnj'><dir id='peEnj'><q id='peEnj'></q></dir></style></legend>
            • <tfoot id='peEnj'></tfoot>
              <i id='peEnj'><tr id='peEnj'><dt id='peEnj'><q id='peEnj'><span id='peEnj'><b id='peEnj'><form id='peEnj'><ins id='peEnj'></ins><ul id='peEnj'></ul><sub id='peEnj'></sub></form><legend id='peEnj'></legend><bdo id='peEnj'><pre id='peEnj'><center id='peEnj'></center></pre></bdo></b><th id='peEnj'></th></span></q></dt></tr></i><div id='peEnj'><tfoot id='peEnj'></tfoot><dl id='peEnj'><fieldset id='peEnj'></fieldset></dl></div>

                  <tbody id='peEnj'></tbody>
                  本文介绍了Python:base64解码时忽略“不正确的填充"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一些经过 base64 编码的数据,即使其中存在填充错误,我也想将其转换回二进制.如果我使用

                  I have some data that is base64 encoded that I want to convert back to binary even if there is a padding error in it. If I use

                  base64.decodestring(b64_string)
                  

                  它会引发不正确的填充"错误.还有其他方法吗?

                  it raises an 'Incorrect padding' error. Is there another way?

                  更新:感谢所有反馈.老实说,所有提到的方法听起来都有点打击错过了所以我决定尝试openssl.以下命令很有效:

                  UPDATE: Thanks for all the feedback. To be honest, all the methods mentioned sounded a bit hit and miss so I decided to try openssl. The following command worked a treat:

                  openssl enc -d -base64 -in b64string -out binary_data
                  

                  推荐答案

                  正如其他回复中所说,base64 数据可能被破坏的方式有很多种.

                  As said in other responses, there are various ways in which base64 data could be corrupted.

                  但是,正如 Wikipedia 所说,删除填充('=' 字符在base64 编码数据的结尾)是无损"的:

                  However, as Wikipedia says, removing the padding (the '=' characters at the end of base64 encoded data) is "lossless":

                  从理论上讲,填充字符是不需要的,因为丢失的字节数可以从数字中计算出来Base64 位数.

                  From a theoretical point of view, the padding character is not needed, since the number of missing bytes can be calculated from the number of Base64 digits.

                  因此,如果这确实是您的 base64 数据唯一错误"的地方,则可以重新添加填充.我想出了这个能够解析 WeasyPrint 中的数据"URL,其中一些是 base64 没有填充:

                  So if this is really the only thing "wrong" with your base64 data, the padding can just be added back. I came up with this to be able to parse "data" URLs in WeasyPrint, some of which were base64 without padding:

                  import base64
                  import re
                  
                  def decode_base64(data, altchars=b'+/'):
                      """Decode base64, padding being optional.
                  
                      :param data: Base64 data as an ASCII byte string
                      :returns: The decoded byte string.
                  
                      """
                      data = re.sub(rb'[^a-zA-Z0-9%s]+' % altchars, b'', data)  # normalize
                      missing_padding = len(data) % 4
                      if missing_padding:
                          data += b'='* (4 - missing_padding)
                      return base64.b64decode(data, altchars)
                  

                  此函数的测试:weasyprint/tests/test_css.py#L68

                  这篇关于Python:base64解码时忽略“不正确的填充"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                    • <small id='pVkKK'></small><noframes id='pVkKK'>

                      <legend id='pVkKK'><style id='pVkKK'><dir id='pVkKK'><q id='pVkKK'></q></dir></style></legend>
                    • <tfoot id='pVkKK'></tfoot>

                        <i id='pVkKK'><tr id='pVkKK'><dt id='pVkKK'><q id='pVkKK'><span id='pVkKK'><b id='pVkKK'><form id='pVkKK'><ins id='pVkKK'></ins><ul id='pVkKK'></ul><sub id='pVkKK'></sub></form><legend id='pVkKK'></legend><bdo id='pVkKK'><pre id='pVkKK'><center id='pVkKK'></center></pre></bdo></b><th id='pVkKK'></th></span></q></dt></tr></i><div id='pVkKK'><tfoot id='pVkKK'></tfoot><dl id='pVkKK'><fieldset id='pVkKK'></fieldset></dl></div>
                            <bdo id='pVkKK'></bdo><ul id='pVkKK'></ul>
                              <tbody id='pVkKK'></tbody>