Backend: Decode invalid base64

Check for invalid input before decoding.
Also remove any whitespaces which might be present in
the base64 data.