1. Overview

In this tutorial, we’ll explore a few popular ways to encode and decode strings to Base64 in Kotlin.

2. Basics

Base64 is a binary-to-text encoding scheme primarily used in modern-day web applications to transfer binary data as text. As the name suggests, there are 64 symbols in the Base64 number system, which maps to the [A-Za-z0-9+/] character set:

Screenshot-2022-01-29-at-2.45.54-AM

Let’s say that we want to encode the string “Baeldung” using the Base64 encoding.

First, we need to represent each character in the original string as the 8-bit binary value of its ASCII representation. Then, we group the 8-bit stream into a 6-bit stream and get the Base64 index for each 6-bit value. Finally, we use the Base64 mapping table to get the Base64 digit corresponding to the index:

Screenshot-2022-01-29-at-2.27.31-AM

We can notice that 3 bytes of binary data can be represented by 4 Base64 digits. Further, each 6-bit zero-padding at the end of the stream is represented by the = symbol.

Finally, we can conclude that “QmFlbGR1bmc=” is the Base64 representation of the string “Baeldung”.

3. Base64 Utility

We can use the java.util.Base64 library to encode and decode strings in Kotlin.

First, let’s use the encoder to encode the “Baeldungstring:

val originalString = "Baeldung"
val encodedString: String = Base64.getEncoder().encodeToString(originalString.toByteArray())
assertEquals("QmFlbGR1bmc=", encodedString)

Internally, the getEncoder() method of the Base64 class gets us an encoder that’s compliant with the encoding standards described in RFC4648.

Next, let’s use the getDecoder() method of the Base64 class to decode the Base64 string representation to the original text:

val encodedString = "QmFlbGR1bmc="
val decodedString: String = String(Base64.getDecoder().decode(encodedString))
assertEquals("Baeldung", decodedString)

We just validated that “QmFlbGR1bmc=” is the correct Base64 representation of the string “Baeldung”.

Similar to the getEncoder() method, the getDecoder() method also returns a Base64 decoder that’s compliant with the RFC4648 standards.

4. Apache Commons Base64 Codec

Let’s learn how we can use the Apache Commons Base64 for Base64 to encode and decode strings.

First, we need to add the commons-codec dependency to our project’s pom.xml:

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version>
</dependency>

Next, we need to explicitly import the org.apache.commons.codec.binary.Base64 in our Kotlin class with an alias such as ApacheBase64, to prevent name conflict with the java.util.Base64 class:

import org.apache.commons.codec.binary.Base64 as ApacheBase64

Now, let’s create an instance of the ApacheBase64 class and use that to encode the string “Baeldung”:

val originalString = "Baeldung"
val base64: ApacheBase64 = ApacheBase64()
val encodedStr = String(base64.encode(originalString.toByteArray()))
assertEquals("QmFlbGR1bmc=", encodedStr)

We must note that we used the toByteArray() method to get the byte-array representation of the string before passing it to the encode() method.

Likewise, let’s also validate the decoding of the string “QmFlbGR1bmc=” using the decode() method:

val encodedString = "QmFlbGR1bmc="
val base64: ApacheBase64 = ApacheBase64()
val decodedString: String = String(base64.decode(encodedString))
assertEquals("Baeldung", decodedString)

Here, the decode() method returns a byte-array representation. Therefore, we need to get a String object to validate against the original String object.

5. Conclusion

In this article, we learned the basics of Base64 encoding and used libraries such as java.util.Base64 and Apache Commons Base64 Codec for Base64 for encoding and decoding strings.

As always, the complete source code for the tutorial is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.