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.