0

I'm trying to encode a Saml 2.0 request in Java but when i decode it using tools online i get weird characters.

This is the input string:

<samlp:AuthnRequest 
xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" 
ID="_8d7bd828-6f91-477a-b158-22d693f56972" 
Version="2.0" 
IssueInstant="2013-04-19T14:07:53Z" 
AssertionConsumerServiceURL="http://test">
<saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
test
</saml:Issuer>
<samlp:NameIDPolicy 
Format="urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified" 
AllowCreate="true" />
</samlp:AuthnRequest>

Here's the code I'm using:

byte[] xmlBytes = baos.toByteArray();

//Deflate
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(
                                                byteOutputStream);
try {
    deflaterOutputStream.write(xmlBytes, 0, xmlBytes.length);
    deflaterOutputStream.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

//BASE64 encode
Base64 base64Encoder = new Base64();
byte[] base64EncodedByteArray = base64Encoder.encode(xmlBytes);
String base64EncodedMessage = new String(base64EncodedByteArray);

// URL encode
String urlEncodedMessage = null;
try {
    urlEncodedMessage = URLEncoder.encode(base64EncodedMessage,"utf-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}
return urlEncodedMessage;

When i run the resulting string through the online decoding tools i get the following result:

x?}?Mk?@??J????ILL@??`?0??^"??B???Gm?}W?`/^?03????,,­????
?=Z}??pT??[iA?-8??q
?I
??N
?h???GYN?\pZ?"?Y:??Pt9mS??<-D?$zCc?V                 
Ah?A??+Zq??8??|?d??$ZZ???J+?4
?/)?u????Kf?d>9??2.cf?-X?<?&??zq?K??A?O???ImZ??`76W???n?~???6
w?m?????
^???$????ie?;??3-I??k??7????Srtion">
test
</saml:Issuer>
<samlp:NameIDPolicy 
Format="urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified" 
AllowCreate="true" />
</samlp:AuthnRequest>
mavis
  • 3,100
  • 3
  • 24
  • 32
Sporki
  • 103
  • 1
  • 2
  • 5

1 Answers1

1

You need to use URL decoder first and then Base64 decoder, is that what you are doing?

performanceuser
  • 2,793
  • 5
  • 34
  • 44
  • Could you please provide sample code to URL Decoder first and then Base64 decoder? Please reply early.. –  Apr 10 '15 at 12:23