VG P
VG P

Reputation: 333

Base64 UTF-32 decoding in java not working as expected

I have Base64 UTF-32 encoded string, while decoding it comes with white spaces. I am using org.apache.commons.codec library.

For encoding below code is used and working fine as expected

public static String encodeBase64(String encodeString,String utfType){
        try {
            return new String(Base64.encodeBase64String(encodeString.getBytes(utfType)));
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
        System.out.println(encodeBase64("This is UTF-32 encoading test","UTF-32"));

this gives me Base64 encoded string as

AAAAVAAAAGgAAABpAAAAcwAAACAAAABpAAAAcwAAACAAAABVAAAAVAAAAEYAAAAtAAAAMwAAADIAAAAgAAAAZQAAAG4AAABjAAAAbwAAAGEAAABkAAAAaQAAAG4AAABnAAAAIAAAAHQAAABlAAAAcwAAAHQ=

I want to decode above string

    public static String decodeBase64(String decodeString,String utfType){
        try {
            byte[] actualByte = java.util.Base64.getDecoder() .decode(decodeString);
             return new String(actualByte);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

        System.out.println(decodeBase64("AAAAVAAAAGgAAABpAAAAcwAAACAAAABpAAAAcwAAACAAAABVAAAAVAAAAEYAAAAtAAAAMwAAADIAAAAgAAAAZQAAAG4AAABjAAAAbwAAAGEAAABkAAAAaQAAAG4AAABnAAAAIAAAAHQAAABlAAAAcwAAAHQ=","UTF-32"));

Output received as below, which not correct

T   h   i   s       i   s       U   T   F   -   3   2        e   n   c   o   a 
  d   i   n   g        t   e   s   t

How to get it as original string after decoding like below value This is UTF-32 encoding test","UTF-32

Upvotes: 0

Views: 633

Answers (1)

Joni
Joni

Reputation: 111239

You forgot to pass the character encoding to the String constructor, so it's creating the string using the platform default character encoding. Use:

return new String(actualByte, utfType);

Upvotes: 2

Related Questions