Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to use Base64 to encode and decode a string in Java

1 Answer

0 votes
import java.util.Base64;
 
public class Main {
    public static String Base64Encode(String str) {
        return Base64.getEncoder().encodeToString(str.getBytes());
    }
    public static String Base64Decode(String encodedString) {
        byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
        
        return new String(decodedBytes);

    }
    public static void main(String args[]) {
        String str = "Java, object-oriented programming language";
        
        // Encode
        String encodedString = Base64Encode(str);
        System.out.println("Encoded String = " + encodedString); 
         
        // Decode
        String decodedString = Base64Decode(encodedString);
        System.out.println("Decoded String = " + decodedString);
         
         
    }
}
    
    
    
    
/*
run:
    
Encoded String = SmF2YSwgb2JqZWN0LW9yaWVudGVkIHByb2dyYW1taW5nIGxhbmd1YWdl
Decoded String = Java, object-oriented programming language
    
*/

 

 



answered Nov 9, 2023 by avibootz
edited Nov 20, 2024 by avibootz

Related questions

1 answer 86 views
1 answer 85 views
1 answer 85 views
2 answers 115 views
1 answer 110 views
1 answer 156 views
...