Search code examples
pythonpython-3.xbase64decodeencode

Extra new lines on base64 encode/decode in Python?


I have written these utility functions:

import base64

def der2str(der):
    return bin2str( base64.encodebytes(der) )

def str2der(str_):
    return base64.b64decode( str2bin(str_) )

def bin2str(binary):
    return binary.decode('utf8')

def str2bin(str_):
    return str_.encode('utf8')

The I run:

if __name__ == '__main__':
    test = 'MIIEowIBAAKCAQEA6cVU+6GZyr1jaxvJcLEdRb9cicL/4Soe/HqN+gE/UdM5C71aG6HhNSplj1qi\nX8Abffen'
    print(test)
    print(der2str(str2der(test)))

but the output is:

MIIEowIBAAKCAQEA6cVU+6GZyr1jaxvJcLEdRb9cicL/4Soe/HqN+gE/UdM5C71aG6HhNSplj1qi
X8Abffen
MIIEowIBAAKCAQEA6cVU+6GZyr1jaxvJcLEdRb9cicL/4Soe/HqN+gE/UdM5C71aG6HhNSplj1qi
X8Abffen


Why am I getting these extra two new lines on the second print ?

[EDIT]
According to marked answer, using return bin2str( base64.b64encode(der) ), works fine as long as the input string test does not contain any '\n'.

If someone needs the newlines then the string must end on '\n' for the assertion assert(test == der2str(str2der(test))) to pass.


Solution

  • The docs for base64.encodebytes(s) state that it inserts newlines

    Encode the bytes-like object s, which can contain arbitrary binary data, and return bytes containing the base64-encoded data, with newlines (b'\n') inserted after every 76 bytes of output, and ensuring that there is a trailing newline, as per RFC 2045 (MIME).

    You may want to use base64.b64encode instead.