Remove the new line "n" from base64 encoded strings in Python3?

Question:

I’m trying to make a HTTPS connection in Python3 and when I try to encode my username and password the base64 encodebytes method returns the encoded value with a new line character at the end “n” and because of this I’m getting an error when I try to connect.

Is there a way to tell the base64 library not to append a new line character when encoding or what is the best way to remove this new line character? I tried using the replace method but I get the following error:

Traceback (most recent call last):
  File "data_consumer.py", line 33, in <module>
    auth_base64 = auth_base64.replace('n', '')
TypeError: expected bytes, bytearray or buffer compatible object

My code:

auth = b'[email protected]:passWORD'
auth_base64 = base64.encodebytes(auth)
auth_base64 = auth_base64.replace('n', '')

Any ideas? Thanks

Asked By: Mo.

||

Answers:

Following code would work

auth_base64 = auth_base64.decode('utf-8').replace('n', '')
Answered By: Sarit Adhikari

Instead of encodestring consider using b64encode. Later does not add n characters. e.g.

In [11]: auth = b'[email protected]:passWORD'

In [12]: base64.encodestring(auth)
Out[12]: b'dXNlcm5hbWVAZG9tYWluLmNvbTpwYXNzV09SRA==n'

In [13]: base64.b64encode(auth)
Out[13]: b'dXNlcm5hbWVAZG9tYWluLmNvbTpwYXNzV09SRA=='

It produces identical encoded string except the n

Answered By: Mandar Vaze

I concur with Mandar’s observation that base64.xxxx_encode() would produce output without line wrap n.

For those who want a more confident understanding than merely an observation, these are the official promise (sort of), that I can find on this topic. The Python 3 documentation does mention base64.encode(...) would add newlines after every 76 bytes of output. Comparing to that, all other *_encode(...) functions do not mention their linewrap behavior at all, which can argurably be considered as “no line wrap behavior”. For what it’s worth, the Python 2 documentation does not mention anything about line wrap at all.

Answered By: RayLuo

For Python 3 use:

binascii.b2a_base64(cipher_text, newline=False)

For Python 2 use:

binascii.b2a_base64(cipher_text)[:-1]
Answered By: Harsh

I applied @Harsh hint and these two functions to encode and decode binary data work for my application. My requirement is to be able to use data URIs in HTML src elements and CSS @font-face statements to represent binary objects, specifically images, sounds and fonts. These functions work.

import binascii

def string_from_binary(binary): 
    return binascii.b2a_base64(binary, newline=False).decode('utf-8')

def string_to_binary(string): 
    return binascii.a2b_base64(string.encode('utf-8'))
Answered By: Mark Kortink
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.