Encoding as Base64 in Java

To encode a string as Base64 in Java, you can use the java.util.Base64 class. Here's an example of how you can use the Base64 class to encode a string:

import java.util.Base64;

public class Main {
    public static void main(String[] args) {
        String originalString = "Hello, World!";

        // Encode the string
        String encodedString = Base64.getEncoder().encodeToString(originalString.getBytes());

        System.out.println("Encoded string: " + encodedString);
    }
}

This will output the following string:

Encoded string: SGVsbG8sIFdvcmxkIQ==

To decode a Base64-encoded string, you can use the decode method of the Base64 class like this:

import java.util.Base64;

public class Main {
    public static void main(String[] args) {
        String encodedString = "SGVsbG8sIFdvcmxkIQ==";

        // Decode the string
        byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
        String decodedString = new String(decodedBytes);

        System.out.println("Decoded string: " + decodedString);
    }
}

This will output the following string:

Decoded string: Hello, World!

I hope this helps! Let me know if you have any questions.