简体   繁体   English

Python中的base64解码问题

[英]issues with base64 decoding in Python

Hello and thanks for reading. 您好,感谢您的阅读。 I am running into some issues decoding previously encoded files using base64. 我在使用base64解码以前编码的文件时遇到了一些问题。 For example, suppose I want to encode a pdf file using base64. 例如,假设我要使用base64编码pdf文件。 The result is a nice 80 char delimited series of strings. 结果是一个很好的以80个字符分隔的字符串。 The code that does the encoding (cribbed from this board) is nice and easy: 进行编码的代码(从此板卡复制)非常简单:

    def encode_file_base64(bin_input):
      flag = 0
      try:
        with open(bin_input, 'rb') as fin, open('tmp.bin_hex', 'w') as fout:
        base64.encode(fin, fout)
      except:
        traceback.print_exc()
        flag = -1
      return flag 

Now the decoding function: 现在解码功能:

    def decode_file_base64(bin_output):
      flag = 0
      try:
        with open('tmp.bin_hex', 'rb') as fin, open(bin_output, 'w') as fout:
          base64.decode(fin, fout)
      except:
          traceback.print_exc()
          flag = -1
      return flag

It does the job, but when I try to open the output file, I am not able to and the file appears to be 'corrupt'. 它可以完成工作,但是当我尝试打开输出文件时,我无法打开文件,并且文件似乎已损坏。 I have been struggling with this more than a fair amount and I'm about to give up. 我一直在为此付出不菲的努力,我将放弃。 I suppose I could use other types of encodings but the BOSS insists on base64 (he must have heard that it's the best...). 我想我可以使用其他类型的编码,但是BOSS坚持使用base64(他一定听说过这是最好的……)。

I don't know if this is your problem (I don't even know what your problem is), but if you're on a platform/version/implementation where binary-mode makes a difference, you're doing it wrong: 我不知道这是否是您的问题(我什至都​​不知道您的问题是什么),但是如果您使用的是二进制模式有所作为的平台/版本/实现,那么您做错了:

with open('tmp.bin_hex', 'rb') as fin, open(bin_output, 'w') as fout:

You're opening the text file (the b64 file you wrote in text mode) in binary mode, and the binary file in text mode. 您将以二进制模式打开文本文件(以文本模式编写的b64文件),并以文本模式打开二进制文件。 Try this: 尝试这个:

with open('tmp.bin_hex', 'r') as fin, open(bin_output, 'wb') as fout:

Meanwhile, for debugging purposes, you might want to try comparing a file to the result of encoding and decoding it. 同时,出于调试目的,您可能希望尝试将文件与编码和解码结果进行比较。 If, for example, you see that the new file is a little longer, and a hexdump shows that this is because every 0x0A byte has been replaced by two 0x0D 0x0A bytes, you know that the problem is that you're translating newlines, which in turn means that you're in text mode. 例如,如果您看到新文件更长,并且十六进制转储表明这是因为每个0x0A字节已被两个0x0D 0x0A字节替换,则您知道问题在于您正在翻译换行符,依次表示您处于文本模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM