Python: Decoding base64 encoded strings
It would appear that the final A
is superfluous:
In [4]: base64.b64decode('aHR0cDovLzR1ZnJlZS50ay9tZWRpYTcyMzY0Ni9mdWVuZi8wMzYubXAz')
Out[4]: 'http://4ufree.tk/media723646/fuenf/036.mp3'
Encode and Decode with Base64 and Pickle
Call data.decode()
or the equivalent str(data, encoding='utf-8')
to convert the bytes to a valid base64-encoded string:
# publishJson = json.dumps({"payload": str(data)}) # -
publishJson = json.dumps({"payload": data.decode())}) # +
From https://docs.python.org/3/library/stdtypes.html#str:
Passing a
bytes
object tostr()
without theencoding
orerrors
arguments falls under the first case of returning the informal string representation
print(data) # b'gASVLwAAAAAAAAB9lCiMCHRlc3RLZXkxlF2UKEsBSwJLA2WMCHRlc3RLZXkylF2UKEsESwVLBmV1Lg=='
print(repr(data)) # b'gASVLwAAAAAAAAB9lCiMCHRlc3RLZXkxlF2UKEsBSwJLA2WMCHRlc3RLZXkylF2UKEsESwVLBmV1Lg=='
print(str(data)) # b'gASVLwAAAAAAAAB9lCiMCHRlc3RLZXkxlF2UKEsBSwJLA2WMCHRlc3RLZXkylF2UKEsESwVLBmV1Lg=='
print(repr(str(data))) # "b'gASVLwAAAAAAAAB9lCiMCHRlc3RLZXkxlF2UKEsBSwJLA2WMCHRlc3RLZXkylF2UKEsESwVLBmV1Lg=='"
print(data.decode()) # gASVLwAAAAAAAAB9lCiMCHRlc3RLZXkxlF2UKEsBSwJLA2WMCHRlc3RLZXkylF2UKEsESwVLBmV1Lg==
print(repr(data.decode())) # 'gASVLwAAAAAAAAB9lCiMCHRlc3RLZXkxlF2UKEsBSwJLA2WMCHRlc3RLZXkylF2UKEsESwVLBmV1Lg=='
How to decode base64 in python3
Base64 needs a string with length multiple of 4. If the string is short, it is padded with 1 to 3 =
.
import base64
code = "YWRtaW46MjAyY2I5NjJhYzU5MDc1Yjk2NGIwNzE1MmQyMzRiNzA="
base64.b64decode(code)
# b'admin:202cb962ac59075b964b07152d234b70'
Decoding (Base64 encoded data with type conversion to string by python type casting) to byte array in JAVA
Maybe you should provide a snippet which can be used to reproduce your issue.
Following snippets are working
import base64
f = open("monitor-1.png", "rb")
data = f.read()
encoded_string = str(base64.b64encode(data))
print(encoded_string)
f.close()
output
b'iVBORw0KGgoAAAANSUhEUgAAAAIAAAABAQMAAADO7O3JAAAABlBMVEUAAAD///+l2Z/dAAAACklEQVQI12NoAAAAggCB3UNq9AAAAABJRU5ErkJggg=='
using the Base64 encoded string in Java jshell
jshell> byte[] bytes = Base64.getDecoder().decode("iVBORw0KGgoAAAANSUhEUgAAAAIAAAABAQMAAADO7O3JAAAABlBMVEUAAAD///+l2Z/dAAAACklEQVQI12NoAAAAggCB3UNq9AAAAABJRU5ErkJggg==")
bytes ==> byte[85] { -119, 80, 78, 71, 13, 10, 26, 10, 0, 0 ... 8, 68, -82, 66, 96, -126 }
jshell> Files.write(Paths.get("out.png"), bytes)
$2 ==> out.png
creates the file out.png
The file monitor-1.png
and out.png
are equal
$ md5sum monitor-1.png out.png
49b0cecce3c3ce0966afd6c13b03a4b5 monitor-1.png
49b0cecce3c3ce0966afd6c13b03a4b5 out.png
How to convert from Base64 to string Python 3.2
A string is already 'decoded', thus the str class has no 'decode' function.Thus:
AttributeError: type object 'str' has no attribute 'decode'
If you want to decode a byte array and turn it into a string call:
the_thing.decode(encoding)
If you want to encode a string (turn it into a byte array) call:
the_string.encode(encoding)
In terms of the base 64 stuff:
Using 'base64' as the value for encoding above yields the error:
LookupError: unknown encoding: base64
Open a console and type in the following:
import base64
help(base64)
You will see that base64 has two very handy functions, namely b64decode and b64encode. b64 decode returns a byte array and b64encode requires a bytes array.
To convert a string into it's base64 representation you first need to convert it to bytes. I like utf-8 but use whatever encoding you need...
import base64
def stringToBase64(s):
return base64.b64encode(s.encode('utf-8'))
def base64ToString(b):
return base64.b64decode(b).decode('utf-8')
Find, decode and replace all base64 values in text file
you already have it, you just need the small pieces:
pattern: r'data=([^&]+?)"'
will match anything after data=
and before "
>>> pat = r'data=([^&]+?)"'
>>> line = '<a href="http://blahblah.org/kb/getattachment.php?data=NHxUb3Bjb25fZGF0YS1kb3dubG9hZF9ob3d0by5wZGY=">attached file</a>'
>>> decodeString = re.search(pat,line).group(1) #because the b64 string is capture by grouping, we only want group(1)
>>> decodeString
'NHxUb3Bjb25fZGF0YS1kb3dubG9hZF9ob3d0by5wZGY='
you can then use str.replace()
method as well as base64.b64decode()
method to finish the rest. I dont want to just write your code for you but this should give you a good idea of where to go.
Related Topics
How to Read the Rgb Value of a Given Pixel in Python
Pyaudio Working, But Spits Out Error Messages Each Time
How to Install Psycopg2 with "Pip" on Python
Including Non-Python Files with Setup.Py
Finding Multiple Occurrences of a String Within a String in Python
How to Add Placeholder to an Entry in Tkinter
How to Convert a Datetime to Date
"Cannot Open Include File: 'Config-Win.H': No Such File or Directory" While Installing MySQL-Python
How to Interpret Conda Package Conflicts
Loop That Also Accesses Previous and Next Values
Converting Dict to Ordereddict
Using Logging in Multiple Modules