Interfaces for Custom Decryption in Java


Write a Java program to demonstrate the use of an interface for custom decryption

  • The Decryptor interface defines a single method decrypt(String data) for decrypting data.
  • In the main method of the Main class, an instance of the Decryptor interface is created using a lambda expression that performs Base64 decoding.
  • The encrypted data string is decrypted using the decrypt method of the Decryptor interface.
  • The encrypted and decrypted data are printed to the console.

Source Code

import java.util.Base64;
interface Decryptor
{
	String decrypt(String data);
}
 
public class Main
{
	public static void main(String[] args)
	{
		Decryptor decryptor = data -> new String(Base64.getDecoder().decode(data));
 
		String encryptedData = "SGVsbG8gV29ybGQhIDEyMw==";
		String decryptedData = decryptor.decrypt(encryptedData);
 
		System.out.println("Encrypted Data : " + encryptedData);
		System.out.println("Decrypted Data : " + decryptedData);
	}
}

Output

Encrypted Data : SGVsbG8gV29ybGQhIDEyMw==
Decrypted Data : Hello World! 123

Example Programs