Saturday, August 30, 2025
HomeUncategorisedURL Encoding/Decoding using Base64 in Java

URL Encoding/Decoding using Base64 in Java

Base 64 is an encoding scheme that converts binary data into text format so that encoded textual data can be easily transported over network un-corrupted and without any data loss.(Base 64 format reference).

The URL encoding is the same as Basic encoding the only difference is that it encodes or decodes the URL and Filename safe Base64 alphabet and does not add any line separation.

URL encoding

String encodedURL = Base64.getUrlEncoder()
                       .encodeToString(actualURL_String.getBytes());

Explanation: In above code we called Base64.Encoder using getUrlEncoder() and then get the encoded URLstring by passing the byte value of actual URL in encodeToString() method as parameter.

URL decoding

byte[] decodedURLBytes = Base64.getUrlDecoder().decode(encodedURLString);

String actualURL= new String(decodedURLBytes);

Explanation: In above code we called Base64.Decoder using getUrlDecoder() and then decoded the URL string passed in decode() method as parameter then convert return value to actual URL.

Below programs illustrate the Encoding and Decoding URL in Java:

Program 1: URL encoding using Base64 class.




// Java program to demonstrate
// URL encoding using Base64 class
  
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
        // create a sample url String to encode
        String sampleURL = "https:// www.geeksforgeeks.org/";
  
        // print actual URL String
        System.out.println("Sample URL:\n"
                           + sampleURL);
  
        // Encode into Base64 URL format
        String encodedURL = Base64.getUrlEncoder()
                                .encodeToString(sampleURL.getBytes());
  
        // print encoded URL
        System.out.println("encoded URL:\n"
                           + encodedURL);
    }
}


Output:

Sample URL:
Home
encoded URL: aHR0cHM6Ly93d3cuZ2Vla3Nmb3JnZWVrcy5vcmcv

Program 2: URL decoding using Base64 class.




// Java program to demonstrate
// Decoding Basic Base 64 format to String
  
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
        // create a encoded URL to decode
        String encoded = "aHR0cHM6Ly93d3cuZ2Vla3Nmb3JnZWVrcy5vcmcv";
  
        // print encoded URL
        System.out.println("encoded URL:\n"
                           + encoded);
  
        // decode into String URL from encoded format
        byte[] actualByte = Base64.getUrlDecoder()
                                .decode(encoded);
  
        String actualURLString = new String(actualByte);
  
        // print actual String
        System.out.println("actual String:\n"
                           + actualURLString);
    }
}


Output:

encoded URL:
aHR0cHM6Ly93d3cuZ2Vla3Nmb3JnZWVrcy5vcmcv
actual String:
Home

References:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7013 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS