
Having the flexibility to perform such operations in Java is a great way to store and transfer our sensitive data in a secure format. After all, encoder should use A-Za-z0–9-_ character set only. Well, in Base64 encoding, the length of an output-encoded String must be a multiple of three and the encoder adds one or two padding characters (=) at the end of the output as needed in order to meet this requirement.Įncoding and decoding strings are a common practice nowadays in order to keep sensitive data encrypted, and base64 is one of the most widely used encoding formats for the same. You might be wondering, why there are ‘ =’ characters present in the encoded strings. New String(Base64.getUrlDecoder().decode(value.getBytes())) In the same manner, URL decoding from base64 can be done using the decode method of UrlDecoder inner class inside Base64 class. String value = "String encoded = Base64.getUrlEncoder().encodeToString(value.getBytes()) Let’s see how we can encode a sample URL like into base64 using encodeToString method of UrlEncoder inner class inside Base64 class.

URL encoding is slightly different from the normal string encoding as it is based on A-Za-z0–9-_ character set. String encoded = "aGVsbG8gd29ybGQ=" īase64.Decoder decoder = Base64.getDecoder() The decode() method returns a byte array instead of a string, that can be converted into a string using the String constructor. To decode any string, we can use the decode() method of the Decoder class which is an inner class of Base64. String encoded = encoder.encodeToString(bytes) String value = "hello world" īase64.Encoder encoder = Base64.getEncoder()
#Base64 encoding how to#
To understand how the encoding algorithm works, check the example below that describes step by step how to manually encode strings to Base64 (if you are looking for an automatic. Technically, it can be said that it converts eight-bit bytes into six-bit bytes. Let’s see how can we encode a simple string like “ hello world”. Base64 is an encoding algorithm used to alter text and binary streams into printable and easy-to-process form to be consumed by various programs as well as. The Base64 encode algorithm converts any data into plain text. In order to encode any string, we need to first convert it into a bytes array.

To encode any string, we can use the encodeToString() method of Encoder class which is an inner class of Base64. Such encoded data must be decoded locally on your system to avoid any security risk.īase64 encoding and decoding capabilities were added in java8 via the 64 utility class.

You might have come across some websites which instantly perform the encoding and decoding for the user.īut is it safe to use an external platform for decoding and encoding your passwords? Definitely not, as the site might be saving your data in the backend. But what exactly is base64? Base64 is an encoding format which maps the input to a set of characters in the A-Za-z0–9+/ character set. Often, the access token we use, Kubernetes external secrets, or database passwords are encoded in base64 format. We often felt the need for encoding or decoding base64 strings.
