base64 encoded string: can be decoded in PHP but not Java?

neubert

PHP code that works:

$str = "528ABf4cCU5g7zKzLC2t8oze4mstEcWKErar6FbTK2Xo97bu17S8cDmEg5HlbgcLtfvKCVvJ4FnRV3R1iX1TWZIyM7T2352wsb6LUyqFXA03Fz9G6dQmhRrkWpOXAoHeU/H63LKKzcJDhNb3YI2hfsU20BcT0qkk74XKneC7D91OKY=";
echo strlen(base64_decode($str));

That outputs 130.

Java code that doesn't work:

String demo = "528ABf4cCU5g7zKzLC2t8oze4mstEcWKErar6FbTK2Xo97bu17S8cDmEg5HlbgcLtfvKCVvJ4FnRV3R1iX1TWZIyM7T2352wsb6LUyqFXA03Fz9G6dQmhRrkWpOXAoHeU/H63LKKzcJDhNb3YI2hfsU20BcT0qkk74XKneC7D91OKY=";
System.out.println(java.util.Base64.getDecoder().decode(demo).length);

This throws:

"Input byte array has wrong 4-byte ending unit" java.lang.IllegalArgumentException.

How can I make the base64 encoded string that PHP likes decodable by Java?

dnault

That demo input is incorrectly padded; it should end in == instead of =.

You could use a more lenient decoder like Guava's BaseEncoding.

Alternatively, since padding is optional in Base64, you could just strip any trailing = characters before decoding with java.util.Base64.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Some utf-8 strings base64 encoded by php can not be decoded using iOS base64 library?

React-native decoded base64 encoded string

"The string to be decoded is not correctly encoded " error when trying to decode Base64 in JS which has been encoded in Java

Decoded value of base64 string when re-encoded(to base64) gives different output

Compare encoded string to decoded string in PHP?

Why encode string with base64 if it can be easily decoded

Valid Base64 string can't be decoded

Can two different BASE 64 encoded strings result into same string if decoded?

How can I construct a java.security.PublicKey object from a base64 encoded string?

How can JAVA decrypt data which was base64 encoded and RIJNDAEL_256 encryped by PHP?

Can I decode data with base64 in php which was encoded in java?

Return decoded String from base64-encoded ByteArrayOutputStream

Base 64 Encoded QRGen QR code can't be decoded

Can Base64 encoded string end with 3 equal signs?

Can't decode base64 encoded string

How can I unzip a base64 encoded string in R?

POST base64 encoded image string to PHP

php string as byte array, hashed then base64 encoded

How can I sharpen base64 encoded image in php

java mail Base64 encoded string to image attachment

java 8 deserialize base64 encoded string

Difference in encoded and decoded string

Base64 Encoded to Decoded File Conversion Problem

base64 'payload' string from rabbitmq queue can't be decoded properly

converting a base64 decoded string to list in python does not produce same result as in Java array output

Is a base64 encoded string unique?

Flutter Base64 Encoded String Is Incomplete

It is possible to know if a string is encoded in base64?

check the string is Base64 encoded in PowerShell

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive