Could not find a way to decode a RFC2045 format base64 string
I am trying to decode a RFC2045 format base64 string inn Python 3 but just could not find a way to achieve the same result as org.apache.commons.codec.binary.Base64.decodeBase64
.
Here is the Java code:
import static org.apache.commons.codec.binary.Base64.decodeBase64;
String str1 = "j-FH-F9__CIiwyg0o3A2mKflRBnxZSMwktDJQyvRevc";
byte[] b1 = decodeBase64(str1);
System.out.println(b1.length + " " + b1);
And the Python 3 code:
import base64
from email import base64mime
def bytes2list(bdata):
return [b if b < 128 else b - 256 for b in bdata]
b64str = 'j-FH-F9__CIiwyg0o3A2mKflRBnxZSMwktDJQyvRevc'
b64str += "=" * ((4 - len(b64str) % 4) % 4)
b1 = base64.b64decode(b64str)
b2 = base64mime.decode(b64str)
print(len(b1), bytes2list(b1))
print(len(b2), bytes2list(b2))
The Java program output: 32 [-113, -31, 71, -8, 95, 127, -4, 34, 34, -61, 40, 52, -93, 112, 54, -104, -89, -27, 68, 25, -15, 101, 35, 48, -110, -48, -55, 67, 43, -47, 122, -9]
Python output: 29 [-116, 81, -59, -12, 34, 34, -61, 40, 52, -93, 112, 54, -104, -89, -27, 68, 25, -15, 101, 35, 48, -110, -48, -55, 67, 43, -47, 122, -9]
for both base64.b64decode
and base64mime.b64decode
I would expect this is not a really rare situation but just could not find a way to get it right. Any hints?