0

I am using JNDI to connect to a LDAP server. A few attributes on the server are stored as BASE64 string.

However, when I query the server and get results back. These attributes are already decoded but not properly. For example, "abc-def@domain.com" may be decoded as "abcûdef@domain.com".

Any idea on how can I fix this?

Added:

The original BASE64 string is:

Q049XCtHcm91cCBBUFNHLU9uLWJvYXJkaW5n4oCTTllDLE9VPU5ZQyxPV
 20=
CoolBeans
  • 20,654
  • 10
  • 86
  • 101
nababa
  • 1,251
  • 3
  • 13
  • 20

1 Answers1

2

This looks to be a problem between UTF16, which is Java's native character format, and UTF8. The entity that is encoding the string must be UTF8.

To decode a string from UTF8 use:

// to decode a string
String decoded = new String(Base64.decodeBase64(encoded.getBytes()), "UTF8");

That gives me the right output. If you need to convert a UTF8 string to be UTF16 you'd do:

new String(utf8String.getBytes(), "UTF8");
Gray
  • 115,027
  • 24
  • 293
  • 354
  • However, outlook can decode this properly. Also, if you use this link: http://coderstoolbox.net/string/, put in the string, and choose UTF-8, the dash can be decoded properly. – nababa Dec 07 '11 at 19:19
  • 1
    Java's default encoding is UTF-16 not UTF-8. See here: http://stackoverflow.com/a/643713/179850 – Gray Dec 07 '11 at 19:26
  • Here's how to do the conversion from UTF8: http://stackoverflow.com/questions/88838/how-to-convert-strings-to-and-from-utf8-byte-arrays-in-java – Gray Dec 07 '11 at 19:28
  • I've edited my answer with the solution if your input is UTF8 – Gray Dec 07 '11 at 19:33
  • Thanks a lot for the answer. I am a newbie on this, another problem i have now is that from JNDI I get the string "CN=+Group APSG-On-boarding‚ÄìNYC,OU=NYC,O", rather than the original bytes. – nababa Dec 07 '11 at 19:35
  • To convert your string from UTF8 to UTF16 you do: new String(jndiString.getBytes(), "UTF8"); – Gray Dec 07 '11 at 19:38
  • I can't help you anymore, sorry. It is definitely a charset issue. Here are the Java supported character sets: http://docs.oracle.com/javase/1.3/docs/guide/intl/encoding.doc.html – Gray Dec 07 '11 at 20:05