Skip to content

Encode Decode In Spring Boot#

What Is The Encoding/Decoding?#

  • Encoding is defined as the process of converting data from one form to another and has nothing to do with cryptography. It guarantees none of the 3 cryptographic properties of confidentiality, integrity and authenticity because it involves no secret and is completely reversible (decoding).
  • Encoding can be used for reducing the size of audio and video files. Each audio and video file format has a corresponding coder decoder (codec) program that is used to code it into the appropriate format and then decodes for playback.
  • It can't be used for securing data, various publicly available algorithms are used for encoding.
  • Encoding Types: ASCII, BASE64, UNICODE.

Encoding/Decoding In Java#

  • Encoding and decoding in Java is a method of representing data in a different format to efficiently transfer information through a network or the web. The encoder converts data into a web representation. Once received, the decoder converts the web representation data into its original format.
  • Encode is not used for security it is used for data transformation for proper consumption. So it's easy to encode/decode data and you just need to know the encoding. We have 2 popular types of encoding: URL encoding, Base64 encoding.

URL Encoding#

  • In URL encoding, certain characters like space or question mark in URL could be misleading. For example, instead if using Api param like this subject=what is the biggest animal on earth we can encode it to this subject%3Dwhat%20is%20the%20biggest%20animal%20on%20earth.
  • More information

Base64 Encoding#

  • Base64 encoding, when we transfer media data like Music, Files, Images, video... as binaries throw network environment, errors or lossing data will happen easily, some protocols may interpret your binary data as control characters (like modem or router) or they could be screwed up because the underlying protocol might think that you've entered a special character combination (like how FTP translates line endings).
  • In the past, a quick method of verifying data integrity was used: bit parity. So in this method a byte will contain 7 bits of data and one bit for calculating even or odd on total number of 1 in 7 bits in data.
  • Ex: we have a byte which is transferred through network 10100101, then we have 7 bits of data [1010010], this this 7 bits of data we have 3 bits with 1, so the addition bit will be 1 (for odd). So if the end modem received a byte like this 10110101, so there is an even byte but the additional bit is 1, so this byte is not correct or invalid. More information.
  • So nowaday, many router support parity check and byte translation into hardware and will force the computers to attach the parity check and byte translation to deal with 7-bit data. This force email attachments (and all other data, which is why HTTP & SMTP protocols are text-based), to be convert into a text-only format. Then Base64 encoding will help us to handle this issue. More information
  • So in base64 original data will turn into bytes which contain 8 bits for each of them. Then these bits will be chunked together and divided into 6 bits for every byte and these byte will be converted into character. See the example in the table below.

    Original 8 bits Chunked 6 bits Base64
    duc 01100100 01110101 01100011 011001000111010101100011 011001 000111 010101 100011 ZHVj
    ducz 01100100 01110101 01100011 01111010 01100100011101010110001101111010 011001 000111 010101 100011 011110 10 0000 ZHVjeg==
  • Base64 will contains all these characters:

    • 10 numeric value i.e., 0,1,2,3,…..9.
    • 26 Uppercase alphabets i.e., A,B,C,D,…….Z.
    • 26 Lowercase alphabets i.e., a,b,c,d,……..z.
    • special characters i.e., +,/ and = ( = used to replace empty bits in the last 6-bits byte one = is equal 00 ).

Encode Decode Example With Spring Boot#

  • Now, let's take an example for using Encode/Decode Url and Base64 in Spring Boot.
  • By default, Spring Boot has default libraries for encode and decode Base64 and Uri, so we don't need to add any dependency into Spring Boot project.

Controller#

  • Let's create an controller with some apis as below:
Controller.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.springboot.security.encode.decode.app.controller;

import com.springboot.security.encode.decode.app.model.DataRequest;
import com.springboot.security.encode.decode.app.service.EncodeDecodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

    @Autowired
    private EncodeDecodeService encodeDecodeService;

    @RequestMapping(method = RequestMethod.POST, path = "v1/cipher/encode/base64", produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> encodeData(@RequestBody DataRequest inputData) {
        return new ResponseEntity<>(encodeDecodeService.encodeBase64(inputData.getData()), HttpStatus.CREATED);
    }

    @RequestMapping(method = RequestMethod.POST, path = "v1/cipher/decode/base64", produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> decodeData(@RequestBody DataRequest inputData) {
        return new ResponseEntity<>(encodeDecodeService.decodeBase64(inputData.getData()), HttpStatus.CREATED);
    }

    @RequestMapping(method = RequestMethod.POST, path = "v1/cipher/encode/url", produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> encodeUrl(@RequestBody DataRequest inputData) {
        return new ResponseEntity<>(encodeDecodeService.encodeUrl(inputData.getData()), HttpStatus.CREATED);
    }

    @RequestMapping(method = RequestMethod.POST, path = "v1/cipher/decode/url", produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> decodeUrl(@RequestBody DataRequest inputData) {
        return new ResponseEntity<>(encodeDecodeService.decodeUrl(inputData.getData()), HttpStatus.CREATED);
    }

}
  • We will need to create a simple model for request body as below.
DataRequest.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package com.springboot.security.encode.decode.app.model;

import org.springframework.lang.NonNull;

public class DataRequest {
    private String data;

    @NonNull
    public String getData() {
        return data;
    }

    public void setData(@NonNull String data) {
        this.data = data;
    }
}

Service#

  • Now, let's create a Service for encoding/decoding url and Base64
EncodeDecodeService.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.springboot.security.encode.decode.app.service;

import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;
import org.springframework.web.util.UriUtils;

import java.nio.charset.StandardCharsets;

@Service
public class EncodeDecodeService {

    public String encodeBase64(String data) {
        return Base64Utils.encodeToString(data.getBytes());
    }

    public String decodeBase64(String data) {
        return new String(Base64Utils.decode(data.getBytes()));
    }

    public String encodeUrl(String urlString) {
        return UriUtils.encode(urlString, StandardCharsets.UTF_8);
    }

    public String decodeUrl(String urlString) {
        return UriUtils.decode(urlString, StandardCharsets.UTF_8);
    }

}

Testing#

  • Now, let's start your Spring Boot service and use Postman to call apis, you will see results as below:

  • Encode Base64  #zoom

  • Decode Base64  #zoom

  • Encode Uri  #zoom

  • Decode Uri  #zoom

See Also#

References#