I need to encode a short String as base 64 in GWT and decode the base 64 string on the server. Anyone have utility class or library for this?
Asked
Active
Viewed 2.0k times
15
-
1possible duplicate of [Decode Base64 data in java](http://stackoverflow.com/questions/469695/decode-base64-data-in-java) – Johan Sep 09 '11 at 18:44
-
18People should start noticing that a GWT question probably has a different context from Java SE and is not a duplicate of a Java SE question. What runs on Java SE requires additional treatment to be placed on GWT. – Blessed Geek Oct 21 '11 at 13:47
4 Answers
15
You can use native JavaScript for this on the client on all browsers except IE ≤ 9. On the server you can use one of the official classes.
Java/GWT:
private static native String b64decode(String a) /*-{
return window.atob(a);
}-*/;
Encode is btoa
.

Janus Troelsen
- 20,267
- 14
- 135
- 196
7
You can use the BaseEncoding class provided by Guava.
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/BaseEncoding.html
For example:
try {
String encoded = BaseEncoding.base64().encode("foo".getBytes("UTF-8"))
} catch (UnsupportedEncodingException e) {
GWT.log(e.getMessage());
}
And don't forget to add the following line to your GWT module XML:
<inherits name="com.google.common.io.Io"/>
The BaseEncoding class can be used on both the GWT client side and server side.

Jake W
- 2,788
- 34
- 39
3
You can have a look at https://github.com/mooreds/gwt-crypto
It provides base64 encoding to GWT.
Base64.encode(string.getBytes());
Add the import below :
import com.googlecode.gwt.crypto.bouncycastle.util.encoders.Base64;
Don't forget to add the following line to your GWT module XML:
<inherits name="com.googlecode.gwt.crypto.Crypto"/>
Maven dependency
<dependency>
<groupId>com.googlecode.gwt-crypto</groupId>
<artifactId>gwt-crypto</artifactId>
<version>2.3.0</version>
</dependency>

Stéphane B.
- 3,260
- 2
- 30
- 35

Ronan Quillevere
- 3,699
- 1
- 29
- 44
0
Base64 class can't be used on the client side. It would have to be emulated.

törzsmókus
- 1,799
- 2
- 21
- 28

Brandon
- 2,034
- 20
- 25