Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 312
Filomena Avatar asked Jun 17 '26 20:06

Filomena


1 Answers

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.

like image 93
snakecharmerb Avatar answered Jun 20 '26 09:06

snakecharmerb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!