Ravi A
Ravi A

Reputation: 525

base64 encoding issue, java

I'm using apache library for encoding to base64. But this time problem is very typical. I've a b64 encoded string.

MIIHSjCCBjKgAwIBAgIQQuw1emUfNRlPD/euDuzBjDANBgkqhkiG9w0BAQUFADCB"+
"5TELMAkGA1UEBhMCRVMxIDAeBgkqhkiG9w0BCQEWEWFjQGFjYWJvZ2FjaWEub3Jn

Its the part of certificate (.CER) file. I am just decoding it and again encoding it but result is little bit different. Resultant string is,

"MIIHSjCCBjKgAwIBAgIQQuw1emUfNRlPD/euDuzBjDANBgkqhkiG9w0BAQUFADA"+ "/5TELMAkGA1UEBhMCRVMxIDAeBgkqhkiG9w0BCQEWEWFjQGFjYWJvZ2FjaWEub3Jn"

The difference is at the end of the first line and starting of the second line. CB are replaced by A/.

This change invalidates my certificate. Where the problem can be ?

Upvotes: 2

Views: 3553

Answers (1)

zvez
zvez

Reputation: 818

The problem is in your intermediate string conversion. If you use only byte array, everything is fine.

public static void main(String args[]) {
        String partOfCer = "MIIHSjCCBjKgAwIBAgIQQuw1emUfNRlPD/euDuzBjDANBgkqhkiG9w0BAQUFADCB" + "5TELMAkGA1UEBhMCRVMxIDAeBgkqhkiG9w0BCQEWEWFjQGFjYWJvZ2FjaWEub3Jn";
        byte[] dec1_byte = Base64.decodeBase64(partOfCer.getBytes());
//        String dec1 = new String(dec1_byte);
        byte[] newBytes = Base64.encodeBase64(dec1_byte);
        String newStr = new String(newBytes);
        System.out.println(partOfCer);
        System.out.println(newStr);
        System.out.println(partOfCer.equals(newStr));
    }

Upvotes: 5

Related Questions